NER のための EntityRuler
EntityRuler は、既存モデルの EntityRecognizer と組み合わせて精度を高めることができます。この演習では、en_core_web_sm モデルの既存の NER コンポーネントと EntityRuler コンポーネントを組み合わせる練習をします。モデルはすでに nlp として読み込まれています。
EntityRuler を NER コンポーネントの前に追加すると、エンティティ認識器は既存のエンティティ範囲を尊重し、EntityRuler に追加したパターンに基づいて予測を調整し、固有表現認識の精度を向上させます。
この演習はコースの一部です
spaCyで学ぶNatural Language Processing
演習の手順
nerコンポーネントの前にEntityRulerをnlpに追加します。- 小文字の
new york groupをORGとして分類するトークンエンティティパターンを定義します。 - その
patternsをEntityRulerコンポーネントに追加します。 - モデルを実行し、
Docコンテナからエンティティのテキストとタイプのタプルを出力します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
nlp = spacy.load("en_core_web_sm")
text = "New York Group was built in 1987."
# Add an EntityRuler to the nlp before NER component
ruler = nlp.____("entity_ruler", ____="ner")
# Define a pattern to classify lower cased new york group as ORG
patterns = [{"label": "ORG", "pattern": [{"lower": ____}]}]
# Add the patterns to the EntityRuler component
ruler.____(____)
# Run the model and print entities text and type for all the entities
doc = ____
print([(ent.____, ent.____) for ent in doc.____])