互換性のある学習データ
生テキストをそのまま spaCy に渡すことはできません。代わりに、各学習例ごとに Example オブジェクトを作成する必要があります。この演習では、1文のみがアノテーションされた training_data を、Example オブジェクトのリストに変換する練習をします。
en_core_web_sm モデルはすでに読み込まれており、nlp として使用できます。Example クラスもインポート済みです。
この演習はコースの一部です
spaCyで学ぶNatural Language Processing
演習の手順
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(____))