spaCy Matcher e PhraseMatcher

Natural Language Processing con spaCy

Azadeh Mobasher

Principal Data Scientist

Matcher in spaCy

 

  • I pattern RegEx possono essere complessi, difficili da leggere e fare debug.
  • spaCy offre un'alternativa leggibile e pronta per la produzione: la classe 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)
Natural Language Processing con spaCy

Matcher in spaCy

 

  • L'output del matching include gli indici di token start e end del pattern trovato.
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
Natural Language Processing con spaCy

Supporto sintassi estesa del Matcher

 

  • Consente operatori nella definizione dei pattern di matching.
  • Operatori simili a in, not in e agli operatori di confronto di Python

 

Attributo Tipo di valore Descrizione
IN qualsiasi tipo Il valore dell'attributo è un membro di una lista
NOT_IN qualsiasi tipo Il valore dell'attributo non è un membro di una lista
==, >=, <=, >, < int, float Operatori di confronto per verifiche di uguaglianza o disuguaglianza
Natural Language Processing con spaCy

Supporto sintassi estesa del Matcher

  • Uso dell'operatore IN per trovare sia good morning sia 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)
  • Output del matching con l'operatore 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
Natural Language Processing con spaCy

PhraseMatcher in spaCy

 

  • La classe PhraseMatcher trova un lungo elenco di frasi in un testo dato.

 

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

PhraseMatcher in spaCy

  • L'output di PhraseMatcher include gli indici di token start e end del pattern trovato
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
Natural Language Processing con spaCy

PhraseMatcher in spaCy

  • Possiamo usare l'argomento attr della classe 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.")
Natural Language Processing con spaCy

Esercitiamoci!

Natural Language Processing con spaCy

Preparing Video For Download...