GithubHelp home page GithubHelp logo

katsuyo-text's Introduction

Katsuyo Text

日本語の活用変換器
A Japanese conjugation form converter

Motivation

日本語文法における活用変形をロジックに落とし込めるかの試み

⚠CAUTION

現状、挙動は不安定です。必要に応じてアップデートしたいです。

How to Use

追加

from katsuyo_text.katsuyo_text_helper import (
    Hitei,
    KakoKanryo,
    DanteiTeinei,
)
from katsuyo_text.spacy_katsuyo_text_detector import SpacyKatsuyoTextSourceDetector
import spacy


nlp = spacy.load("ja_ginza")
src_detector = SpacyKatsuyoTextSourceDetector()


doc = nlp("今日は旅行に行く")
sent = next(doc.sents)
katsuyo_text = src_detector.try_detect(sent[-1])

katsuyo_text
# => KatsuyoText(gokan='行', katsuyo=GodanKatsuyo(renyo_ta='っ', mizen_u='こ', meirei='け', katei='け', rentai='く', shushi='く', renyo='き', mizen='か'))

print(katsuyo_text + Hitei())
# => 行かない
print(katsuyo_text + Hitei() + KakoKanryo())
# => 行かなかった
print(katsuyo_text + Hitei() + KakoKanryo() + DanteiTeinei())
# => 行かなかったです

変換

from katsuyo_text.katsuyo_text_helper import (
    Teinei,
    Dantei,
    DanteiTeinei,
)
from katsuyo_text.spacy_sentence_converter import SpacySentenceConverter
import spacy


nlp = spacy.load("ja_ginza")
converter = SpacySentenceConverter(
    conversions_dict={
        Teinei(): None,
        DanteiTeinei(): Dantei(),
    }
)


doc = nlp("今日は旅行に行きました")
sent = next(doc.sents)
print(converter.convert(sent))
# => 今日は旅行に行った

doc = nlp("今日は最高の日でした")
sent = next(doc.sents)
print(converter.convert(sent))
# => 今日は最高の日だった

カスタマイズ

文法的に成立しない活用変形を bridge で実現している

from katsuyo_text.katsuyo_text import TaigenText, JODOUSHI_NAI

TaigenText("大丈夫") + JODOUSHI_NAI
# error => katsuyo_text.katsuyo_text.KatsuyoTextError: Unsupported katsuyo_text in merge of <class 'katsuyo_text.katsuyo_text.Nai'>: 大丈夫 type: <class 'katsuyo_text.katsuyo_text.TaigenText'>

from katsuyo_text.katsuyo_text_helper import Hitei
TaigenText("大丈夫") + Hitei()
# => KatsuyoText(gokan='大丈夫ではな', katsuyo=KeiyoushiKatsuyo(katei='けれ', rentai='い', shushi='い', renyo_ta='かっ', renyo='く', mizen='かろ'))

TaigenText("大丈夫") + Hitei() == Hitei().bridge(TaigenText("大丈夫"))
# => True

bridge はカスタマイズ可能

from katsuyo_text.katsuyo_text import KatsuyoText, TaigenText, KAKUJOSHI_GA
from katsuyo_text.katsuyo import KEIYOUSHI

nai = KatsuyoText(gokan="な", katsuyo=KEIYOUSHI)
custom_hitei = Hitei(bridge=lambda src: src + KAKUJOSHI_GA + nai)

TaigenText("耐性") + custom_hitei
# => KatsuyoText(gokan='耐性がな', katsuyo=KeiyoushiKatsuyo(katei='けれ', rentai='い', shushi='い', renyo_ta='かっ', renyo='く', mizen='かろ'))

IKatsuyoTextHelper で独自の活用変形を実装可能

from typing import Optional
from katsuyo_text.katsuyo_text_helper import IKatsuyoTextHelper
from katsuyo_text.katsuyo_text import (
    TaigenText,
    KatsuyoTextError,
    IKatsuyoTextSource,
    SetsuzokujoshiText,
    KURU,
    SETSUZOKUJOSHI_KARA,
    JUNTAIJOSHI_NO,
    JODOUSHI_DA_DANTEI,
)


class JunsetsuKakutei(IKatsuyoTextHelper[SetsuzokujoshiText]):
    def try_merge(self, pre: IKatsuyoTextSource) -> Optional[SetsuzokujoshiText]:
        try:
            pre + SETSUZOKUJOSHI_KARA
        except KatsuyoTextError as e:
            # Handle error
            return None


KURU
# => KatsuyoText(gokan='', katsuyo=KaGyoHenkakuKatsuyo(meirei='こい', katei='くれ', rentai='くる', shushi='くる', renyo='き', mizen='こ'))
KURU + JunsetsuKakutei()
# => SetsuzokujoshiText(gokan='くるから', katsuyo=None)

custom_junsetsu_kakutei = JunsetsuKakutei(bridge=lambda src: src + JODOUSHI_DA_DANTEI + SETSUZOKUJOSHI_KARA)

TaigenText("症状") + JunsetsuKakutei()
# error => katsuyo_text.katsuyo_text.KatsuyoTextError: Unsupported katsuyo_text in merge of <class '__main__.JunsetsuKakutei'>: 症状 type: <class 'katsuyo_text.katsuyo_text.TaigenText'> katsuyo: <class 'NoneType'>
TaigenText("症状") + custom_junsetsu_kakutei
# => SetsuzokujoshiText(gokan='症状だから', katsuyo=None)

katsuyo-text's People

Contributors

sadahry avatar

Stargazers

 avatar

Watchers

 avatar

katsuyo-text's Issues

bridge at first

IKatsuyoTextHelper について、
bridge メソッドを先に呼び出せるオプションをつける。

これにより、カスタマイズされた活用変形を優先して出力できるようになる。

課題:

  • 現状 bridge の失敗 -> raise error となっている
    • bridge の失敗 + try_merge の失敗 -> raise error となるよう仕組みを変える必要がある

Lazy addition(+)

KatsuyoTextを + で繋げた際、部分的に切り出すことが困難
(たとえば「今日」 +「です」 + 「た」 から「です」を抜くには再度detectしないといけない)

str()(もしくは同等のメソッド)するまで原型を保持しておくことで
「今日」 +「です」 + 「た」 -「です」ができるようになるはず

Hiteiの意味的convert

現状では助動詞「ない」のみのconvertであり
以下をカバーできていない。

  • 形容詞「ない」の否定
    • e.g,「ある」に変える
  • 「非〇〇」の否定
    • e.g, 「非」を消せるオプション
  • 「仕方がない」等の「助詞」+「ない」の慣用句
    • e.g, 辞書から慣用句がヒットした場合変換しない

文章全体の意味としてHiteiをconvertできるように

助詞のconvert

convertメソッドの対象に FukujoshiTextAppendant, SetsuzokujoshiTextAppendant, ShujoshiTextAppendant を含めたい
(それぞれ専用のHelperを作ってもよい)

ただしFukujoshiTextAppendant, SetsuzokujoshiTextAppendant, は None を許容しない。
(助動詞のように、文中から文字を抜いて成立する類の品詞ではないため)

用途は不明なのでメモとして残すのみ

終助詞の意味分類

文法のみで意味分類が不可能なケースが多数存在する。

e.g.,

の[終助](上昇調のイントネーションを伴って)質問または疑問の意を表す。

終助詞をどうやって手軽に扱えるHelperとするのかが課題

Helperの品詞選択

例えば Hitei ヘルバーにおいて、
語尾を「ぬ」に変えたい場合に ヘルパー のコンストラクタへクラスを指定すれば
指定した語尾に変えてくれるようなオプションを個別に実装していきたい

# TODO オプションで「ぬ」を選択できるように

意志推量「う」「よう」の実装

意志推量は助動詞ではなく活用形として形態素解析されるため
detectorとしては1文字から2つKatsuyoTextを出力する必要がある

大丈夫でしょう
# text = 大丈夫でしょう
1       大丈夫  大丈夫  ADJ     形状詞-一般     _       0       root    _       SpaceAfter=No|BunsetuBILabel=B|BunsetuPositionType=ROOT|Reading=ダイジョウブ
2       でしょう        です    AUX     助動詞  _       1       aux     _       SpaceAfter=No|BunsetuBILabel=I|BunsetuPositionType=SYN_HEAD|Inf=助動詞-デス,意志推量形|Reading=デショウ

-> 「です(でしょ)」と「う」に分離

方言対応

対応工数がかかるため現状サポートしていない

残りの副助詞の実装

sudachi辞書に含まれる副助詞をすべて対応していない。
残りの副助詞に対応する必要がある。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.