stanzaを使ってみる 4
以前の投稿でstanzaを使ってみたことをまとめた.
今回はその続きで固有表現認識(NER)をstanzaで行ってみたい.
コードは下記.
import stanza
# モデルのダウンロード(初回のみ)
stanza.download('en')
# NERを含むパイプラインを初期化
nlp = stanza.Pipeline(lang='en', processors='tokenize,ner')
# 文章
text = """
John and Mary recently moved to a quiet town near the mountains.
They often visit Central Park on weekends and enjoy coffee at a small café called Green Leaf.
Their friend Alice, who works at the City Library, recommended a book written by Emma Watson.
Sometimes they travel to the countryside with Robert and Lisa to escape the noise of New York.
Although Mary grew up in Paris, she has always dreamed of visiting Dublin and Edinburgh.
They also have a cat named Oliver, who was adopted from the Happy Tails Animal Shelter.
"""
# NER解析
doc = nlp(text)
# 結果を表示
print("Named Entities:")
for ent in doc.ents:
print(f"{ent.text} [{ent.type}]")
Output例
Named Entities:
John [PERSON]
Mary [PERSON]
Central Park [LOC]
Green Leaf [LOC]
Alice [PERSON]
the City Library [ORG]
Emma Watson [PERSON]
Robert [PERSON]
Lisa [PERSON]
New York [GPE]
Mary [PERSON]
Paris [GPE]
Dublin [GPE]
Edinburgh [GPE]
Oliver [PERSON]
the Happy Tails Animal Shelter [ORG]