From 97e4d5d76da3b1398956ce9aae2f8fe05093ad75 Mon Sep 17 00:00:00 2001 From: F74026284 Date: Sat, 19 Nov 2016 22:00:13 +0800 Subject: [PATCH] Add validation module. --- Chatbot/QuestionAnswering/qaBase.py | 6 ++-- Chatbot/Validation/__init__.py | 3 ++ Chatbot/Validation/validate.py | 44 +++++++++++++++++++++++ Chatbot/task_modules/medicine/medicine.py | 2 ++ Chatbot/task_modules/other/stock.py | 2 +- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 Chatbot/Validation/validate.py diff --git a/Chatbot/QuestionAnswering/qaBase.py b/Chatbot/QuestionAnswering/qaBase.py index 0bbdf90..fd863a5 100644 --- a/Chatbot/QuestionAnswering/qaBase.py +++ b/Chatbot/QuestionAnswering/qaBase.py @@ -64,14 +64,14 @@ def getCustomQA(self, sentence, api_key, threshold=50): #i.e IMPLEMENT getUserQA(api_key) #customqa_list = json.loads(getUserQA(api_key)) - data = '[{"Question":"你媽長得像魚人","Answers":["你媽也長得像魚人","你比痲瘋地精還臭"]}]' + data = '[{"question": "你好嗎?","answers": ["很好","不太好"]},{"question": "吃飽了沒?","answers": ["正要吃","剛吃飽"]}]' customqa_list = json.loads(data) # Load question to a list. - q_list = [qa["Question"] for qa in customqa_list] + q_list = [qa["question"] for qa in customqa_list] #TODO customized threshold. title,index = self.matcher.match(sentence,custom_title=q_list) sim = self.matcher.getSimilarity() if sim < threshold: return None,0 - return customqa_list[index]["Answers"][random.randrange(0,len(customqa_list[index]["Answers"]))],sim + return customqa_list[index]["answers"][random.randrange(0,len(customqa_list[index]["answers"]))],sim diff --git a/Chatbot/Validation/__init__.py b/Chatbot/Validation/__init__.py index e69de29..4955e7e 100644 --- a/Chatbot/Validation/__init__.py +++ b/Chatbot/Validation/__init__.py @@ -0,0 +1,3 @@ +import os +import sys +sys.path.append(os.path.dirname(__file__)) diff --git a/Chatbot/Validation/validate.py b/Chatbot/Validation/validate.py new file mode 100644 index 0000000..c728bfb --- /dev/null +++ b/Chatbot/Validation/validate.py @@ -0,0 +1,44 @@ + +class Validator(object): + + """ + 用於驗證預設匹配結果與實際匹配結果是否有不同 + """ + + def __init__(self): + self.paths = [] + self.sentences = [] + + def loadValiadationData(self, paths_datapath, sentences_datapath): + + self.loadPaths(paths_datapath) + self.loadSentences(sentences_datapath) + + def loadPaths(self, path): + with open(path,'r',encoding='utf-8') as input: + self.paths = [line.strip('\n') for line in input] + + def loadSentences(self, path): + with open(path,'r',encoding='utf-8') as input: + self.sentences = [line.strip('\n') for line in input] + + def valiate(self, match): + + """ + 驗證預設分類結果與實際分類結果 + + Args: + # match:用於比對的函式,建議採用 console.match,該函式 + # 需要回傳匹配路徑與匹配相似度 + + Return : 誤分點的表列 + """ + + miss = 0 + + for i in range(0,len(self.sentences)): + for sentence in self.sentences: + sim,path = match(sentence) + if path != self.paths[i]: + miss += 1 + print("在'%s'中,預期為 %s,實際為 %s" % (sentence,self.paths[i],path)) diff --git a/Chatbot/task_modules/medicine/medicine.py b/Chatbot/task_modules/medicine/medicine.py index 6e87cd3..23b4034 100644 --- a/Chatbot/task_modules/medicine/medicine.py +++ b/Chatbot/task_modules/medicine/medicine.py @@ -178,6 +178,8 @@ def pattern_match(self, subject, description): return "胃痛" elif subject=="腹部" and description == "痛推理": return "腹痛" + elif subject=="腹部" and description == "不舒服推理": + return "腹脹" elif subject=="腹部" and description == "瀉推理": return "腹瀉" elif subject=="心臟" and description == "痛推理": diff --git a/Chatbot/task_modules/other/stock.py b/Chatbot/task_modules/other/stock.py index 821e600..4b4e02c 100644 --- a/Chatbot/task_modules/other/stock.py +++ b/Chatbot/task_modules/other/stock.py @@ -9,7 +9,7 @@ class Stock(object): def __init__(self, console): self.console = console self.is_close = False - + def get_response(self,user_input, domain, target): """ Return: