def run(query): #if __name__ == '__main__': #初始化jb分词器 T.jieba_initialize() #切换到语料库所在工作目录 mybot_path = './' os.chdir(mybot_path) mybot = aiml.Kernel() mybot.learn(os.path.split(os.path.realpath(__file__))[0]+"/resources/std-startup.xml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/bye.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/tools.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/bad.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/funny.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/OrdinaryQuestion.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/Common conversation.aiml") if len(query) > 60: answer = '句子长度过长' elif query.strip() == '': answer = mybot.respond('无') else: message = T.wordSegment(query) words = T.postag(query) response = mybot.respond(message) if response == '': answer = mybot.respond('找不到答案') elif response[0] == '#': if response.__contains__("searchbaike"): res = response.split(':') entity = str(res[1]).replace(" ","") attr = str(res[2]).replace(" ","") ans = baike.query(entity, attr) if '找不到' not in ans: answer = ans elif ans.__contains__('找不到'): answer = search_summary.kwquery(query) if len(answer) == 0: answer = mybot.respond('找不到答案') elif len(answer) == 1: print(answer) answer = answer[0].strip().replace(' ','').replace("\n","") else: answer = '找不到答案' else: answer = '找不到答案' else: answer = search_summary.kwquery(query) if len(answer) == 0: answer = mybot.respond('找不到答案') elif len(answer) == 1: answer = answer[0].strip().replace(' ','').replace("\n","") else: answer = '找不到答案' return answer
def qa(): #初始化jb分词器 T.jieba_initialize() #切换到语料库所在工作目录 mybot_path = './' # os.chdir(mybot_path) mybot = aiml.Kernel() mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/std-startup.xml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/Common conversation.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/bye.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/tools.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/bad.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/funny.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/OrdinaryQuestion.aiml") # mybot.respond('Load Doc Snake') #载入百科属性列表 print ''' .----------------. .-----------------. .----------------. .----------------. .----------------. | .--------------. || .--------------. || .--------------. || .--------------. || .--------------. | | | _______ | || | ____ _____ | || | __ | || | ___ ____ | || | _________ | | | | / ___ | | || ||_ \|_ _| | || | / \ | || | |_ ||_ _| | || | |_ ___ | | | | | | (__ \_| | || | | \ | | | || | / /\ \ | || | | |_/ / | || | | |_ \_| | | | | '.___`-. | || | | |\ \| | | || | / /__\ \ | || | | __'. | || | | _| _ | | | | |`\____) | | || | _| |_\ |_ | || | _/ / \ \_ | || | _| | \ \_ | || | _| |___/ | | | | | |_______.' | || ||_____|\____| | || ||____| |____|| || | |____||____| | || | |_________| | | | | | || | | || | | || | | || | | | | '--------------' || '--------------' || '--------------' || '--------------' || '--------------' | '----------------' '----------------' '----------------' '----------------' '----------------' Eric:你好,我是Eric。╭(╯^╰)╮ ''' while True: input_message = raw_input("Enter your message >> ") if len(input_message) > 60: print mybot.respond("句子长度过长") continue elif input_message.strip() == '': print mybot.respond("无") continue print input_message message = T.wordSegment(input_message) # 去标点 print 'word Seg:' + message print '词性:' words = T.postag(input_message) if message == 'q': exit() else: response = mybot.respond(message) print "=======" print response print "=======" if response == "": ans = mybot.respond('找不到答案') print 'Eric:' + ans # 百科搜索 elif response[0] == '#': # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') #实体 entity = str(res[1]).replace(" ", "") #属性 attr = str(res[2]).replace(" ", "") print entity + '<---->' + attr ans = baike.query(entity, attr) # 如果命中答案 if type(ans) == list: print 'Eric:' + QAT.ptranswer(ans, False) continue elif ans.decode('utf-8').__contains__(u'::找不到'): #百度摘要+Bing摘要 print "通用搜索" ans = search_summary.kwquery(input_message) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(input_message) if len(ans) == 0: ans = mybot.respond('找不到答案') print 'Eric:' + ans elif len(ans) > 1: print "不确定候选答案" print 'Eric: ' for a in ans: print a.encode("utf8") else: print 'Eric:' + ans[0].encode("utf8") # 匹配模版 else: print 'Eric:' + response
ans = mybot.respond('找不到答案') print 'Eric:' + ans # 百科搜索 elif response[0] == '#': # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') #实体 entity = str(res[1]).replace(" ", "") #属性 attr = str(res[2]).replace(" ", "") print entity + '<---->' + attr ans = baike.query(entity, attr) # 如果命中答案 if type(ans) == list: print 'Eric:' + QAT.ptranswer(ans, False) continue elif ans.decode('utf-8').__contains__(u'::找不到'): #百度摘要+Bing摘要 print "通用搜索" ans = search_summary.kwquery(input_message) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(input_message) if len(ans) == 0:
def ws(): user_socket = request.environ.get('wsgi.websocket') # type:WebSocket while 1: msg =user_socket.receive() question = json.loads(msg) q = question['data']['mine']['content'] msg =q input_message = str(msg).encode('utf-8') if len(input_message) > 60: answer = mybot.respond("句子长度过长") # continue elif input_message.strip() == '': answer = mybot.respond("无话可说") # continue # print input_message message = T.wordSegment(input_message) # 去标点 # print 'word Seg:'+ message # print '词性:' words = T.postag(input_message) if message == 'q': exit() else: response = mybot.respond(message) # 在AIML数据集里寻找答案 print "=======" if response[0] == '#': print response + 'mark' else: answer = response print "=======" if response == "": ans = mybot.respond('找不到答案') answer = ans # 百科搜索 elif response[0] == '#' or len(response) < 1: # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') # 实体 entity = str(res[1]).replace(" ", "") # 属性 attr = str(res[2]).replace(" ", "") print entity + '<---->' + attr ans = baike.query(entity, attr) # 如果命中答案 print type(ans) if type(ans) == list: answer = '回答:' + QAT.ptranswer(ans, False) # continue elif ans.decode('utf-8').__contains__(u'::找不到'): # 百度摘要+Bing摘要 print "通用搜索" answer = search_summary.kwquery(input_message) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(input_message) print type(ans) if len(ans) == 0: ans = mybot.respond('找不到答案') answer = '回答:' + ans elif len(ans) > 1: print "不确定候选答案" answer = ans[0] print 'Eric: ' for a in ans: print a.encode("utf-8") else: answer = '回答:' + ans[0].encode("utf-8") # 匹配模版 else: answer = '回答:' + response s = '展开全部' print type(answer).__name__ if (type(answer).__name__ == 'list' and len(answer)>0) or '唔... 怎么回答...'in answer or '天气' in msg: answer = geta(msg) answer = answer print 'wocao' else: #print answer if s in str(answer): print answer answer = str(answer).replace('\n', '').replace('展开全部', "").split('已赞过')[0] print 'OS' +answer print user_socket,msg res =answer a = { "username": "******", "avatar": "https://robot.rszhang.top/images/icon/nv/0.jpg", "id": "-2", # //消息的来源ID(如果是私聊,则是用户id,如果是群聊,则是群组id) "type": "friend", # //聊天窗口来源类型,从发送消息传递的to里面获取 "content": res, # //消息内容 "cid": 0, # //消息id,可不传。除非你要对消息进行一些操作(如撤回) "mine": True, # //是否我发送的消息,如果为true,则会显示在右方 "fromid": "100000", # /消息的发送者id(比如群组中的某个消息发送者),可用于自动解决浏览器多窗口时的一些问题 "timestamp": 1467475443306, # //服务端时间戳毫秒数。注意:如果你返回的是标准的 unix 时间戳,记得要 *1000 } user_socket.send(json.dumps(a))
ans = mybot.respond('找不到答案') print 'Eric:' + ans # 百科搜索 elif response[0] == '#': # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') #实体 entity = str(res[1]).replace(" ","") #属性 attr = str(res[2]).replace(" ","") print entity+'<---->'+attr ans = baike.query(entity, attr) # 如果命中答案 if type(ans) == list: print 'Eric:' + QAT.ptranswer(ans,False) continue elif ans.decode('utf-8').__contains__(u'::找不到'): #百度摘要+Bing摘要 print "通用搜索" ans = search_summary.kwquery(input_message) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(input_message)
def run(question): # 初始化jb分词器 T.jieba_initialize() # 切换到语料库所在工作目录 mybot_path = './' os.chdir(mybot_path) mybot = aiml.Kernel() if os.path.isfile("bot_brain.brn"): mybot.bootstrap(brainFile="bot_brain.brn") else: mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/std-startup.xml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/tuling.xml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/bye.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/tools.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/bad.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/funny.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/OrdinaryQuestion.aiml") mybot.learn( os.path.split(os.path.realpath(__file__))[0] + "/resources/Common conversation.aiml") #mybot.bootstrap(learnFiles="std-startup.xml", commands="load aiml b") mybot.saveBrain("bot_brain.brn") # 载入百科属性列表 print ''' Eric:你好,我是问答机器人。╭(╯^╰)╮ ''' input_message = question if len(input_message) > 60: return mybot.respond("句子长度过长") #continue elif input_message.strip() == '': return mybot.respond("无话可说") #continue # print input_message message = T.wordSegment(input_message) # 去标点 # print 'word Seg:'+ message # print '词性:' words = T.postag(input_message) if message == 'q': exit() else: response = mybot.respond(message) # 在AIML数据集里寻找答案 print "=======" if response[0] == '#': print response + 'mark' pass else: return response print "=======" if response == "": ans = mybot.respond('找不到答案') print 'Eric:' + ans # 百科搜索 elif response[0] == '#' or len(response) < 1: # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') # 实体 entity = str(res[1]).replace(" ", "") # 属性 attr = str(res[2]).replace(" ", "") print entity + '<---->' + attr ans = baike.query(entity, attr) # 如果命中答案 if type(ans) == list: return '回答:' + QAT.ptranswer(ans, False) #continue elif ans.decode('utf-8').__contains__(u'::找不到'): # 百度摘要+Bing摘要 print "通用搜索" ans = search_summary.kwquery(input_message) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(input_message) if len(ans) == 0: ans = mybot.respond('找不到答案') return '回答:' + ans elif len(ans) > 1: print "不确定候选答案" return ans print 'Eric: ' for a in ans: print a.encode("utf-8") else: return '回答:' + ans[0].encode("utf-8") # 匹配模版 else: return '回答:' + response
def handle(self): conn = self.request conn.sendall('欢迎访问智能百科问答系统') Flag =True data = conn.recv(4096) while Flag: input_message = data print "input_message=====" print input_message print "==========" reply = '' if len(input_message) > 60: print mybot.respond("句子长度过长") reply = mybot.respond("句子长度过长") conn.sendall(reply) Flag =False continue elif input_message.strip() == '无': print mybot.respond("无") reply = mybot.respond("无") conn.sendall(reply) Flag = False continue print input_message message = T.wordSegment(input_message) # 去标点 print 'word Seg:' + message print '词性:' words = T.postag(input_message) if message == 'q': exit() else: response = mybot.respond(message) print "=======" print response print "=======" if response == "": ans = mybot.respond('找不到答案') print 'Eric:' + ans reply = mybot.respond('找不到答案') conn.sendall(reply) Flag = False # 百科搜索 elif response[0] == '#': # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') # 实体 entity = str(res[1]).replace(" ", "") # 属性 attr = str(res[2]).replace(" ", "") print entity + '<---->' + attr ans = baike.query(entity, attr) # 如果命中答案 if type(ans) == list: print 'Eric:' + QAT.ptranswer(ans, False) reply = QAT.ptranswer(ans, False) conn.sendall(reply) Flag = False continue elif ans.decode('utf-8').__contains__(u'::找不到'): # 百度摘要+Bing摘要 print "通用搜索" ans = search_summary.kwquery(input_message) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(input_message) if len(ans) == 0: ans = mybot.respond('找不到答案') print 'Eric:' + ans reply = ans conn.sendall(reply) Flag = False elif len(ans) > 1: print "不确定候选答案" print 'Eric: ' for a in ans: print a.encode("utf8") reply += a.encode("utf8") + '\n' conn.sendall(reply) Flag = False else: print 'Eric:' + ans[0].encode("utf8") reply = ans[0].encode("utf8") conn.sendall(reply) Flag = False # 匹配模版 else: print 'Eric:' + response reply = response conn.sendall(reply) Flag = False
def qa(question): #初始化jb分词器 T.jieba_initialize() #切换到语料库所在工作目录 mybot_path = './' # os.chdir(mybot_path) mybot = aiml.Kernel() mybot.learn(os.path.split(os.path.realpath(__file__))[0]+"/resources/std-startup.xml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/bye.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/tools.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/bad.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/funny.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/OrdinaryQuestion.aiml") mybot.learn(os.path.split(os.path.realpath(__file__))[0] + "/resources/Common conversation.aiml") # mybot.respond('Load Doc Snake') #载入百科属性列表 print ''' .----------------. .-----------------. .----------------. .----------------. .----------------. | .--------------. || .--------------. || .--------------. || .--------------. || .--------------. | | | _______ | || | ____ _____ | || | __ | || | ___ ____ | || | _________ | | | | / ___ | | || ||_ \|_ _| | || | / \ | || | |_ ||_ _| | || | |_ ___ | | | | | | (__ \_| | || | | \ | | | || | / /\ \ | || | | |_/ / | || | | |_ \_| | | | | '.___`-. | || | | |\ \| | | || | / /__\ \ | || | | __'. | || | | _| _ | | | | |`\____) | | || | _| |_\ |_ | || | _/ / \ \_ | || | _| | \ \_ | || | _| |___/ | | | | | |_______.' | || ||_____|\____| | || ||____| |____|| || | |____||____| | || | |_________| | | | | | || | | || | | || | | || | | | | '--------------' || '--------------' || '--------------' || '--------------' || '--------------' | '----------------' '----------------' '----------------' '----------------' '----------------' Eric:你好,我是Eric。╭(╯^╰)╮ ''' input_message = question if len(input_message) > 60: print mybot.respond("句子长度过长") elif input_message.strip() == '': print mybot.respond("无") print input_message message = T.wordSegment(input_message) # 去标点 print 'word Seg:'+ message print '词性:' words = T.postag(input_message) if message == 'q': exit() else: response = mybot.respond(message) print "=======" print response print "=======" if response == "": ans = mybot.respond('找不到答案') print 'Eric:' + ans # 百科搜索 elif response[0] == '#': # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') #实体 entity = str(res[1]).replace(" ","") #属性 attr = str(res[2]).replace(" ","") print entity+'<---->'+attr ans = baike.query(entity, attr) # 如果命中答案 if type(ans) == list: print 'Eric:' + QAT.ptranswer(ans,False) elif ans.decode('utf-8').__contains__(u'::找不到'): #百度摘要+Bing摘要 print "通用搜索" ans = search_summary.kwquery(input_message) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(input_message) if len(ans) == 0: ans = mybot.respond('找不到答案') print 'Eric:' + ans elif len(ans) >1: print "不确定候选答案" print 'Eric: ' for a in ans: print a.encode("utf8") else: print 'Eric:' + ans[0].encode("utf8") # 匹配模版 else: print 'Eric:' + response
def answer(question): if len(question) > 600: print mybot.respond("句子长度过长") raise Exception("Too Long") elif question.strip() == '': print mybot.respond("无") raise Exception("No Input") print question message = T.wordSegment(question) # 去标点 print 'word Seg:'+ message print '词性:' words = T.postag(question) if message == 'q': exit() else: response = mybot.respond(message) print response if response == "": raise Exception("No Answer") # 百科搜索 elif response[0] == '#': # 匹配百科 if response.__contains__("searchbaike"): print "searchbaike" print response res = response.split(':') #实体 entity = str(res[1]).replace(" ","") #属性 attr = str(res[2]).replace(" ","") print entity+'<---->'+attr ans = baike.query(entity, attr) # 如果命中答案 if type(ans) == list: print 'Eric:' + QAT.ptranswer(ans,False) return [QAT.ptranswer(ans,False)] elif ans.decode('utf-8').__contains__(u'::找不到'): #百度摘要+Bing摘要 print "通用搜索" ans = search_summary.kwquery(question) # 匹配不到模版,通用查询 elif response.__contains__("NoMatchingTemplate"): print "NoMatchingTemplate" ans = search_summary.kwquery(question) if len(ans) == 0: raise Exception("No Answer") elif len(ans) >1: print "不确定候选答案" print 'Eric: ' for a in ans: print a.encode("utf8") return [a.encode("utf8") for a in ans] else: print 'Eric:' + ans[0].encode("utf8") return [ans[0].encode("utf8")] # 匹配模版 else: print 'Eric:' + response return [response]