forked from SnakeHacker/QA-Snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqa.py
116 lines (95 loc) · 4.31 KB
/
qa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#coding:utf8
import aiml
import os
from QA.QACrawler import baike
from QA.Tools import Html_Tools as QAT
from QA.Tools import TextProcess as T
from QACrawler import search_summary
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