spaCy の Matcher と PhraseMatcher

spaCyで学ぶNatural Language Processing

Azadeh Mobasher

Principal Data Scientist

spaCy の Matcher

 

  • 正規表現は複雑で、可読性・デバッグ性が低いことがある。
  • spaCy には、読みやすく本番向きの代替として Matcher クラスがある。

 

import spacy
from spacy.matcher import Matcher

nlp = spacy.load("en_core_web_sm") doc = nlp("Good morning, this is our first day on campus.")
matcher = Matcher(nlp.vocab)
spaCyで学ぶNatural Language Processing

spaCy の Matcher

 

  • 出力には、一致パターンの 開始終了 トークンインデックスが含まれる。
pattern = [{"LOWER": "good"}, {"LOWER": "morning"}]

matcher.add("morning_greeting", [pattern])
matches = matcher(doc) for match_id, start, end in matches: print("Start token: ", start, " | End token: ", end, "| Matched text: ", doc[start:end].text)
>>> Start token:  0  | End token:  2 | Matched text:  Good morning
spaCyで学ぶNatural Language Processing

Matcher の拡張構文サポート

 

  • パターン定義で演算子を使用可能。
  • Python の innot in、比較演算子に類似。

 

Attribute Value type Description
IN any type 属性値がリスト内のいずれか
NOT_IN any type 属性値がリスト内にない
==, >=, <=, >, < int, float 等価・不等比較を行う
spaCyで学ぶNatural Language Processing

Matcher の拡張構文サポート

  • IN 演算子で good morninggood evening の両方に一致
doc = nlp("Good morning and good evening.")
matcher = Matcher(nlp.vocab)
pattern = [{"LOWER": "good"}, {"LOWER": {"IN": ["morning", "evening"]}}]
matcher.add("morning_greeting", [pattern])
matches = matcher(doc)
  • IN 演算子を使ったマッチ結果
for match_id, start, end in matches:
    print("Start token: ", start, " | End token: ", end,
          "| Matched text: ", doc[start:end].text)
>>> Start token:  0  | End token:  2 | Matched text:  Good morning
Start token:  3  | End token:  5 | Matched text:  good evening
spaCyで学ぶNatural Language Processing

spaCy の PhraseMatcher

 

  • PhraseMatcher クラスは、与えられたテキストから多数のフレーズを照合する。

 

from spacy.matcher import PhraseMatcher
nlp = spacy.load("en_core_web_sm")
matcher = PhraseMatcher(nlp.vocab)
terms = ["Bill Gates", "John Smith"]
spaCyで学ぶNatural Language Processing

spaCy の PhraseMatcher

  • PhraseMatcher の出力には、一致パターンの 開始終了 のトークンインデックスが含まれる
patterns = [nlp.make_doc(term) for term in terms]
matcher.add("PeopleOfInterest", patterns)

doc = nlp("Bill Gates met John Smith for an important discussion regarding importance of AI.")
matches = matcher(doc) for match_id, start, end in matches: print("Start token: ", start, " | End token: ", end, "| Matched text: ", doc[start:end].text)
>>> Start token:  0  | End token:  2 | Matched text:  Bill Gates
Start token:  3  | End token:  5 | Matched text:  John Smith
spaCyで学ぶNatural Language Processing

spaCy の PhraseMatcher

  • PhraseMatcher クラスの attr 引数を利用できる
matcher = PhraseMatcher(nlp.vocab, attr = "LOWER")

terms = ["Government", "Investment"] patterns = [nlp.make_doc(term) for term in terms] matcher.add("InvestmentTerms", patterns) doc = nlp("It was interesting to the investment division of the government.")
matcher = PhraseMatcher(nlp.vocab, attr = "SHAPE")

terms = ["110.0.0.0", "101.243.0.0"] patterns = [nlp.make_doc(term) for term in terms] matcher.add("IPAddresses", patterns) doc = nlp("The tracked IP address was 234.135.0.0.")
spaCyで学ぶNatural Language Processing

Vamos praticar!

spaCyで学ぶNatural Language Processing

Preparing Video For Download...