这是经典的培训形式。
TRAIN_DATA = [
("Who is Shaka Khan?", {"entities": [(7, 17, "PERSON")]}),
("I like London and Berlin.", {"entities": [(7, 13, "LOC"), (18, 24, "LOC")]}),
]
Run Code Online (Sandbox Code Playgroud)
我曾经用代码训练,但据我所知,用 CLI 训练方法训练效果更好。但是,我的格式是这样的。
我已经找到了用于这种类型转换的代码片段,但它们中的每一个都在执行spacy.load('en')而不是空白 - 这让我想到,他们是在训练现有模型而不是空白吗?
这个块看起来很简单:
import spacy
from spacy.gold import docs_to_json
import srsly
nlp = spacy.load('en', disable=["ner"]) # as you see it's loading 'en' which I don't have
TRAIN_DATA = #data from above
docs = []
for text, annot in TRAIN_DATA:
doc = nlp(text)
doc.ents = [doc.char_span(start_idx, end_idx, label=label) for start_idx, end_idx, label in annot["entities"]]
docs.append(doc)
srsly.write_json("ent_train_data.json", [docs_to_json(docs)])
Run Code Online (Sandbox Code Playgroud)
运行此代码会抛出我:找不到模型“en”。它似乎不是快捷方式链接、Python 包或数据目录的有效路径。
我很困惑如何spacy train在空白上使用它。就用spacy.blank('en')?但是disable=["ner"]国旗呢?
编辑:
如果我尝试spacy.blank('en'),我会收到 Can't import language target from spacy.lang: No module named 'spacy.lang.en'
编辑 2:我试过加载en_core_web_sm
nlp = spacy.load('en_core_web_sm')
docs = []
for text, annot in TRAIN_DATA:
doc = nlp(text)
doc.ents = [doc.char_span(start_idx, end_idx, label=label) for start_idx, end_idx, label in annot["entities"]]
docs.append(doc)
srsly.write_json("ent_train_data.json", [docs_to_json(docs)])
Run Code Online (Sandbox Code Playgroud)
类型错误:“NoneType”类型的对象没有 len()
艾尔顿 -
print(text[start:end])目标!FK Qarabag 1, Partizani Tirana 0. Filip Ozobic - FK Qarabag - 从禁区中心向球门中心射门。助攻 - 艾尔顿 -
print(text)无-
doc.ents =...线类型错误:“NoneType”类型的对象没有 len()
编辑 3:来自 Ines 的评论
nlp = spacy.load('en_core_web_sm')
docs = []
for text, annot in TRAIN_DATA:
doc = nlp(text)
tags = biluo_tags_from_offsets(doc, annot['entities'])
docs.append(doc)
srsly.write_json(train_name + "_spacy_format.json", [docs_to_json(docs)])
Run Code Online (Sandbox Code Playgroud)
这创建了 json,但我在生成的 json 中没有看到我的任何标记实体。
编辑 3 已接近尾声,但您缺少将实体添加到文档的步骤。这应该有效:
import spacy
import srsly
from spacy.gold import docs_to_json, biluo_tags_from_offsets, spans_from_biluo_tags
TRAIN_DATA = [
("Who is Shaka Khan?", {"entities": [(7, 17, "PERSON")]}),
("I like London and Berlin.", {"entities": [(7, 13, "LOC"), (18, 24, "LOC")]}),
]
nlp = spacy.load('en_core_web_sm')
docs = []
for text, annot in TRAIN_DATA:
doc = nlp(text)
tags = biluo_tags_from_offsets(doc, annot['entities'])
entities = spans_from_biluo_tags(doc, tags)
doc.ents = entities
docs.append(doc)
srsly.write_json("spacy_format.json", [docs_to_json(docs)])
Run Code Online (Sandbox Code Playgroud)
添加一个内置函数来进行这种转换会很好,因为想要从示例脚本(这只是简单的演示)转移到训练 CLI 是很常见的。
编辑:
您还可以跳过对内置 BILUO 转换器的间接使用,并使用上面的内容:
doc.ents = [doc.char_span(start_idx, end_idx, label=label) for start_idx, end_idx, label in annot["entities"]]
Run Code Online (Sandbox Code Playgroud)
import spacy
import srsly
from spacy.training import docs_to_json, offsets_to_biluo_tags, biluo_tags_to_spans
TRAIN_DATA = [
("Who is Shaka Khan?", {"entities": [(7, 17, "PERSON")]}),
("I like London and Berlin.", {"entities": [(7, 13, "LOC"), (18, 24, "LOC")]}),
]
nlp = spacy.load('en_core_web_lg')
docs = []
for text, annot in training_sub:
doc = nlp(text)
tags = offsets_to_biluo_tags(doc, annot['entities'])
entities = biluo_tags_to_spans(doc, tags)
doc.ents = entities
docs.append(doc)
srsly.write_json("spacy_format.json", [docs_to_json(docs)])
Run Code Online (Sandbox Code Playgroud)
从 spaCy v3.1 开始,上述代码可以运行。一些相关方法已spacy.gold被重命名并迁移到spacy.training.