GiNZAを使って日本語の文章解析をしようとしたところ、spacy.load("ja_ginza") でエラーにハマったので、その備忘録です。
環境
!pip install -q spacy ginza ja_ginza
import spacy
import ginza
import ja_ginza
print(f"spacy version: {spacy.__version__}")
print(f"ja_ginza version: {ja_ginza.__version__}")
spacy version: 3.8.14
ja_ginza version: 5.2.0
やりたかったこと
GiNZAを使って日本語テキストを解析する。
# GiNZAのモデルをロード
nlp = spacy.load("ja_ginza")
# テキスト読み込み
with open('sample.txt', 'r', encoding='utf-8') as f:
text = f.read()
# テキストを解析(Docオブジェクトが生成される)
doc = nlp(text)
# 解析結果の抽出
for sent in doc.sents: # 文単位でループ
print(f"--- 文: {sent.text} ---")
for token in sent: # 単語単位でループ
print(f"{token.i:<3} {token.text:<5} {token.tag_}")
発生したエラー
---------------------------------------------------------------------------
ConfigValidationError Traceback (most recent call last)
/tmp/ipykernel_6870/3158883143.py in <cell line: 0>()
8
9 # GiNZAのモデルをロード
---> 10 nlp = spacy.load("ja_ginza")
11
12 # テキスト読み込み
15 frames
/usr/local/lib/python3.12/dist-packages/confection/_registry.py in _validate_promise_args(filled, schema, func_name, parent)
343 )
344 if errors:
--> 345 raise ConfigValidationError(
346 config=filled,
347 errors=errors,
ConfigValidationError:
Config error for 'compound_splitter'
compound_splitter -> split_mode at root: None is not <class 'str'>
{'nlp': <spacy.lang.ja.Japanese object at 0x78053766b0e0>, 'name': 'compound_splitter', 'split_mode': None, '@factories': 'compound_splitter'}
どうも split_modeがNoneまたはstrではない のがお気に召さないらしい...
対処法①:configを明示的に渡す
- nlp = spacy.load("ja_ginza")
+ config = {
+ "components": {
+ "compound_splitter": {
+ "split_mode": "A",
+ }
+ }
+ }
+ nlp = spacy.load("ja_ginza", config=config)
対処法②:spacyのバージョンを下げる
!pip install -q spacy==3.8.11 ginza ja_ginza
対処法①か②のどちらかを行うと動きます
動作確認(成功例)
--- 文: 「現在かくの如く切迫した決戦期にあり、国民の多くが一人で二人前も三人前も働いている時そしてその戦闘労力の要求が更に増大していくと信ぜられる時に、主食一割減という事実は各方面にかなり大きな問題を投げているようです」 ---
0 「 補助記号-括弧開
1 現在 名詞-普通名詞-副詞可能
2 かく 動詞-一般
3 の 助詞-準体助詞
4 如く 助動詞
5 切迫 名詞-普通名詞-サ変可能
...(続く)
まとめ
-
spacy 3.8.14+ja_ginza 5.2.0の組み合わせでエラー発生 -
原因は
compound_splitter.split_mode=None または not str -
回避方法は以下のどちらか:
-
configを明示的に渡す -
spacyのバージョンを下げる
-