spaCy Matcher și PhraseMatcher

Procesarea limbajului natural cu spaCy

Azadeh Mobasher

Principal Data Scientist

Matcher în spaCy

 

  • Tiparele RegEx pot fi complexe, greu de citit și de depanat.
  • spaCy oferă o alternativă lizibilă, de nivel producție: clasa 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)
Procesarea limbajului natural cu spaCy

Matcher în spaCy

 

  • Rezultatele potrivirii includ indicii start și end ai tokenurilor potrivite.
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
Procesarea limbajului natural cu spaCy

Sintaxă extinsă Matcher

 

  • Permite utilizarea operatorilor la definirea tiparelor de potrivire.
  • Operatori similari cu in, not in și operatorii de comparație din Python

 

Atribut Tip valoare Descriere
IN orice tip Valoarea atributului este membră a unei liste
NOT_IN orice tip Valoarea atributului _nu_ este membră a unei liste
==, >=, <=, >, < int, float Operatori de comparație pentru egalitate sau inegalitate
Procesarea limbajului natural cu spaCy

Sintaxă extinsă Matcher

  • Utilizarea operatorului IN pentru a potrivi atât good morning, cât și 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)
  • Rezultatul potrivirii cu operatorul 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
Procesarea limbajului natural cu spaCy

PhraseMatcher în spaCy

 

  • Clasa PhraseMatcher potrivește o listă extinsă de fraze într-un text dat.

 

from spacy.matcher import PhraseMatcher
nlp = spacy.load("en_core_web_sm")
matcher = PhraseMatcher(nlp.vocab)
terms = ["Bill Gates", "John Smith"]
Procesarea limbajului natural cu spaCy

PhraseMatcher în spaCy

  • Rezultatele PhraseMatcher includ indicii start și end ai tokenurilor potrivite
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
Procesarea limbajului natural cu spaCy

PhraseMatcher în spaCy

  • Putem utiliza argumentul attr al clasei 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.")
Procesarea limbajului natural cu spaCy

Să exersăm!

Procesarea limbajului natural cu spaCy

Preparing Video For Download...