長(zhǎng)治網(wǎng)站建設(shè)收費(fèi)多少2022最好的百度seo
筆者要做命名實(shí)體識(shí)別(NER)的工作,選擇了Doccano平臺(tái)來(lái)進(jìn)行文本標(biāo)注。
Doccano平臺(tái)對(duì)標(biāo)注結(jié)果的導(dǎo)出格式是JSONL格式,我們導(dǎo)出了NER.jsonl文件。
但是用python語(yǔ)言搭建深度學(xué)習(xí)模型來(lái)實(shí)現(xiàn)NER時(shí),一般接收的輸入數(shù)據(jù)格式為CoNLL 2003格式,需要將Doccano導(dǎo)出的JSONL數(shù)據(jù)轉(zhuǎn)換成CoNLL 2003格式。CoNLL 2003格式大概長(zhǎng)下面這樣,左邊是原文,右邊是標(biāo)簽:
剛開(kāi)始我還琢磨怎么變代碼做轉(zhuǎn)換,后來(lái)查到Doccano有官方的轉(zhuǎn)換工具:doccano-transformer,就是個(gè)python庫(kù),用起來(lái)很方便,下面是官方給出的使用代碼:
先在命令提示符里安裝:
pip install doccano-transformer
再用python語(yǔ)句來(lái)使用:
from doccano_transformer.datasets import NERDataset
from doccano_transformer.utils import read_jsonldataset = read_jsonl(filepath='example.jsonl', dataset=NERDataset, encoding='utf-8')
dataset.to_conll2003(tokenizer=str.split)
但是官方給的代碼不夠完整,沒(méi)有把結(jié)果轉(zhuǎn)成可以直接操作的txt文本,下面是我真正使用的代碼,增加了將轉(zhuǎn)換結(jié)果存儲(chǔ)成txt文件這一環(huán)節(jié):?
from doccano_transformer.datasets import NERDataset
from doccano_transformer.utils import read_jsonldataset = 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")
但卻報(bào)錯(cuò),提示:KeyError: 'The file should includes either "labels" or "annotations".':
?
在網(wǎng)上找了很久發(fā)現(xiàn)了解決辦法,需要兩步:
①將導(dǎo)出的jsonl文件里的“entities”標(biāo)簽轉(zhuǎn)換成“annotations”。
②將“doccano_transformer\examples.py”腳本中第29行的“doccano_transformer\examples.py”修改成“labels[0].append([”。(截圖中使用Notepad++打開(kāi)的examples.py腳本)
然后再按照我們之前的轉(zhuǎn)換代碼運(yùn)行就可以了:
from doccano_transformer.datasets import NERDataset
from doccano_transformer.utils import read_jsonldataset = 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")