def answer_SBV_ATT_POB(cls, words, arcs_dict, postags, hed_index): pron = words[hed_index] obj_index = arcs_dict[hed_index]['POB'][0] obj = words[obj_index] subj_index = arcs_dict[hed_index]['SBV'][0] subj = words[subj_index] att_index = arcs_dict[obj_index]['ATT'][0] att = words[att_index] if postags[att_index] == 'r': uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj r = requests.post(uri) if r.json()['message'] == 'success': ans_dict = dict(r.json()['data']['avp']) keys = ans_dict.keys() ans = NLPUtil.get_similarity(obj, keys) if ans != None: return ans_dict[ans] else: return "对不起,我不太明白你在说什么\n" elif postags[att_index] in NLPUtil.noun: uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj r = requests.post(uri) if r.json()['message'] == 'success': ans_list = list(r.json()['data']['avp']) values = [x[1] for x in ans_list] ans = NLPUtil.get_similarity(att, values) if ans != None: return "是的," + subj + pron + att + obj + "\n" else: return "很抱歉," + subj + "不" + pron + att + obj + "\n"
def answer_SBV_POB(cls, words, arcs_dict, postags, hed_index): prep = words[hed_index] obj_index = arcs_dict[hed_index]['POB'][0] obj = words[obj_index] subj_index = arcs_dict[hed_index]['SBV'][0] subj = words[subj_index] if postags[obj_index] == 'r': uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj r = requests.post(uri) if r.json()['message'] == 'success': ans = dict(r.json())['data']['desc'] if ans != None: return ans else: return "对不起,我不太明白你在说什么\n" elif postags[obj_index] in NLPUtil.noun and postags[subj_index] != 'r': uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj r = requests.post(uri) ans_list = list(r.json()['data']['avp']) values = [x[1] for x in ans_list] ans = NLPUtil.get_similarity(obj, values) if ans != None: return "是的," + subj + prep + obj + "\n" else: return "对不起," + subj + "不" + prep + obj + "\n" elif postags[subj_index] == 'r': uri = "https://api.ownthink.com/kg/knowledge?entity=" + obj r = requests.post(uri) if r.json()['message'] == 'success': ans = dict(r.json())['data']['desc'] if ans != None: return ans else: return "对不起,我不太明白你在说什么\n"
def answer_ATT_HED(cls, words, arcs_dict, postags, hed_index): word = words[hed_index] att_index = arcs_dict[hed_index]['ATT'][0] att = words[att_index] if postags[att_index] in NLPUtil.noun: #print(att,"qqq") uri = "https://api.ownthink.com/kg/knowledge?entity=" + att r = requests.post(uri) if r.json()['message'] == 'success': ans_dict = dict(r.json()['data']['avp']) keys = ans_dict.keys() ans = NLPUtil.get_similarity(word, keys) if ans != None: return ans_dict[ans] else: return "对不起,我不太明白你在说什么\n" else: uri = "https://api.ownthink.com/kg/knowledge?entity=" + word r = requests.post(uri) if r.json()['message'] == 'success': ans = dict(r.json())['data']['desc'] if ans != None: return ans else: return "对不起,我不太明白你在说什么\n"
def answer_ATT_SBV_POB(cls, words, arcs_dict, postags, hed_index): prep = words[hed_index] obj_index = arcs_dict[hed_index]['POB'][0] obj = words[obj_index] subj_index = arcs_dict[hed_index]['SBV'][0] subj = words[subj_index] att_index = arcs_dict[subj_index]['ATT'][0] att = words[att_index] if postags[att_index] in NLPUtil.noun: uri = "https://api.ownthink.com/kg/knowledge?entity=" + att r = requests.post(uri) if r.json()['message'] == 'success': ans_dict = dict(r.json()['data']['avp']) keys = ans_dict.keys() ans = NLPUtil.get_similarity(subj, keys) if ans != None: return ans_dict[ans] else: return "对不起,我不太明白你在说什么\n" else: uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj r = requests.post(uri) if r.json()['message'] == 'success': ans = dict(r.json())['data']['desc'] if ans != None: return ans else: return "对不起,我不太明白你在说什么\n"
def chat(question_str): words, pattern, arcs_dict, postags, hed_index = NLPUtil.get_sentence_pattern( question_str) aiml_reponse = AIMLUtil.response(pattern) #print(pattern,aiml_reponse) answer = TaskManager.task_response(aiml_reponse, words, arcs_dict, postags, hed_index) return answer
def get_attr(cls, target, attr): uri = "https://api.ownthink.com/kg/knowledge?entity=" + target r = requests.post(uri) if r.json()['message'] == 'success': ans_dict = dict(r.json()['data']['avp']) keys = ans_dict.keys() ans = NLPUtil.get_similarity(attr, keys) if ans != None: return target + "的" + ans + "为" + ans_dict[ans] + "\n" else: return "对不起,我不太明白你在说什么\n"
def answer_SBV_ATT_POB2(cls, words, arcs_dict, postags, hed_index): flag = False att_index = 0 subject = "" attribution = "" for sub_arcs in arcs_dict: keys = sub_arcs.keys() #print(keys) if 'ATT' in keys: for att in range(len(sub_arcs['ATT'])): #print("sub_arcs['ATT'][att]",sub_arcs['ATT'][att]) if postags[sub_arcs['ATT'][att]] == 'r': flag = True att_index = sub_arcs['ATT'][att] break if flag: break #print(att_index) if flag: break for sub_arcs in arcs_dict: keys = sub_arcs.keys() if 'SBV' in keys: sbv_list = sub_arcs['SBV'] #print("sbv_list",sbv_list) for sub_sbv in sbv_list: if sub_sbv < att_index: subject = words[sub_sbv] break #print("subject",subject) if 'POB' in keys: pob_list = sub_arcs['POB'] for sub_pob in pob_list: if sub_pob > att_index: attribution = words[sub_pob] break #print(attribution) if flag: uri = "https://api.ownthink.com/kg/knowledge?entity=" + subject r = requests.post(uri) #print(r.json()) if r.json()['message'] == 'success': ans_dict = dict(r.json()['data']['avp']) keys = ans_dict.keys() #print(keys) ans = NLPUtil.get_similarity(attribution, keys) if ans != None: #print(ans,ans_dict[ans]) return ans_dict[ans] else: return "对不起,我不太明白你在说什么\n"
def get_judge_att(cls, subj, hed, att, obj, attr): uri = "https://api.ownthink.com/kg/knowledge?entity=" + subj r = requests.post(uri) if r.json()['message'] == 'success': ans_list = list(r.json()['data']['avp']) values = [x[1] for x in ans_list] ans = NLPUtil.get_similarity(attr, values) if ans != None: return "是的," + subj + hed + att + obj + "\n" elif hed == '有': return "很抱歉," + subj + "没" + hed + att + obj + "\n" else: return "很抱歉," + subj + "不" + hed + att + obj + "\n"
import sys project_path = os.path.abspath(os.path.join(os.getcwd(), "..")) sys.path.append(project_path) from cbot import AIMLUtil from cbot import NLPUtil from cbot import TaskManager def chat(question_str): words, pattern, arcs_dict, postags, hed_index = NLPUtil.get_sentence_pattern( question_str) aiml_reponse = AIMLUtil.response(pattern) #print(pattern,aiml_reponse) answer = TaskManager.task_response(aiml_reponse, words, arcs_dict, postags, hed_index) return answer if __name__ == '__main__': AIMLUtil() NLPUtil('ltp_data_v3.4.0') while True: question_str = input('User:'******'exit': break else: ans = chat(question_str) print(ans)