EntityRuler กับหลาย pattern ใน spaCy
EntityRuler ช่วยให้เพิ่ม entity เข้าไปใน doc.ents และยังช่วยเพิ่มประสิทธิภาพการจดจำ named entity ได้อีกด้วย ในแบบฝึกหัดนี้ จะได้ฝึกเพิ่ม component EntityRuler เข้าไปใน pipeline nlp ที่มีอยู่แล้ว เพื่อให้แน่ใจว่า entity หลายรายการได้รับการจำแนกประเภทอย่างถูกต้อง
โมเดล en_core_web_sm โหลดไว้แล้วและพร้อมใช้งานในชื่อ nlp สามารถเข้าถึงข้อความตัวอย่างได้ที่ example_text และใช้ nlp กับ doc เพื่อเข้าถึงโมเดล spaCy และ container Doc ของ example_text ตามลำดับ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การประมวลผลภาษาธรรมชาติด้วย spaCy
คำแนะนำการฝึกหัด
- แสดงรายการ tuple ของข้อความและประเภท entity ใน
example_textโดยใช้โมเดลnlp - กำหนด pattern หลายรายการเพื่อจับคู่คำว่า
brotherและsistersที่เป็นตัวพิมพ์เล็กกับ labelPERSON - เพิ่ม component
EntityRulerเข้าไปใน pipelinenlpแล้วเพิ่มpatternsเข้าไปในEntityRuler - แสดง tuple ของข้อความและประเภท entity สำหรับ
example_textโดยใช้โมเดลnlp
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
nlp = spacy.load("en_core_web_md")
# Print a list of tuples of entities text and types in the example_text
print("Before EntityRuler: ", [____ for ____ in nlp(____).____], "\n")
# Define pattern to add a label PERSON for lower cased sisters and brother entities
patterns = [{"label": ____, "pattern": [{"lower": ____}]},
{"label": ____, "pattern": [{"lower": ____}]}]
# Add an EntityRuler component and add the patterns to the ruler
ruler = nlp.____("entity_ruler")
ruler.____(____)
# Print a list of tuples of entities text and types
print("After EntityRuler: ", [(ent.____, ent.____) for ent in nlp(example_text).____])