Matcher e PhraseMatcher do spaCy

Processamento de Linguagem Natural com spaCy

Azadeh Mobasher

Principal Data Scientist

Matcher no spaCy

 

  • Padrões de RegEx podem ser complexos, difíceis de ler e depurar.
  • O spaCy oferece uma alternativa legível e pronta para produção: a 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)
Processamento de Linguagem Natural com spaCy

Matcher no spaCy

 

  • A saída inclui os índices de tokens de início e fim do padrão encontrado.
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
Processamento de Linguagem Natural com spaCy

Suporte a sintaxe estendida do Matcher

 

  • Permite operadores ao definir padrões de correspondência.
  • Operadores semelhantes aos in, not in e comparações do Python

 

Atributo Tipo de valor Descrição
IN qualquer tipo Valor do atributo é membro de uma lista
NOT_IN qualquer tipo Valor do atributo não é membro de uma lista
==, >=, <=, >, < int, float Operadores de comparação para testar igualdade ou desigualdade
Processamento de Linguagem Natural com spaCy

Suporte a sintaxe estendida do Matcher

  • Usando o operador IN para corresponder good morning e 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)
  • Saída da correspondência com o operador 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
Processamento de Linguagem Natural com spaCy

PhraseMatcher no spaCy

 

  • A classe PhraseMatcher encontra uma longa lista de expressões em um texto.

 

from spacy.matcher import PhraseMatcher
nlp = spacy.load("en_core_web_sm")
matcher = PhraseMatcher(nlp.vocab)
terms = ["Bill Gates", "John Smith"]
Processamento de Linguagem Natural com spaCy

PhraseMatcher no spaCy

  • A saída do PhraseMatcher inclui os índices de tokens de início e fim do padrão encontrado
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
Processamento de Linguagem Natural com spaCy

PhraseMatcher no spaCy

  • Podemos usar o argumento attr da 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.")
Processamento de Linguagem Natural com spaCy

Vamos praticar!

Processamento de Linguagem Natural com spaCy

Preparing Video For Download...