spaCy EntityRuler

Обробка природної мови (NLP) зі spaCy

Azadeh Mobasher

Principal Data Scientist

spaCy EntityRuler

 

  • EntityRuler додає іменовані сутності до контейнера Doc
  • Його можна використовувати окремо або разом з EntityRecognizer
  • Шаблони сутностей за фразою для точних збігів рядка (string):
{"label": "ORG", "pattern": "Microsoft"}
  • Шаблони сутностей за токенами: один словник описує один токен (list):
{"label": "GPE", "pattern": [{"LOWER": "san"}, {"LOWER": "francisco"}]}
Обробка природної мови (NLP) зі spaCy

Додавання EntityRuler до конвеєра spaCy

 

  • Використання методу .add_pipe()
  • Список шаблонів додається методом .add_patterns()

 

nlp = spacy.blank("en")
entity_ruler = nlp.add_pipe("entity_ruler")
patterns = [{"label": "ORG", "pattern": "Microsoft"},
            {"label": "GPE", "pattern": [{"LOWER": "san"}, {"LOWER": "francisco"}]}]
entity_ruler.add_patterns(patterns)
Обробка природної мови (NLP) зі spaCy

Додавання EntityRuler до конвеєра spaCy

 

  • .ents зберігає результати компонента EntityLinker

 

doc = nlp("Microsoft is hiring software developer in San Francisco.")
print([(ent.text, ent.label_) for ent in doc.ents])
[("Microsoft", "ORG"), ("San Francisco", "GPE")]
Обробка природної мови (NLP) зі spaCy

EntityRuler у дії

 

  • Інтегрується з компонентами конвеєра spaCy
  • Підсилює розпізнавач іменованих сутностей

  • Модель spaCy без EntityRuler:

nlp = spacy.load("en_core_web_sm")

doc = nlp("Manhattan associates is a company in the U.S.")
print([(ent.text, ent.label_) for ent in doc.ents])
>>> [("Manhattan", "GPE"), ("U.S.", "GPE")]
Обробка природної мови (NLP) зі spaCy

EntityRuler у дії

 

  • EntityRuler додано після наявного компонента ner:
nlp = spacy.load("en_core_web_sm")
ruler = nlp.add_pipe("entity_ruler", after='ner')
patterns = [{"label": "ORG", "pattern": [{"lower": "manhattan"}, {"lower": "associates"}]}]
ruler.add_patterns(patterns)

doc = nlp("Manhattan associates is a company in the U.S.")
print([(ent.text, ent.label_) for ent in doc.ents])
>>> [("Manhattan", "GPE"), ("U.S.", "GPE")]
Обробка природної мови (NLP) зі spaCy

EntityRuler у дії

 

  • EntityRuler додано перед наявним компонентом ner:
nlp = spacy.load("en_core_web_sm")
ruler = nlp.add_pipe("entity_ruler", before='ner')
patterns = [{"label": "ORG", "pattern": [{"lower": "manhattan"}, {"lower": "associates"}]}]
ruler.add_patterns(patterns)

doc = nlp("Manhattan associates is a company in the U.S.")
print([(ent.text, ent.label_) for ent in doc.ents])
>>> [("Manhattan associates", "ORG"), ("U.S.", "GPE")]
Обробка природної мови (NLP) зі spaCy

Давайте потренуємось!

Обробка природної мови (NLP) зі spaCy

Preparing Video For Download...