ข้อมูลการฝึกในรูปแบบที่ถูกต้อง
โปรดทราบว่าไม่สามารถส่งข้อความดิบให้ spaCy โดยตรงได้ แต่ต้องสร้างออบเจ็กต์ Example สำหรับตัวอย่างการฝึกแต่ละรายการ ในแบบฝึกหัดนี้ จะได้ฝึกแปลง training_data ที่มีประโยคที่มี annotation อยู่หนึ่งประโยคให้เป็น list ของออบเจ็กต์ Example
โมเดล en_core_web_sm ถูก import ไว้แล้วและพร้อมใช้งานในชื่อ nlp และคลาส Example ก็ถูก import ไว้ให้แล้วเช่นกัน
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การประมวลผลภาษาธรรมชาติด้วย spaCy
คำแนะนำการฝึกหัด
- วนซ้ำผ่านข้อความและ annotations ใน
training_dataจากนั้นแปลงข้อความให้เป็นDoccontainer และเก็บไว้ในตัวแปรdoc - สร้างออบเจ็กต์
Exampleโดยใช้ออบเจ็กต์docและ annotations ของข้อมูลการฝึกแต่ละจุด แล้วเก็บไว้ในตัวแปรexample_sentence - เพิ่ม
example_sentenceเข้าไปใน list ชื่อall_examples
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
example_text = 'A patient with chest pain had hyperthyroidism.'
training_data = [(example_text, {'entities': [(15, 25, 'SYMPTOM'), (30, 45, 'DISEASE')]})]
all_examples = []
# Iterate through text and annotations and convert text to a Doc container
for text, annotations in training_data:
doc = nlp(____)
# Create an Example object from the doc contianer and annotations
example_sentence = ____.____(doc, ____)
print(example_sentence.to_dict(), "\n")
# Append the Example object to the list of all examples
all_examples.append(____)
print("Number of formatted training data: ", len(____))