【问题标题】:How do I convert simple training style data to spaCy's command line JSON format?如何将简单的训练风格数据转换为 spaCy 的命令行 JSON 格式?
【发布时间】:2018-02-21 22:41:44
【问题描述】:

我在 spaCy 文档的 "Training an additional entity type" 部分中有新 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')]
    })
]

我想使用 spacy command line application 在此数据上训练 NER 模型。这需要 spaCy 的 JSON format 中的数据。如何以这种 JSON 格式编写上述数据(即带有标记的字符偏移跨度的文本)?

查看该格式的文档后,我不清楚如何以这种格式手动写入数据。 (例如,我是否已将所有内容划分为段落?)还有一个convert 命令行实用程序,可将非spaCy 数据格式转换为spaCy 格式,但它不采用上述spaCy 格式作为输入.

我了解使用“简单训练样式”的 NER 训练代码示例,但我希望能够使用命令行实用程序进行训练。 (尽管从我的previous spaCy question 中可以看出,我不清楚你什么时候应该使用那种风格,什么时候应该使用命令行。)

谁能给我看一个“spaCy 的 JSON 格式”的上述数据的例子,或者指向解释如何进行这种转换的文档。

【问题讨论】:

    标签: spacy


    【解决方案1】:

    spaCy 有一个内置函数,可以帮助您完成大部分工作:

    from spacy.gold import biluo_tags_from_offsets
    

    这会接受您那里的“偏移”类型注释并将它们转换为逐个令牌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)
    

    确保在使用此数据进行训练之前禁用非 NER 管道。 我在仅 NER 数据上使用 spacy train 时遇到了一些问题。请参阅 #1907 并查看 Prodigy 论坛上的 this discussion,了解一些可能的解决方法。

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      相关资源
      最近更新 更多