兼容的训练数据
请注意,您不能将原始文本直接输入 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(____))