企业推广宣传文案,谷歌seo关键词排名优化,电子商务市场营销,上海网站建设找哪家笔者要做命名实体识别#xff08;NER#xff09;的工作#xff0c;选择了Doccano平台来进行文本标注。 Doccano平台对标注结果的导出格式是JSONL格式#xff0c;我们导出了NER.jsonl文件。 但是用python语言搭建深度学习模型来实现NER时#xff0c;一般接收的输入数据格式…笔者要做命名实体识别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_jsonldataset read_jsonl(filepathexample.jsonl, datasetNERDataset, encodingutf-8)
dataset.to_conll2003(tokenizerstr.split) 但是官方给的代码不够完整没有把结果转成可以直接操作的txt文本下面是我真正使用的代码增加了将转换结果存储成txt文件这一环节
from doccano_transformer.datasets import NERDataset
from doccano_transformer.utils import read_jsonldataset read_jsonl(filepathNER.jsonl, datasetNERDataset, encodingutf-8)
gendataset.to_conll2003(tokenizerstr.split)file_nameCoNLL.txtwith 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_jsonldataset read_jsonl(filepathNER.jsonl, datasetNERDataset, encodingutf-8)
gendataset.to_conll2003(tokenizerstr.split)file_nameCoNLL.txtwith open(file_name, w, encoding utf-8) as file:for item in gen:file.write(item[data] \n)