感谢天善智能的aopu老师,因为此次学习的代码及资料都由aopu老师提供,这次主要对自己的学习情况进行整理并做些笔记。感兴趣的可以查看老师的课程链接:https://edu.hellobi.com/course/263/lessons
整体结构

整体结构图
数据生成
数据使用IOBES结构标签进行标注:
# B,即Begin,表示开始 # I,即Intermediate,表示中间 # E,即End,表示结尾 # S,即Single,表示单个字符 # O,即Other,表示其他,用于标记无关字符
此外还有部分医学相关的命名实体:
# DIS:疾病 PT:既往信息词 CL:条件词 # SYM:症状 REG:器官 PSB:可能的 # TES:检查 ORG:部位词 FW:频率词 # DRU:药品 AT:否定词 PRE:措施 # SUR:手术
通过上述两种标签组合为以下的形式:

减号之前表示词的结构,B为词的开头,I为词的中间;减号之后表示词性,比如疼痛被标注为SYM,意为患者的症状。最终实现的效果是将原始文本转换为标签标注格式的文本:每个句子逐个拆解,与医学有关的词后面会加一个后缀。

将原始文本转换为IOB结构
理解了上述的一些内容以后,下面对数据生成部分的程序进行设计:

程序执行示意图
从source_data读取患者出院情况:

# 相对路径source_data c_root=os.getcwd()+os.sep+"source_data"+os.sep
从DICT_NOW.csv读取病状字典:

# 从DICT_NOW.csv读取病状字典 dics=csv.reader(open("DICT_NOW.csv",'r',encoding='utf8')) for row in dics: if len(row)==2: jieba.add_word(row[0].strip(),tag=row[1].strip()) jieba.suggest_freq(row[0].strip()) split_num=0
定义标点符号与标签:
# 标点符号 fuhao=set(['。','?','?','!','!']) # 标签 biaoji = set(['DIS', 'SYM', 'SGN', 'TES', 'DRU', 'SUR', 'PRE', 'PT', 'Dur', 'TP', 'REG', 'ORG', 'AT', 'PSB', 'DEG', 'FW','CL'])
对出院情况切词,并用病状字典判断词性:
# 从source_data读取数据 for file in os.listdir(c_root): if "txtoriginal.txt" in file: # 读取后缀为.txtoriginal.txt的数据 fp=open(c_root+file,'r',encoding='utf8') for line in fp: split_num+=1 # 使用jieba分词模块对原始文本分词 words=pseg.cut(line) for key,value in words: #print(key) #print(value) if value.strip() and key.strip(): import time start_time=time.time() # 分三个文件:训练集,测试集,验证集 index=str(1) if split_num%15<2 else str(2) if split_num%15>1 and split_num%15<4 else str(3) end_time=time.time() print("method one used time is {}".format(end_time-start_time)) # 不在标签中的值value记为'O' if value not in biaoji: value='O' for achar in key.strip(): if achar and achar.strip() in fuhao: string=achar+" "+value.strip()+"\n"+"\n" dev.write(string) if index=='1' else test.write(string) if index=='2' else train.write(string) elif achar.strip() and achar.strip() not in fuhao: string = achar + " " + value.strip() + "\n" dev.write(string) if index=='1' else test.write(string) if index=='2' else train.write(string) # 在标签中的值首个字记为'B-',在标签中的值首个字以后的字记为'I-' elif value.strip() in biaoji: begin=0 for char in key.strip(): if begin==0: begin+=1 string1=char+' '+'B-'+value.strip()+'\n' if index=='1': dev.write(string1) elif index=='2': test.write(string1) elif index=='3': train.write(string1) else: pass else: string1 = char + ' ' + 'I-' + value.strip() + '\n' if index=='1': dev.write(string1) elif index=='2': test.write(string1) elif index=='3': train.write(string1) else: pass else: continue
将判断结果分别写入到验证集、测试集,训练集:


# 写入到dev验证集(1),test测试集(2),train训练集(14) dev=open("example.dev",'w',encoding='utf8') train=open("example.train",'w',encoding='utf8') test=open("example.test",'w',encoding='utf8') dev.close() train.close() test.close()
坐等沙发