当前位置:网站首页>AI中台序列标注任务:三个数据集构造过程记录
AI中台序列标注任务:三个数据集构造过程记录
2022-08-03 07:30:00 【木尧大兄弟】
数据介绍
人民日报命名实体识别数据集(example.train 28046条数据和example.test 4636条数据),共3种标签:地点(LOC), 人名(PER), 组织机构(ORG)
时间识别数据集(time.train 1700条数据和time.test 300条数据),共1种标签:TIME
CLUENER细粒度实体识别数据集(cluener.train 10748条数据和cluener.test 1343条数据),共10种标签:地址(address),书名(book),公司(company),游戏(game),政府(goverment),电影(movie),姓名(name),组织机构(organization),职位(position),景点(scene)
数据来源:GitHub
格式说明
原始格式:如图,每个字和label占一行,每句用空行隔开;
目标格式:每句一行,前面是用空格分开的字符 \t 后面是用空格分开的label,并生成一个label_map.json文件,如图(参考EasyDL)。
代码实现
数据转换函数代码:
import os
def dataFucker(filepath):
new_filepath = filepath+'_new.txt'
f_new = open(new_filepath, 'w+', encoding="utf-8")
with open(filepath, 'r', encoding="utf-8") as f:
lines = f.read().split('\n\n')
for line in lines:
if not line:
continue
tokens_words = line.split('\n')
tokens, labels = [_.split(' ')[0].strip() for _ in tokens_words if _], [_.split(' ')[1].strip() for _ in tokens_words if _]
assert len(tokens) == len(labels)
new_line = ' '.join(tokens) + '\t' + ' '.join(labels) + '\n'
f_new.write(new_line)
f_new.close()
return new_filepath
数据转换入口函数:
import os
path = r"C:\xxx\data"
datasets = os.listdir(path)
for dataset in datasets:
filepath = os.path.join(path, dataset)
new_filepath = dataFucker(filepath)
print(filepath.split("\\")[-1], '---->', new_filepath.split("\\")[-1])
执行结果:
生成映射json文件代码:
import os
import json
def mapJsonFucker(filepath):
with open(filepath, 'r', encoding="utf-8") as f:
lines = f.readlines()
labels = []
for line in lines:
_labels = line.split('\t')[1].split(' ')
for each in _labels:
if each.strip() not in labels:
labels.append(each.strip())
labels.remove('O')
labels.append('O')
res = json.dumps({
key:value for value, key in enumerate(labels)}, indent=2)
with open(filepath+'_label_map.json', 'w+', encoding='utf-8') as fd:
fd.write(res)
return res
生成映射json文件入口函数:
path = r"C:\xxx\data"
datasets = [_ for _ in os.listdir(path) if 'txt' in _ and 'train' in _]
print(dataset)
for dataset in datasets:
filepath = os.path.join(path, dataset)
print(filepath)
resList = mapJsonFucker(filepath)
print(resList)
执行结果:
后续就可以根据自己需求处理了。
边栏推荐
- Windows安装MySQL(MIS)
- 训练正常&异常的GAN损失函数loss变化应该是怎么样的
- 【图像去噪】基于matlab稀疏表示KSVD图像去噪【含Matlab源码 2016期】
- 戳Web3的神话?戳到铁板。
- 华为设备配置BFD与接口联动(触发与BFD联动的接口物理状态变为Down)
- 千万级别的表分页查询非常慢,怎么办?
- 22-08-02 西安 尚医通(02)Vscode、ES6、nodejs、npm、Bable转码器
- Data warehouse buried point system and attribution practice
- 品牌方发行NFT时,应如何考量实用性?
- 安全狗云原生安全能力全面亮相全球数字经济大会暨ISC互联网安全大会
猜你喜欢
随机推荐
volta管理node版本
依赖注入(DI),自动配置,集合注入
Fortify白盒神器20.1.1下载及安装(非百度网盘)
学习笔记:机器学习之逻辑回归
consul理解
Postman will return to the interface to generate a json file to the local
请求与响应:响应
【多线程进阶】--- 常见锁策略,CAS,synchronized底层工作原理,JUC,线程安全的集合类,死锁
C语言实现树的底层遍历--超简代码
熊市中预言机,牛市中的战斗机,藏宝计划起飞,坐稳扶好!
pyspark @udf loop using variable problem
剑指offer专项突击版第18天
9月考,如何选择靠谱正规的培训机构?
[机缘参悟-59]:《素书》-6-安于礼仪[安礼章第六]
【C语言】函数栈帧的创建和销毁详解
【第1天】SQL快速入门-基础查询(SQL 小虚竹)
mysql备份时的快照原理
从学生到职场的转变
如何在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster?
Karatsuba大数乘法的Verilog实现