-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
70 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
import sys | ||
sys.path.append(os.path.dirname(__file__)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,61 @@ | ||
import json | ||
import logging | ||
import os | ||
|
||
from .match import * | ||
from .responsesEvaluate import Evaluator | ||
|
||
class QABase(object): | ||
class Answerer(object): | ||
|
||
def __init__(self, data_path): | ||
def __init__(self): | ||
|
||
""" | ||
Args: | ||
data_path: 指出 data 資料夾位在哪個位置 | ||
""" | ||
self.general_questions = [] | ||
self.data_path = data_path | ||
self.path = os.path.dirname(__file__) | ||
|
||
self.matcher = getMatcher(matcherType="Fuzzy") | ||
self.evaluator = Evaluator() | ||
self.testSegment() | ||
|
||
def testSegment(self): | ||
logging.info("測試斷詞模塊中") | ||
try: | ||
self.matcher.wordSegmentation("測試一下斷詞") | ||
logging.info("測試成功") | ||
except Exception as e: | ||
logging.info(repr(e)) | ||
logging.info("模塊載入失敗,請確認data與字典齊全") | ||
|
||
def getResponse(self, sentence, api_key=None): | ||
|
||
if api_key is not None: | ||
response = self.getCustomQA(sentence,api_key) | ||
else: | ||
response = self._getGeneralQA(sentence) | ||
response = self.getGeneralQA(sentence) | ||
return response | ||
|
||
def getGeneralQA(self, sentence): | ||
def getGeneralQA(self,query,threshold=50): | ||
|
||
pass | ||
title,index = self.matcher.match(query) | ||
sim = self.matcher.getSimilarity() | ||
if sim < threshold: | ||
return None | ||
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 | ||
|
||
def randomPick(self, answers): | ||
try: | ||
answer = answers[random.randrange(0,len(answers))][0] | ||
except: | ||
answer = None | ||
return answer | ||
|
||
def getCustomQA(self, sentence, api_key): | ||
|
||
#TODO GET USER'S QA BY api_key | ||
#customqa_list = json.loads(getUserQA(api_key)) | ||
pass | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters