Python代码实现基于标签的文本提取:从Conll文件中提取地名
读取train.conll文件
with open('train.conll', 'r', encoding='utf-8') as f: lines = f.readlines()
定义标签列表
tags = ['B-prov', 'I-prov', 'E-prov', 'B-city', 'I-city', 'E-city', 'B-district', 'I-district', 'E-district', 'B-town', 'I-town', 'E-town', 'B-community', 'I-community', 'E-community', 'B-village_group', 'E-village_group', 'B-road', 'I-road', 'E-road', 'B-subpoi', 'I-subpoi', 'E-subpoi']
定义提取结果的列表
result = []
遍历每一行数据
for line in lines: # 去除空格和换行符 line = line.strip() # 如果是空行,说明一句话结束,将提取结果加入列表中 if line == '': result.append('\n') else: # 按制表符分割每一行数据 parts = line.split('\t') # 获取标签 tag = parts[1] # 如果标签在标签列表中,将词加入提取结果中 if tag in tags: result.append(parts[0])
将提取结果写入ci.txt文件中
with open('ci.txt', 'w', encoding='utf-8') as f: f.write(''.join(result))
原文地址: https://www.cveoy.top/t/topic/f2JR 著作权归作者所有。请勿转载和采集!