公式ドキュメント
SudachiPy/docs/tutorial.md at develop · WorksApplications/SudachiPy
Python version of Sudachi, a Japanese tokenizer. Contribute to WorksApplications/SudachiPy development by creating an account on GitHub.
sudachipyのバージョン等
anaconda環境で動かしているsudachipy(v0.6.10)。
$ pip show sudachipy
Name: SudachiPy
Version: 0.6.10
Summary: Python version of Sudachi, the Japanese Morphological Analyzer
Home-page: https://github.com/WorksApplications/sudachi.rs/tree/develop/python
Author: Works Applications
Author-email: sudachi@worksap.co.jp
License: Apache-2.0
Location: C:\Users\og3\anaconda3\Lib\site-packages
Requires:
Required-by: SudachiDict-full
sudachidict_fullの辞書ファイルが見つからない
$ sudachipy ubuild -s full -o dict/taberogu_dict.dic dict/taberogu_ryori_dict.csv
System dictionary file full does not exist
=>sudachidict_fullの辞書が見つからないらしい
sudachidict_fullの辞書がどこにあるか確認
lsコマンドで探す。
pipでインストールしたがanaconda環境にて動かしているのでanaconda3以下にある。
$ ls C:/Users/og3/anaconda3/Lib/site-packages/sudachidict_full/resources/
LEGAL LICENSE-2.0.txt system.dic
=>system.dicが辞書ファイル。
sudachipy ubuildでcsvのフォーマットエラーが出る
sudachidict_fullの辞書ファイルのパスを指定してビルドする
以下でビルドすると、ユーザー辞書の素になるcsvファイルのフォーマットエラーがたくさん出る。
$ sudachipy ubuild -s C:/Users/og3/anaconda3/Lib/site-packages/sudachidict_full/resources/system.dic -o dict/taberogu_dict.dic dict/taberogu_ryori_dict.csv
Invalid i16 literal
csvの1行目でエラーが出た。
sudachipy.errors.SudachiError: WindowsPath('dict/taberogu_ryori_dict.csv'):
dict\taberogu_ryori_dict.csv:1 Invalid i16 literal 左文脈ID
=> csvを確認する
表層形,左文脈ID,右文脈ID,コスト,品詞,品詞細分類1,品詞細分類2,品詞細分類3,活用型,活用形,原形,読み,発音
天ぷら,1287,1287,5000,名詞,普通名詞,一般,*,*,*,天ぷら,てんぷら,てんぷら
ごま合え,1287,1287,5000,名詞,普通名詞,一般,*,*,*,ごま合え,ごまあえ,ごまあえ
おひたし,1287,1287,5000,名詞,普通名詞,一般,*,*,*,おひたし,おひたし,おひたし
---以下略
webで記事を見ると、ヘッダー(カラム名)はいらないっぽい。
「左文脈ID」は本来、数値が入るところにヘッダーの文字列が入ったからエラーになった模様。
=>ヘッダーを消して再度実行する。
Field (13) dic-form did not exist in CSV lexicon
sudachipy.errors.SudachiError: WindowsPath('dict/taberogu_dict.csv'):
dict\taberogu_dict.csv:1 Field (13) dic-form did not exist in CSV lexicon
=>csvを確認する
天ぷら,1287,1287,5000,名詞,普通名詞,一般,*,*,*,天ぷら,てんぷら,てんぷら
ごま合え,1287,1287,5000,名詞,普通名詞,一般,*,*,*,ごま合え,ごまあえ,ごまあえ
おひたし,1287,1287,5000,名詞,普通名詞,一般,*,*,*,おひたし,おひたし,おひたし
Fieldの13番目がないってことか。13個の要素はあるが、0から数えたら12個ってことになる?
=>追加してみる。
【解決】読み込ませるcsvの要素数が18列必要だった
公式ドキュメントを見たら書いてあった。。csvを以下のように変更する。
天ぷら,1287,1287,5000,名詞,普通名詞,一般,*,*,*,天ぷら,てんぷら,てんぷら,*,*,*,*,*
=>ビルドする。。
$ sudachipy ubuild -s C:/Users/og3/anaconda3/Lib/site-packages/sudachidict_full/resources/system.dic -o dict/taberogu_dict.dic dict/taberogu_dict.csv
C:\Users\og3\dev\find_kuso_review_from_tabelog\dict\taberogu_dict.csv -> 1 in 0.01 sec
validate -> 1 in 0.00 sec
pos_table -> 32 in 0.00 sec
conn_matrix -> 36 in 0.00 sec
trie -> 1028 in 0.00 sec
word_id table -> 9 in 0.00 sec
word_params -> 10 in 0.00 sec
wordinfo_offsets -> 4 in 0.00 sec
wordinfos (copy only) -> 34 in 0.00 sec
=>できた!
カスタム辞書が読み込めないエラー
$ python tokenize_review/tokenize_review_by_sudachi.py
Traceback (most recent call last):
File "C:\Users\ai\dev\find_kuso_review_from_taberogu\tokenize_review\tokenize_review_by_sudachi.py", line 5, in <module>
tokenizer_obj = dictionary.Dictionary("dict/taberogu_dict.dic").create()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sudachipy.errors.SudachiError: Serde error: expected value at line 1 column 1
=>指定方法が間違ってた
tokenizer_obj = dictionary.Dictionary(dict="dict/taberogu_dict.dic").create()
=>これで読み込めた!
Invalid header: Invalid system dictionary version【未解決】
ビルドした辞書ファイルが読み込めない。。
$ python tokenize_review/tokenize_review_by_sudachi.py
Traceback (most recent call last):
File "C:\Users\ai\dev\find_kuso_review_from_taberogu\tokenize_review\tokenize_review_by_sudachi.py", line 5, in <module>
tokenizer_obj = dictionary.Dictionary(dict="dict/taberogu_dict.dic").create()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sudachipy.errors.SudachiError: "Error while constructing dictionary": Invalid header: Invalid system dictionary version
ビルドはうまく通っている
ubuildでビルドした辞書はエラー等は出ずに問題なくできた。
$ sudachipy ubuild -s C:/Users/ai/anaconda3/Lib/site-packages/sudachidict_core/resources/system.dic -o dict/taberogu_dict.dic dict/taberogu_ryori_dict.csv
C:\Users\ai\dev\find_kuso_review_from_taberogu\dict\taberogu_ryori_dict.csv -> 155 in 0.00 sec
validate -> 155 in 0.00 sec
pos_table -> 5576 in 0.00 sec
conn_matrix -> 5580 in 0.00 sec
trie -> 10244 in 0.00 sec
word_id table -> 777 in 0.00 sec
word_params -> 934 in 0.00 sec
wordinfo_offsets -> 620 in 0.00 sec
wordinfos (copy only) -> 7414 in 0.00 sec
ヘッダーの値を確認してみる
エラー内容が「Invalid header」なのでビルドしたカスタム辞書のヘッダーの値を確認する。
def print_binary_header(file_path):
with open(file_path, "rb") as f:
data = f.read(40)
print(" ".join(f"{b:02X}" for b in data))
print("taberogu_dict.dic ヘッダー:")
print_binary_header("dict/taberogu_dict.dic")
print("\nsystem.dic ヘッダー:")
print_binary_header("C:/Users/ai/anaconda3/Lib/site-packages/sudachidict_core/resources/system.dic")
print("\nfull_system.dic ヘッダー:")
print_binary_header("C:/Users/ai/anaconda3/Lib/site-packages/sudachidict_full/resources/system.dic")
=>
taberogu_dict.dic ヘッダー:
B0 4F F6 6F 75 11 98 CA F2 E3 BF 67 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
core_system.dic ヘッダー:
34 44 39 92 1A 01 9F CE BB 66 B5 B0 94 01 00 00 32 30 32 35 30 31 32 39 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
full_system.dic ヘッダー:
34 44 39 92 1A 01 9F CE 0F C9 B5 B0 94 01 00 00 32 30 32 35 30 31 32 39 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
カスタム辞書のヘッダーは公式辞書とは異なることがわかった。なんでやねん。。とりあえず、ビルドの際に使用した辞書のヘッダーと同じにしてみる。(fullと同じにする)
def fix_dictionary_header(target_file, reference_file, bytes_to_replace=32):
with open(reference_file, "rb") as ref, open(target_file, "r+b") as target:
reference_header = ref.read(bytes_to_replace) # `system.dic` のヘッダーを取得
target.seek(0) # ファイルの先頭に移動
target.write(reference_header) # ヘッダーを書き換える
print(f"✅ {target_file} のヘッダーを {reference_file} と一致させました")
fix_dictionary_header("dict/taberogu_dict.dic", "C:/Users/ai/anaconda3/Lib/site-packages/sudachidict_full/resources/system.dic", bytes_to_replace=32)
ヘッダーの値をビルドする際に使用した公式辞書に合わせてみると、同じエラーは出なくなった。代わりにcsvに入ってない値についてのエラーが出た。
$ python tokenize_review/tokenize_review_by_sudachi.py
Traceback (most recent call last):
File "C:\Users\ai\dev\find_kuso_review_from_taberogu\tokenize_review\tokenize_review_by_sudachi.py", line 4, in <module>
tokenizer_obj = dictionary.Dictionary(dict="dict/taberogu_dict.dic").create()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sudachipy.errors.SudachiError: "Error while constructing dictionary": oov: plugin com.worksap.nlp.sudachi.MeCabOovPlugin setup: Invalid part of speech: POS 補助記号,一般,*,*,*,* was not in the dictionary, user-defined POS are forbidden
いや、入ってないでしょ?「補助記号,一般,*,*,*,*」なんて。。
カスタム辞書の素ファイル:
https://docs.google.com/spreadsheets/d/1DHCvjYSZV4dJF-IDrVIRwc-Uppnub8wkz31B8b16xFY/edit?usp=sharing
sudachipy(v0.6.10)のユーザー辞書を作る際のcsvの正しいフォーマット
正しいCSVのフォーマットは以下の通り。

sudachipy(v0.6.10)のユーザー辞書のフォーマットとサンプル
sudachipy(v0.6.10)のユーザー辞書のフォーマットとサンプルsudachipy(v0.6.10)向け$ pip show sudachipyName: SudachiPyVersion: 0.6.10Summary: Python version of Sudach...
コメント