def check_medical(self, question): region_wds = [] # region_tree 是一棵用region_wds 做出来的actree,快速找出question与之匹配的实体 # 但是有时候匹配的结果与我们想的不一,比如“瓜烧白菜”和“白菜”是不一样的 for i in self.region_tree.iter(question): # wd是question 用actree做了加速 wd = i[1][1] region_wds.append(wd) # 利用停用词过滤 stop_wds = [] for wd1 in region_wds: for wd2 in region_wds: # 如果词语不一样,则添加较长的 if wd1 in wd2 and wd1 != wd2: stop_wds.append(wd1) # 更新最后剩下的词语组合 final_wds = [i for i in region_wds if i not in stop_wds] # 更新字典,格式比如{'感冒':'disease'....} final_dict = {i:self.wdtype_dict.get(i) for i in final_wds} return final_dict
时间: 2023-06-24 08:09:20 浏览: 65
这段代码的作用是对输入的问题进行医疗实体识别,即从问题中找出和医疗相关的实体词,并返回一个字典,其中键为实体词,值为该实体词的类型。
具体来说,该函数首先利用 AC 自动机(即 actree)快速找出问题中与预定义实体词表(即 region_wds)匹配的词语,然后利用停用词过滤筛选出最终的实体词组合(即 final_wds)。接着,该函数利用预定义的实体类型词典(即 wdtype_dict)将每个实体词映射为其对应的类型,并返回最终的实体词字典(即 final_dict)。
相关问题
region_wds = [] # region_tree 是一棵用region_wds 做出来的actree,快速找出question与之匹配的实体 # 但是有时候匹配的结果与我们想的不一,比如“瓜烧白菜”和“白菜”是不一样的 for i in self.region_tree.iter(question): # wd是question 用actree做了加速 wd = i[1][1] region_wds.append(wd)
这段代码的作用是构建一个 AC 自动机(Aho-Corasick 自动机)用于快速找出问题中可能对应的实体。AC 自动机是一种字符串匹配算法,可以在多个模式串中同时匹配出所有出现的位置。在这里,我们将所有可能的实体作为模式串,并将它们构建成 AC 自动机,然后输入问题进行匹配。如果问题中包含某个实体,那么 AC 自动机会返回这个实体,并保存在列表 `region_wds` 中。但是,由于 AC 自动机是一种精确匹配算法,所以有时候匹配的结果可能与我们期望的不太一样。比如,“瓜烧白菜”中的“白菜”可能被误认为是问题中的实体,但实际上它并不是我们想要的那个实体。
def build_wdtype_dict(self): wd_dict = dict() for wd in self.region_words: wd_dict[wd] = [] if wd in self.disease_wds: wd_dict[wd].append('disease') if wd in self.department_wds: wd_dict[wd].append('department') if wd in self.check_wds: wd_dict[wd].append('check') if wd in self.drug_wds: wd_dict[wd].append('drug') if wd in self.food_wds: wd_dict[wd].append('food') if wd in self.symptom_wds: wd_dict[wd].append('symptom') if wd in self.producer_wds: wd_dict[wd].append('producer') return wd_dict 改为伪代码
```
定义函数 build_wdtype_dict(self):
初始化空字典 wd_dict
遍历 self.region_words 中的每个词 wd:
将 wd 作为键,值初始化为空列表
如果 wd 在 self.disease_wds 中:
将字符串 'disease' 添加到 wd_dict[wd] 列表中
如果 wd 在 self.department_wds 中:
将字符串 'department' 添加到 wd_dict[wd] 列表中
如果 wd 在 self.check_wds 中:
将字符串 'check' 添加到 wd_dict[wd] 列表中
如果 wd 在 self.drug_wds 中:
将字符串 'drug' 添加到 wd_dict[wd] 列表中
如果 wd 在 self.food_wds 中:
将字符串 'food' 添加到 wd_dict[wd] 列表中
如果 wd 在 self.symptom_wds 中:
将字符串 'symptom' 添加到 wd_dict[wd] 列表中
如果 wd 在 self.producer_wds 中:
将字符串 'producer' 添加到 wd_dict[wd] 列表中
返回 wd_dict
```