spaCy の言語特徴

spaCyで学ぶNatural Language Processing

Azadeh Mobasher

Principal Data Scientist

品詞タグ付け(POS)

  • 文中での機能と文脈に基づき、語を文法上で分類
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で学ぶNatural Language Processing

spaCy による POS タグ付け

 

  • POS タグ付けは語の意味を確定します
  • watch のように名詞にも動詞にもなる語があります
  • spaCypos_ 特徴量に POS を保持します
  • spacy.explain() で POS タグの説明を取得

POS タガー コンポーネント

spaCyで学ぶNatural Language Processing

spaCy による 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')]
spaCyで学ぶNatural Language Processing

固有表現認識(NER)

  • 固有表現は、固有の名称で特定の実体を指す語や句
  • 固有表現抽出(NER)は、事前定義のカテゴリに分類します
エンティティ型 説明
PERSON 個人名・家族名
ORG 企業・団体など
GPE 地政学的実体(国・都市など)
LOC 非 GPE の場所(山脈など)
DATE 日付・期間(絶対/相対)
TIME 1 日未満の時刻
spaCyで学ぶNatural Language Processing

NER と spaCy

 

  • spaCy モデルは NER パイプラインで固有表現を抽出します
  • 固有表現は doc.ents で取得できます
  • 各表現にはラベル(.label_)が付きます

NER コンポーネント

spaCyで学ぶNatural Language Processing

NER と spaCy

 

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')]
spaCyで学ぶNatural Language Processing

NER と spaCy

  • 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で学ぶNatural Language Processing

displaCy

 

  • 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")

displaCy NER の出力

spaCyで学ぶNatural Language Processing

演習に進みましょう

spaCyで学ぶNatural Language Processing

Preparing Video For Download...