spaCy EntityRuler

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

Azadeh Mobasher

Principal Data Scientist

spaCy EntityRuler

 

  • EntityRuler เพิ่ม named entity ลงใน Doc container
  • ใช้แบบอิสระหรือร่วมกับ EntityRecognizer ได้
  • Phrase entity patterns สำหรับจับคู่สตริงแบบตรงทั้งหมด (string):
{"label": "ORG", "pattern": "Microsoft"}
  • Token entity patterns โดยใช้ dictionary หนึ่งตัวต่อหนึ่ง token (list):
{"label": "GPE", "pattern": [{"LOWER": "san"}, {"LOWER": "francisco"}]}
การประมวลผลภาษาธรรมชาติด้วย spaCy

การเพิ่ม EntityRuler ลงใน spaCy pipeline

 

  • เพิ่มด้วยเมธอด .add_pipe()
  • เพิ่มรายการ pattern ได้ด้วยเมธอด .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)
การประมวลผลภาษาธรรมชาติด้วย spaCy

การเพิ่ม EntityRuler ลงใน spaCy pipeline

 

  • .ents เก็บผลลัพธ์จาก component 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')]
การประมวลผลภาษาธรรมชาติด้วย spaCy

EntityRuler ในการปฏิบัติจริง

 

  • ทำงานร่วมกับ component ต่าง ๆ ใน spaCy pipeline
  • ช่วยเสริมประสิทธิภาพของ named-entity recognizer

  • โมเดล 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')]
การประมวลผลภาษาธรรมชาติด้วย spaCy

EntityRuler ในการปฏิบัติจริง

 

  • เพิ่ม EntityRuler หลัง component 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')]
การประมวลผลภาษาธรรมชาติด้วย spaCy

EntityRuler ในการปฏิบัติจริง

 

  • เพิ่ม EntityRuler ก่อน component 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')]
การประมวลผลภาษาธรรมชาติด้วย spaCy

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

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

Preparing Video For Download...