เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

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 ที่เป็นตัวพิมพ์เล็กกับ label PERSON
  • เพิ่ม component EntityRuler เข้าไปใน pipeline nlp แล้วเพิ่ม 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).____])
แก้ไขและรันโค้ด