我在spaCy文档的“训练其他实体类型”部分中有关于新NER类型的训练数据。
TRAIN_DATA = [
("Horses are too tall and they pretend to care about your feelings", {
'entities': [(0, 6, 'ANIMAL')]
}),
("Do they bite?", {
'entities': []
}),
("horses are too tall and they pretend to care about your feelings", {
'entities': [(0, 6, 'ANIMAL')]
}),
("horses pretend to care about your feelings", {
'entities': [(0, 6, 'ANIMAL')]
}),
("they pretend to care about your feelings, those horses", {
'entities': [(48, 54, 'ANIMAL')]
}),
("horses?", {
'entities': [(0, 6, 'ANIMAL')]
})
]
Run Code Online (Sandbox Code Playgroud)
我想使用spacy 命令行应用程序对此数据训练一个NER模型。这需要spaCy的JSON格式的数据。如何以JSON格式写入上述数据(即带有标记字符偏移范围的文本)?
在查看了该格式的文档之后,我不清楚如何手动以这种格式写入数据。(例如,我是否将所有内容都划分为段落?)还有一个convert命令行实用程序,可以将非spaCy数据格式转换为spaCy的格式,但是并不需要像上面那样的spaCy格式作为输入。
我了解使用“简单培训风格”的NER培训代码的示例,但是我希望能够使用命令行实用程序进行培训。(尽管从我之前的spaCy问题可以明显看出,我不清楚何时应使用该样式以及何时应使用命令行。)
有人可以用“ spaCy的JSON格式”向我展示上述数据的示例,或指向说明如何进行此转换的文档。
小智 6
有一个内置的函数spaCy可以帮助您实现大部分目标:
from spacy.gold import biluo_tags_from_offsets
Run Code Online (Sandbox Code Playgroud)
这将采用您拥有的“偏移”类型注释,并将其转换为逐令牌BILOU格式。
要将NER注释放入最终的训练JSON格式中,您只需要在它们周围包裹一些内容即可填充数据所需的其他位置:
sentences = []
for t in TRAIN_DATA:
doc = nlp(t[0])
tags = biluo_tags_from_offsets(doc, t[1]['entities'])
ner_info = list(zip(doc, tags))
tokens = []
for n, i in enumerate(ner_info):
token = {"head" : 0,
"dep" : "",
"tag" : "",
"orth" : i[0].string,
"ner" : i[1],
"id" : n}
tokens.append(token)
sentences.append(tokens)
Run Code Online (Sandbox Code Playgroud)
在训练此数据之前,请确保禁用非NER管道。我在使用spacy train仅限NER的数据时遇到了一些问题。请参阅#1907,也可以在Prodigy论坛上查看此讨论,以获取一些可能的解决方法。