相容的訓練資料
記得你不能把原始文字直接餵給 spaCy。你需要為每個訓練樣本建立一個 Example 物件。這個練習要你把只含一個已標註句子的 training_data 轉換成 Example 物件的清單。
en_core_web_sm 模型已匯入並可用作 nlp。Example 類別也已匯入,供你使用。
本練習屬於課程
使用 spaCy 的自然語言處理
練習說明
- 逐一走訪
training_data中的文字與標註,將文字轉成Doc容器並存成doc。 - 使用
doc物件與各筆訓練資料的標註建立一個Example物件,並存成example_sentence。 - 將
example_sentence加到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(____))