“基于医疗知识图谱的问答系统”代码解析(二)
question_classifier.py --问题分类器代码解析
“基于知识医疗图谱的问答系统”代码解析(一)
“基于医疗知识图谱的问答系统”代码解析(三)
“基于医疗知识图谱的问答系统”代码解析(四)
“基于医疗知识图谱的问答系统”代码解析(五)
#!/usr/bin/env python3
# coding: utf-8
# File: question_classifier.py
# Author: lhy<lhy_in_blcu@126.com,https://huangyong.github.io>
# Date: 18-10-4
# 导入操作系统接口模块
import os
# ahocosick:自动机的意思
# 可实现自动批量匹配字符串的作用,即可一次返回该条字符串中命中的所有关键词
import ahocorasick
# 建立问题分类器的类
class QuestionClassifier:
def __init__(self):
# cur_dir 是当前目录 其中[:-1]可以达到返回上一层的效果
cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
# 加载特征词路径
self.disease_path = os.path.join(cur_dir, 'dict/disease.txt')
self.department_path = os.path.join(cur_dir, 'dict/department.txt')
self.check_path = os.path.join(cur_dir, 'dict/check.txt')
self.drug_path = os.path.join(cur_dir, 'dict/drug.txt')
self.food_path = os.path.join(cur_dir, 'dict/food.txt')
self.producer_path = os.path.join(cur_dir, 'dict/producer.txt')
self.symptom_path = os.path.join(cur_dir, 'dict/symptom.txt')
self.deny_path = os.path.join(cur_dir, 'dict/deny.txt')
# 加载特征词 这里encoding用的是‘utf-8’模式,不加的话,我的pycharm会报错
self.disease_wds= [i.strip() for i in open(self.disease_path,encoding='utf-8') if i.strip()]
self.department_wds= [i.strip() for i in open(self.department_path,encoding='utf-8') if i.strip()]
self.check_wds= [i.strip() for i in open(self.check_path,encoding='utf-8') if i.strip()]
self.drug_wds= [i.strip() for i in open(self.drug_path,encoding='utf-8') if i.strip()]
self.food_wds= [i.strip() for i in open(self.food_path,encoding='utf-8') if i.strip()]
self.producer_wds= [i.strip() for i in open(self.producer_path,encoding='utf-8') if i.strip()]
self.symptom_wds= [i.strip() for i in open(self.symptom_path,encoding='utf-8') if i.strip()]
self.region_words