spaCyで学ぶNatural Language Processing
Azadeh Mobasher
Principal Data Scientist
| POS | 説明 | 例 |
|---|---|---|
| VERB | 動詞 | run, eat, ate, take |
| NOUN | 名詞 | man, airplane, tree, flower |
| ADJ | 形容詞 | big, old, incompatible, conflicting |
| ADV | 副詞 | very, down, there, tomorrow |
| CONJ | 接続詞 | and, or, but |
spaCy は pos_ 特徴量に POS を保持しますspacy.explain() で POS タグの説明を取得verb_sent = "I watch TV."print([(token.text, token.pos_, spacy.explain(token.pos_)) for token in nlp(verb_sent)])
[('I', 'PRON', 'pronoun'),
('watch', 'VERB', 'verb'),
('TV', 'NOUN', 'noun'),
('.', 'PUNCT', 'punctuation')]
noun_sent = "I left without my watch."print([(token.text, token.pos_, spacy.explain(token.pos_)) for token in nlp(noun_sent)])
[('I', 'PRON', 'pronoun'),
('left', 'VERB', 'verb'),
('without', 'ADP', 'adposition'),
('my', 'PRON', 'pronoun'),
('watch', 'NOUN', 'noun'),
('.', 'PUNCT', 'punctuation')]
| エンティティ型 | 説明 |
|---|---|
| PERSON | 個人名・家族名 |
| ORG | 企業・団体など |
| GPE | 地政学的実体(国・都市など) |
| LOC | 非 GPE の場所(山脈など) |
| DATE | 日付・期間(絶対/相対) |
| TIME | 1 日未満の時刻 |
spaCy モデルは NER パイプラインで固有表現を抽出しますdoc.ents で取得できます.label_)が付きます
import spacy nlp = spacy.load("en_core_web_sm") text = "Albert Einstein was genius." doc = nlp(text)print([(ent.text, ent.start_char, ent.end_char, ent.label_) for ent in doc.ents])
>>> [('Albert Einstein', 0, 15, 'PERSON')]
Doc 内の各トークンの表現タイプにもアクセスできます
import spacy nlp = spacy.load("en_core_web_sm") text = "Albert Einstein was genius." doc = nlp(text)print([(token.text, token.ent_type_) for token in doc])
>>> [('Albert', 'PERSON'), ('Einstein', 'PERSON'),
('was', ''), ('genius', ''), ('.', '')]
spaCy には可視化ツール displaCy がありますdisplaCy の entity ビジュアライザは固有表現とラベルを強調表示しますimport spacy from spacy import displacy text = "Albert Einstein was genius." nlp = spacy.load("en_core_web_sm") doc = nlp(text)displacy.serve(doc, style="ent")
spaCyで学ぶNatural Language Processing