開始使用免費開始

相容的訓練資料

記得你不能把原始文字直接餵給 spaCy。你需要為每個訓練樣本建立一個 Example 物件。這個練習要你把只含一個已標註句子的 training_data 轉換成 Example 物件的清單。

en_core_web_sm 模型已匯入並可用作 nlpExample 類別也已匯入,供你使用。

本練習屬於課程

使用 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(____))
編輯並執行程式碼