spaCy での NER
Named entity recognition (NER) は、人名や地名など、文書内の重要な要素を簡単に特定するのに役立ちます。非構造化データを整理し、重要情報を検出できるため、大規模なデータセットを扱う際に不可欠です。この演習では、Named Entity Recognition を練習します。
en_core_web_sm は nlp として読み込まれています。Airline Travel Information System (ATIS) データセットからの 3 つのコメントが、texts というリストで用意されています。
この演習はコースの一部です
spaCyで学ぶNatural Language Processing
演習の手順
- リスト内包表記を使って、
textsの各テキストに対応するDocコンテナをすべて含むリストdocumentsを作成します。 - 各
docコンテナについて、doc.entsを反復処理して、各固有表現のテキストと対応するラベルを表示します。 - 2 番目の
Docコンテナについて、6 番目のトークンのテキストと、その固有表現タイプを表示します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Compile a list of all Doc containers of texts
documents = [____ for text in texts]
# Print the entity text and label for the entities in each document
for doc in documents:
print([(____, ____) for ent in ____])
# Print the 6th token's text and entity type of the second document
print("\nText:", documents[1][5].____, "| Entity type: ", documents[1][5].____)