ConLL2003格式如何转换为适配doccano的JsonL并合并IOB实体标签
IOB标签合并与doccano格式转换实现
现有方案的核心问题
之前两种合并方法的本质缺陷是没有维护连续实体的状态上下文:
- 固定k值的正向遍历会把整句话里所有连续B/I的长度累加为全局固定k,遇到句内多个独立实体时必然出现索引越界
- 反向逐对合并只会处理相邻两个标签,无法覆盖3段及以上的长连续实体
另外你现有代码里的逐词坐标计算存在隐藏bug:用text.find(word)反推词的位置时,只要句子里出现重复单词,坐标就会匹配到第一次出现的位置,就算标签合并正确,导入doccano后标注框也会完全错位。
正确实现逻辑
合并IOB标签不需要复杂的回溯计算,按顺序遍历逐词标签、维护当前正在累积的实体状态即可:
- 遇到
O标签:把当前累积的未关闭实体写入结果,清空状态 - 遇到
B-XXX标签:先把之前累积的未关闭实体写入结果,开启新的实体,记录起始位置和实体类型 - 遇到
I-XXX标签:如果和当前累积的实体类型一致,就更新实体的结束位置;如果类型不一致,就把之前累积的实体写入,重新开启新实体
完整可运行代码
直接从原始嵌套列表(sentences逐词列表、labels逐词标签列表)生成符合doccano要求的JsonL文件,同时解决坐标计算错位问题:
import json # 替换为你自己的实际语料 sentences = [ ['EU', 'rejects', 'German', 'call', 'to', 'boycott', 'British', 'lamb', '.'], ['Peter', 'Blackburn'], ['Corina', 'Casanova', ',', 'İsviçre', 'Federal', 'Şansölyesidir', '.'] ] labels = [ ['B-ORG', 'O', 'B-MISC', 'O', 'O', 'O', 'B-MISC', 'O', 'O'], ['B-Person', 'I-Person'], ['B-Person', 'I-Person', 'O', 'B-Country', 'B-Misc', 'I-Misc', 'O'] ] doccano_records = [] for sent_idx, (words, tags) in enumerate(zip(sentences, labels)): # 同步计算每个词的准确坐标,避免字符串搜索的重复匹配问题 word_spans = [] current_pos = 0 for word in words: start = current_pos end = start + len(word) word_spans.append((start, end)) current_pos = end + 1 # 计入词间空格的长度 full_text = " ".join(words) # 合并IOB标签 merged_labels = [] current_entity = None for tag, (w_start, w_end) in zip(tags, word_spans): if tag == "O": if current_entity: merged_labels.append(current_entity) current_entity = None continue iob_prefix, ent_type = tag.split("-", 1) if iob_prefix == "B": if current_entity: merged_labels.append(current_entity) current_entity = [w_start, w_end, ent_type] elif iob_prefix == "I": if current_entity and current_entity[2] == ent_type: current_entity[1] = w_end else: if current_entity: merged_labels.append(current_entity) current_entity = [w_start, w_end, ent_type] # 处理句尾最后一个未关闭的实体 if current_entity: merged_labels.append(current_entity) doccano_records.append({ "id": sent_idx, "text": full_text, "labels": merged_labels }) # 写入JsonL文件,每行一个JSON对象,可直接导入doccano with open("doccano_import.jsonl", "w", encoding="utf-8") as f: for record in doccano_records: f.write(json.dumps(record, ensure_ascii=False) + "\n")
适配现有中间结构的合并方案
如果你已经生成了包含逐词坐标、逐词IOB标签的list_json中间结构,不需要重新从原始语料处理,直接调用下面的合并函数替换原有labels即可:
def merge_iob_labels(per_word_labels): merged = [] current_entity = None for start, end, tag in per_word_labels: if tag == "O": if current_entity: merged.append(current_entity) current_entity = None continue iob_prefix, ent_type = tag.split("-", 1) if iob_prefix == "B": if current_entity: merged.append(current_entity) current_entity = [start, end, ent_type] elif iob_prefix == "I": if current_entity and current_entity[2] == ent_type: current_entity[1] = end else: if current_entity: merged.append(current_entity) current_entity = [start, end, ent_type] if current_entity: merged.append(current_entity) return merged # 批量处理所有条目 for item in list_json: item["labels"] = merge_iob_labels(item["labels"])
这个实现不会出现索引越界问题,不管连续同类型的I标签有多少段,都会合并成一个完整实体,最终输出的标签格式完全匹配doccano的导入要求。
内容的提问来源于stack exchange,提问作者Harun Küfrevi
相关产品推荐
相关产品推荐

