繁体   English   中英

将 Spacy 训练数据格式转换为 Spacy CLI 格式(用于空白 NER)

[英]Converting Spacy Training Data format to Spacy CLI Format (for blank NER)

这是经典的培训形式。

TRAIN_DATA = [
    ("Who is Shaka Khan?", {"entities": [(7, 17, "PERSON")]}),
    ("I like London and Berlin.", {"entities": [(7, 13, "LOC"), (18, 24, "LOC")]}),
]

我曾经使用代码进行训练,但据我了解,使用 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)])

运行此代码会抛出我:找不到模型“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)])

类型错误:“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)])

这创建了 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)])

添加一个内置函数来进行这种转换会很好,因为想要从示例脚本(这只是简单的演示)转移到训练 CLI 是很常见的。

编辑

您还可以跳过对内置 BILUO 转换器的间接使用,并使用上面的内容:

    doc.ents = [doc.char_span(start_idx, end_idx, label=label) for start_idx, end_idx, label in annot["entities"]]
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)])

从 spaCy v3.1 开始,上面的代码有效。 spacy.gold一些相关方法已重命名并迁移到spacy.training

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM