Package: katsuyo-text
Katsuyo Text日本語の活用変換器A Japanese conjugation form converterMotivation日本語文法における活用変形をロジックに落とし込めるかの試み⚠CAUTION現状、挙動は不安定です。必要に応じてアップデートしたいです。How to Use追加fromkatsuyo_text.katsuyo_text_helperimport(Hitei,KakoKanryo,DanteiTeinei,)fromkatsuyo_text.spacy_katsuyo_text_detectorimportSpacyKatsuyoTextSourceDetectorimportspacynlp=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())# => 行かなかったです変換fromkatsuyo_text.katsuyo_text_helperimport(Teinei,Dantei,DanteiTeinei,)fromkatsuyo_text.spacy_sentence_converterimportSpacySentenceConverterimportspacynlp=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で実現しているfromkatsuyo_text.katsuyo_textimportTaigenText,JODOUSHI_NAITaigenText("大丈夫")+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'>fromkatsuyo_text.katsuyo_text_helperimportHiteiTaigenText("大丈夫")+Hitei()# => KatsuyoText(gokan='大丈夫ではな', katsuyo=KeiyoushiKatsuyo(katei='けれ', rentai='い', shushi='い', renyo_ta='かっ', renyo='く', mizen='かろ'))TaigenText("大丈夫")+Hitei()==Hitei().bridge(TaigenText("大丈夫"))# => Truebridgeはカスタマイズ可能fromkatsuyo_text.katsuyo_textimportKatsuyoText,TaigenText,KAKUJOSHI_GAfromkatsuyo_text.katsuyoimportKEIYOUSHInai=KatsuyoText(gokan="な",katsuyo=KEIYOUSHI)custom_hitei=Hitei(bridge=lambdasrc:src+KAKUJOSHI_GA+nai)TaigenText("耐性")+custom_hitei# => KatsuyoText(gokan='耐性がな', katsuyo=KeiyoushiKatsuyo(katei='けれ', rentai='い', shushi='い', renyo_ta='かっ', renyo='く', mizen='かろ'))IKatsuyoTextHelperで独自の活用変形を実装可能fromtypingimportOptionalfromkatsuyo_text.katsuyo_text_helperimportIKatsuyoTextHelperfromkatsuyo_text.katsuyo_textimport(TaigenText,KatsuyoTextError,IKatsuyoTextSource,SetsuzokujoshiText,KURU,SETSUZOKUJOSHI_KARA,JUNTAIJOSHI_NO,JODOUSHI_DA_DANTEI,)classJunsetsuKakutei(IKatsuyoTextHelper[SetsuzokujoshiText]):deftry_merge(self,pre:IKatsuyoTextSource)->Optional[SetsuzokujoshiText]:try:pre+SETSUZOKUJOSHI_KARAexceptKatsuyoTextErrorase:# Handle errorreturnNoneKURU# => KatsuyoText(gokan='', katsuyo=KaGyoHenkakuKatsuyo(meirei='こい', katei='くれ', rentai='くる', shushi='くる', renyo='き', mizen='こ'))KURU+JunsetsuKakutei()# => SetsuzokujoshiText(gokan='くるから', katsuyo=None)custom_junsetsu_kakutei=JunsetsuKakutei(bridge=lambdasrc: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)