【已解决】关于如何将Doccano标注的文本转换成NER模型可以直接处理的CoNLL 2003格式

笔者要做命名实体识别(NER)的工作,选择了Doccano平台来进行文本标注。

Doccano平台对标注结果的导出格式是JSONL格式,我们导出了NER.jsonl文件。

但是用python语言搭建深度学习模型来实现NER时,一般接收的输入数据格式为CoNLL 2003格式,需要将Doccano导出的JSONL数据转换成CoNLL 2003格式。CoNLL 2003格式大概长下面这样,左边是原文,右边是标签:

刚开始我还琢磨怎么变代码做转换,后来查到Doccano有官方的转换工具:doccano-transformer,就是个python库,用起来很方便,下面是官方给出的使用代码:

先在命令提示符里安装:

pip install doccano-transformer

再用python语句来使用:

from doccano_transformer.datasets import NERDataset
from doccano_transformer.utils import read_jsonl

dataset = read_jsonl(filepath='example.jsonl', dataset=NERDataset, encoding='utf-8')
dataset.to_conll2003(tokenizer=str.split)

但是官方给的代码不够完整,没有把结果转成可以直接操作的txt文本,下面是我真正使用的代码,增加了将转换结果存储成txt文件这一环节: 

from doccano_transformer.datasets import NERDataset
from doccano_transformer.utils import read_jsonl

dataset = read_jsonl(filepath='NER.jsonl', dataset=NERDataset, encoding='utf-8')
gen=dataset.to_conll2003(tokenizer=str.split)

file_name="CoNLL.txt"

with open(file_name, "w", encoding = "utf-8") as file:
    for item in gen:
        file.write(item["data"] + "\n")

但却报错,提示:KeyError: 'The file should includes either "labels" or "annotations".':

在网上找了很久发现了解决办法,需要两步:

①将导出的jsonl文件里的“entities”标签转换成“annotations”。

②将“doccano_transformer\examples.py”脚本中第29行的“doccano_transformer\examples.py”修改成“labels[0].append([”。(截图中使用Notepad++打开的examples.py脚本)

然后再按照我们之前的转换代码运行就可以了:

from doccano_transformer.datasets import NERDataset
from doccano_transformer.utils import read_jsonl

dataset = read_jsonl(filepath='NER.jsonl', dataset=NERDataset, encoding='utf-8')
gen=dataset.to_conll2003(tokenizer=str.split)

file_name="CoNLL.txt"

with open(file_name, "w", encoding = "utf-8") as file:
    for item in gen:
        file.write(item["data"] + "\n")

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值