实体抽取-将bio标签转为json文件的代码

27 篇文章 1 订阅
3 篇文章 2 订阅
该代码段定义了一个函数convert_biotext_to_json,用于将不同类型的CRF标注(如BIO,BIESO,BMEO)转换为BIO格式,并保存为JSON文件。它读取输入文件,处理每一行的文本和标签,然后构造出包含实体开始和结束索引的JSON结构。最终,转换后的数据被写入到指定的输出文件中。
摘要由CSDN通过智能技术生成
def convert_biotext_to_json(input_file, save_file, format="json"):
    '''
        对于crf三种标注类型,bio, bieso, bmeo均会转为bio标签系统
    '''

    data = []
    with open(input_file, "r", encoding='utf-8') as f:
        text = ''
        labels = []
        for line in f.readlines():
            if len(line.strip()) == 0:
                assert len(text) == len(labels)
                data.append({
                    'text': text,
                    'labels': labels
                })
                text = ''
                labels = []
            else:
                w, t = line.strip().split()
                text += w
                if t.startswith("E"):
                    labels.append("I"+t[1:])
                elif t.startswith("M"):
                    labels.append("I"+t[1:])
                elif t.startswith("S"):
                    labels.append("B"+t[1:])
                else:
                    labels.append(t)

    for line in data:
        text = line["text"]
        bios = line["labels"]
        entities = []
        start_index, end_index = -1, -1
        ent_type = None
        for indx, tag in enumerate(bios):
            if tag.startswith("B-"):
                if end_index != -1:
                    entities.append(
                        {
                            "start_idx": start_index,
                            "end_idx": end_index,
                            "type": ent_type,
                            "entity": text[start_index:end_index + 1]
                        }
                    )
                # 新的实体
                start_index = indx
                end_index = indx
                ent_type = tag.split('-')[1]
                if indx == len(bios) - 1:
                    entities.append(
                        {
                            "start_idx": start_index,
                            "end_idx": end_index,
                            "type": ent_type,
                            "entity": text[start_index:end_index + 1]
                        }
                    )
            elif tag.startswith('I-') and start_index != -1:
                _type = tag.split('-')[1]
                if _type == ent_type:
                    end_index = indx

                if indx == len(bios) - 1:
                    entities.append(
                        {
                            "start_idx": start_index,
                            "end_idx": end_index,
                            "type": ent_type,
                            "entity": text[start_index:end_index + 1]
                        }
                    )
            else:
                if end_index != -1:
                    entities.append(
                        {
                            "start_idx": start_index,
                            "end_idx": end_index,
                            "type": ent_type,
                            "entity": text[start_index:end_index + 1]
                        }
                    )
                start_index, end_index = -1, -1
                ent_type = None

        line.pop("labels")
        line["entities"] = entities

    if format == "json":
        json.dump({"data": data}, open(save_file, "w"), ensure_ascii=False, indent=4)
    else:
        with open(save_file, "w") as f:
            for line in data:
                f.write(json.dumps(line, ensure_ascii=False)+"\n")

    logger.info(f"*** 转化后的json数据保存在:{save_file} ***")
    print(f"*** 转化后的json数据保存在:{save_file} ***")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值