开始使用免费开始使用

兼容的训练数据

请注意,您不能将原始文本直接输入 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(____))
编辑并运行代码