LoginSignup
2
0

More than 1 year has passed since last update.

TL;DR

pip install chikkarpy sudachipy sudachidict_core
from chikkarpy import Chikkar
from chikkarpy.dictionarylib import Dictionary as chikkardict
from sudachipy import Dictionary, SplitMode

test_txt = 'オンラインアイスクリーム'

chikkar = Chikkar()
chikkar.add_dictionary(chikkardict())
chikkar.enable_verb()

tokenizer = Dictionary().create(mode=SplitMode.C)
morphemes = tokenizer.tokenize(test_txt)

for m in morphemes:
    print(m.surface(), chikkar.find(m.normalized_form()))
>>> オンライン ['online']
>>> アイスクリーム ['ice cream', 'アイス', 'ice']

chikkarpyとは

chikkarpyはchikkarのPython版です。 chikkarpy は Sudachi 同義語辞書を利用するためのライブラリです。
また、「ちっか」とは徳島では「ちくわ」のことを指します。

プログラムの簡単な説明

chikkar = Chikkar()
chikkar.add_dictionary(chikkardict())
chikkar.enable_verb()

まず、chikkar.add_dictionary()で任意の同義語辞書を読み込ませます。
ここではchikkardict()に何も引数を与えていないため、chikkarpyが持っている同義語辞書を読み込んでいます。同義語辞書については 公式のGitHub をご参照ください。
別の同義語辞書を使いたい場合はchikkar.add_dictionary(chikkardict(user_dict_path))のようにパスを指定して使います。たとえば、http://sudachi.s3-website-ap-northeast-1.amazonaws.com/sudachisynonym/ で新しいバージョンの同義語辞書を入手できますが、その中にある「system_synonym.dic」をパス指定することで別のバージョンの同義語辞書を使うことができます。
または、同義語辞書を自作して使うこともできます。辞書の作り方は 同義語検索ライブラリchikkarpyで独自の辞書を使う の記事で解説されています。

chikkar.enable_verb()は用言の検索を許可する場合に設定します。
体言のみ検索したいのであれば不要なのでこの行を消してください。

$ echo "勉強" | chikkarpy
勉強    学習,研究,スタディー,スタディ,study
$ echo "勉強" | chikkarpy -ev
勉強    学習,研究,スタディー,スタディ,study,学ぶ

コマンドライン上での例になりますが、「勉強」に対してenable_verbを有効にすることで「学ぶ」が同義語に追加されています。

tokenizer = Dictionary().create(mode=SplitMode.C)
morphemes = tokenizer.tokenize(test_txt)

for m in morphemes:
    print(m.surface(), chikkar.find(m.normalized_form()))

そして、sudachipyで解析した結果を使ってchikkar.find()で同義語を検索します。
例ではm.normalized_form()を渡していますが、m.surface()でも可です。
ただし、活用の変化などは同義語辞書で考慮されていないため、正規化していないと検索に引っかからない場合があります。

終わり

ということで、chikkarpyの紹介でした。
chikkarpyはWorks Applicationsが開発・公開しており、不具合や質問、要望等があればgithubにissueを立てていただくと対応できるかもしれません。
また、開発者やユーザーの方々が質問したり議論するためのSlackワークスペースを用意しています。そちらでも受け付けているためご活用ください。
https://github.com/WorksApplications/chikkarpy#contact

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0