spaCyで複数パターンを使うEntityRuler
EntityRuler を使うと、エンティティを doc.ents に追加して固有表現認識の精度を高められます。この演習では、既存の nlp パイプラインに EntityRuler コンポーネントを追加し、複数のエンティティが正しく分類されることを確認します。
en_core_web_sm モデルはすでに読み込まれており、nlp として利用できます。example_text でサンプルテキストにアクセスでき、nlp と doc を使って、それぞれ spaCy モデルと example_text の Doc コンテナにアクセスします。
この演習はコースの一部です
spaCyで学ぶNatural Language Processing
演習の手順
nlpモデルでexample_text中のエンティティのテキストとタイプのタプル一覧を表示します。- 小文字の
brotherとsistersにマッチする複数のパターンを定義し、PERSONラベルに対応付けます。 nlpパイプラインにEntityRulerコンポーネントを追加し、patternsをEntityRulerに追加します。nlpモデルでexample_textのエンティティのテキストとタイプのタプルを表示します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
nlp = spacy.load("en_core_web_md")
# Print a list of tuples of entities text and types in the example_text
print("Before EntityRuler: ", [____ for ____ in nlp(____).____], "\n")
# Define pattern to add a label PERSON for lower cased sisters and brother entities
patterns = [{"label": ____, "pattern": [{"lower": ____}]},
{"label": ____, "pattern": [{"lower": ____}]}]
# Add an EntityRuler component and add the patterns to the ruler
ruler = nlp.____("entity_ruler")
ruler.____(____)
# Print a list of tuples of entities text and types
print("After EntityRuler: ", [(ent.____, ent.____) for ent in nlp(example_text).____])