spaCy Matcher และ PhraseMatcher

การประมวลผลภาษาธรรมชาติด้วย spaCy

Azadeh Mobasher

Principal Data Scientist

Matcher ใน spaCy

 

  • รูปแบบ RegEx มักซับซ้อน อ่านและดีบักได้ยาก
  • spaCy มีทางเลือกที่อ่านง่ายและใช้งานในระดับ production ได้ คือคลาส 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

Matcher ใน spaCy

 

  • ผลลัพธ์ของการจับคู่จะมี index โทเคน start และ end ของรูปแบบที่ตรงกัน
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

Matcher รองรับ syntax ขั้นสูง

 

  • รองรับตัวดำเนินการในการกำหนดรูปแบบการจับคู่
  • คล้ายกับตัวดำเนินการ in, not in และตัวดำเนินการเปรียบเทียบของ Python

 

Attribute Value type Description
IN any type ค่า attribute เป็นสมาชิกของ list
NOT_IN any type ค่า attribute ไม่ใช่ สมาชิกของ list
==, >=, <=, >, < int, float ตัวดำเนินการเปรียบเทียบสำหรับตรวจสอบความเท่าเทียมหรือไม่เท่าเทียม
การประมวลผลภาษาธรรมชาติด้วย spaCy

Matcher รองรับ syntax ขั้นสูง

  • ใช้ตัวดำเนินการ IN เพื่อจับคู่ทั้ง good morning และ good 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

PhraseMatcher ใน spaCy

 

  • คลาส PhraseMatcher ใช้จับคู่วลีจำนวนมากในข้อความที่กำหนด

 

from spacy.matcher import PhraseMatcher
nlp = spacy.load("en_core_web_sm")
matcher = PhraseMatcher(nlp.vocab)
terms = ["Bill Gates", "John Smith"]
การประมวลผลภาษาธรรมชาติด้วย spaCy

PhraseMatcher ใน spaCy

  • ผลลัพธ์ของ PhraseMatcher จะมี index โทเคน start และ end ของรูปแบบที่ตรงกัน
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

PhraseMatcher ใน spaCy

  • สามารถใช้อาร์กิวเมนต์ attr ของคลาส PhraseMatcher ได้
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

มาฝึกกันเถอะ!

การประมวลผลภาษาธรรมชาติด้วย spaCy

Preparing Video For Download...