Skip to content

Commit

Permalink
Change workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
zake7749 committed Nov 9, 2016
1 parent 540ca0c commit 45daf7c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Chatbot/QuestionAnswering/qaBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def getGeneralQA(self,query,threshold=50):
title,index = self.matcher.match(query)
sim = self.matcher.getSimilarity()
if sim < threshold:
return None
return None,0
else:
res = json.load(open(os.path.join(self.path+"/data/processed/reply/",str(int(index/1000))+'.json'),
'r',encoding='utf-8'))
targetId = index % 1000
candiates = self.evaluator.getBestResponse(res[targetId],topk=3)
reply = self.randomPick(candiates)
return reply
return reply,sim

def randomPick(self, answers):
try:
Expand All @@ -56,8 +56,8 @@ def randomPick(self, answers):
answer = None
return answer

def getCustomQA(self, sentence, api_key):
def getCustomQA(self, sentence, api_key, threshold=50):

#TODO GET USER'S QA BY api_key
#customqa_list = json.loads(getUserQA(api_key))
return None
return None,0
38 changes: 26 additions & 12 deletions Chatbot/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, name="NCKU"):
self.custom_rulebase.model = self.console.rb.model # pass word2vec model

# For QA
self.github_qa_unupdated = True
self.github_qa_unupdated = False
if not self.github_qa_unupdated:
self.answerer = qa.Answerer()

Expand All @@ -52,7 +52,7 @@ def waiting_loop(self):
res = self.listen(speech)
print(res[0])

def listen(self, sentence, target=None, api_key=None):
def listen(self, sentence, target=None, api_key=None, qa_block_threshold=80):

"""
listen function is to encapsulate the following getResponse methods:
Expand Down Expand Up @@ -90,6 +90,15 @@ def listen(self, sentence, target=None, api_key=None):
# 區隔 custom rule 與 root rule 匹配的原因是 custom rule 並不支援多段式應答
# 若後續在模組上進行更動,可考慮將兩者合併,透過辨識 api_key 的有無來修改操作

# First of all,
# Assume this sentence is for qa, but use a very high threshold.
cqa_response,cqa_sim = self.getResponseForCustomQA(sentence,api_key)
if cqa_sim > qa_block_threshold:
return cus_response,None,None,None
gqa_response,gqa_sim = self.getResponseForGeneralQA(sentence)
if gqa_sim > qa_block_threshold:
return gqa_response,None,None,None

# matching on custom rules.
response = self.getResponseOnCustomDomain(sentence, api_key)
if response is not None:
Expand All @@ -104,14 +113,13 @@ def listen(self, sentence, target=None, api_key=None):
return response,stauts,target,candiates

# The result based on custom rules and general rules are not confident.
# Assume that there are no intent in the sentence, do query matching for
# question answering.
# Assume that there are no intent in the sentence, consider this questions
# is qa again, but this time use a smaller threshold.
else:
response = self.getResponseForCustomQA(sentence,api_key)
if response is None:
response = self.getResponseForGeneralQA(sentence)
if response is not None:
return response,None,None,None
if cqa_sim > 50:
return cus_response,None,None,None
elif gqa_sim > 50:
return gqa_response,None,None,None
else:
# This query has too low similarity for all matching methods.
# We can only send back a default response.
Expand Down Expand Up @@ -186,9 +194,12 @@ def getResponseForGeneralQA(self, sentence):
"""
Listen user's input and return a response which is based on our
knowledge base.
Return:
answer, similarity
"""
if self.github_qa_unupdated:
return None
return None, 0

return self.answerer.getResponse(sentence)

Expand All @@ -197,12 +208,15 @@ def getResponseForCustomQA(self,sentence,api_key):
"""
Listen user's input and return a response which is based on a cutsom
knowledge base.
Return:
answer, similarity
"""
if self.github_qa_unupdated:
return None
return None, 0

if api_key is None:
return None
return None, 0
return self.answerer.getResponse(sentence,api_key)

def getLoggerData(self):
Expand Down

0 comments on commit 45daf7c

Please sign in to comment.