• 总结一下常见的序列标注任务的标注体系


    序列标注:序列标注是NLP中一个基本任务,在序列标注中,我们想对一个序列的每一个元素标注一个标签,一般情况下,序列标注可以分为中文分词,命名实体识别等

    每个元素都需要被标注为一个标签,,其中一个标签指向实体的开始,另外一个标签指向实体的中间部分或者结束部分,例如在NER任务中,最常用的就是BIO标注体系。

    记录下常见的标注体系:

    1.BIO标注体系:

    B-begin:代表实体的开头

    I-inside:代表实体的中间或结尾

    O-outside:代表非实体部分

    2.BIOES标注体系:

    B-begin:代表实体的开头

    I-inside:代表实体的中间

    O-outside:代表非实体部分

    E-end:代表实体的结尾

    S-single:代表单个字符,其本身就是一个实体

    3.BMES标注体系

    B-begin:代表实体的开头

    M-inside:代表实体的中间

    O-outside:代表非实体部分

    E-end:代表实体的结尾

    S-single:代表单个字符,其本身就是一个实体

    综合来看,在很多任务上各种标注体系的表现差异不大。

    下面附上BMES转为BIO标签体系的代码实现:

    1. def load_lines(path, encoding='utf8'):
    2. with open(path, 'r', encoding=encoding) as f:
    3. lines = [line.strip() for line in f.readlines()]
    4. return lines
    5. def write_lines(lines, path, encoding='utf8'):
    6. with open(path, 'w', encoding=encoding) as f:
    7. for line in lines:
    8. f.writelines('{}\n'.format(line))
    9. def bmes_to_json(bmes_file, json_file):
    10. """
    11. 将bmes格式的文件,转换为json文件,json文件包含text和label,并且转换为BIO的标注格式
    12. Args:
    13. bmes_file:
    14. json_file:
    15. :return:
    16. """
    17. texts = []
    18. with open(bmes_file, 'r', encoding='utf8') as f:
    19. lines = f.readlines()
    20. words = []
    21. labels = []
    22. for idx in trange(len(lines)):
    23. line = lines[idx].strip()
    24. if not line:
    25. assert len(words) == len(labels), (len(words), len(labels))
    26. sample = {}
    27. sample['text'] = words
    28. sample['label'] = labels
    29. texts.append(json.dumps(sample, ensure_ascii=False))
    30. words = []
    31. labels = []
    32. else:
    33. word = line.split()
    34. label = line.split()
    35. label = str(label).replace('M-', 'I-').replace('E-', 'I-')
    36. words.append(word)
    37. labels.append(label)
    38. with open(json_file, 'w', encoding='utf-8') as f:
    39. for text in texts:
    40. f.write("{}\n".format(text))
    41. if __name__ == '__main__':
    42. # 生成json文件
    43. data_names = ['msra']
    44. path = '../datasets'
    45. for data_name in data_names:
    46. logger.info('processing dataset:{}'.format(data_name))
    47. files = os.listdir(join(path, data_name))
    48. for file in files:
    49. file = join(path, data_name, file)
    50. data_type = os.path.basename(file).split('.')[0]
    51. out_path = join(path, data_name, data_type+'.json')

  • 相关阅读:
    jupyter使用教程及python语法基础
    微信小程序- - - - - rich-text 富文本问题
    HDLbits: Lemmings2
    Python(四)——变量的定义和简单使用
    Java多线程案例【定时器】
    JuiceFS v1.0 正式发布,首个面向生产环境的 LTS 版本
    盘点四大运营商的5G套餐亮点,国庆出游上车5G不用愁!
    【Java分享客栈】一文搞定CompletableFuture并行处理,成倍缩短查询时间。
    从头开始机器学习:逻辑回归
    QT集成Protobuf
  • 原文地址:https://blog.csdn.net/weixin_48592695/article/details/126004598