-
Notifications
You must be signed in to change notification settings - Fork 12
/
testWitAI
186 lines (157 loc) · 6.9 KB
/
testWitAI
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import wit
import os
import json
from wit import Wit
'''
Initializate wit
'''
#access_token = 'NUFTF6VJH6EC3BN5S2EK2STCPYGKPPNR'
access_token = '3LH5Q3E3KEIYYVINQKBLOL2QGAIQ2IRG'
def say(session_id, context, msg):
print(msg)
def merge(session_id, context, entities, msg):
return context
def error(session_id, context, e):
print(str(e))
actions = {
'say': say,
'merge': merge,
'error': error,
}
client = Wit(access_token, actions)
responses=[]
'''
Prepare testing sentences with expected responses
'''
sentences = {
'Is there a storm coming': {'intent': 'weather', 'entities': {'weather_type': 'storm'}},
'When does the sun set today': {'intent': 'weather', 'entities': {'datetime': None, 'weather_type': 'sunset'}},
'Will it rain on Monday in New York': {'intent': 'weather',
'entities': {'location': 'New York', 'weather_type': 'rain',
'datetime': None}},
'What will be the temperature during weekend': {'intent': 'weather',
'entities': {'weather_type': 'temperature', 'datetime': None}},
'Will it be sunny on Thursday in Ostrava': {'intent': 'weather',
'entities': {'location': 'Ostrava', 'datetime': None, 'weather_type': 'cloudy'}},
'Will there be a new moon on Tuesday': {'intent': 'weather',
'entities': {'weather_type': 'moonphase', 'datetime': None}},
'Tell me a joke': {'intent': 'agenda', 'entities': {'agenda_en': 'joke'}},
'How is the weather today': {'intent': 'weather', 'entities': {'datetime': None}},
'Should I take an umbrella': {'intent': 'weather', 'entities': {'weather_type': 'rain'}},
'Is it cold outside': {'intent': 'weather', 'entities': {'location': 'outside', 'weather_type': 'temperature'}},
'How is the wind outside': {'intent': 'weather', 'entities': {'location': 'outside', 'weather_type': 'windspeed'}},
'Is it raining outside': {'intent': 'weather', 'entities': {'location': 'outside', 'weather_type': 'rain'}},
'Is it snowing in Brno': {'intent': 'weather', 'entities': {'location': 'Brno', 'weather_type': 'snow'}},
'Is it cloudy in Liberec': {'intent': 'weather', 'entities': {'location': 'Liberec', 'weather_type': 'cloudy'}},
'What is the weather forecast for the next week': {'intent': 'weather', 'entities': {'datetime': None}},
'Will the sun shine on Thursday': {'intent': 'weather', 'entities': {'datetime': None, 'weather_type': 'cloudy'}},
'When will the sun rise on Monday': {'intent': 'weather',
'entities': {'datetime': None, 'weather_type': 'sunrise'}},
'What is the weather in London': {'intent': 'weather', 'entities': {'location': 'London'}},
'What is the temperature outside': {'intent': 'weather',
'entities': {'location': 'outside', 'weather_type': 'temperature'}},
'What moon phase will be on Wednesday': {'intent': 'weather',
'entities': {'weather_type': 'moonphase', 'datetime': None}},
'What will be the weather during the weekend': {'intent': 'weather', 'entities': {'datetime': None}},
}
wrongSentences = [
'Where is my car',
'What is the time',
'When is the dinner'
'Where is London',
'What homeworks do I have',
'What are the latest news',
'What will happen on Thursday',
'What color is my house',
'Where is she from',
'Who are you',
'Is the meal ready',
'Turn on the heat',
'You have been terminated',
'Tell me something',
'Is there any food left',
'Do my homework for me',
'Who is the queen of Ireland',
'How big is the sun',
'How far is the Moon',
'What is the rain',
'How cold is snow',
'Talk to me',
'Where is Paris',
'What is the capital of Russia',
'Destroy all humans on wednesday',
'Raise the temperature in house in the morning',
'What is you opinion on current economical crisis',
'Do you think I look good in the new dress I bought',
'I would like to build a swing outside, in the garden',
]
def testSentence(message,expOutcome):
res={'intent':None,'entities':None,'confidence':None}
resp = client.message(message)
responses.append(resp)
#print(str(resp['outcomes'][0]['intent']))
print(str(resp['_text']))
#print(str(resp['outcomes'][0]['confidence']))
res['confidence'] = resp['outcomes'][0]['confidence']
if expOutcome['intent'] == resp['outcomes'][0]['intent']:
res['intent'] = 'Ok'
else:
res['intent'] = 'Bad'
print('Bad intent')
print(resp['outcomes'][0]['intent'])
if expOutcome['entities'].keys() == resp['outcomes'][0]['entities'].keys():
res['entities']='Ok'
for key, value in expOutcome['entities'].items():
if not(key=='datetime') and (not value == resp['outcomes'][0]['entities'][key][0]['value']):
res['entities']='Bad'
print('Bad value')
print(resp['outcomes'][0]['entities'][key][0]['value'])
print(value)
else:
res['entities'] = 'Bad'
print(resp['outcomes'][0]['entities'].keys())
print(expOutcome['entities'].keys())
return res
#Go through the test sentences and try them
for key, value in sentences.items():
result=testSentence(key,value)
if (result['intent'] == 'Ok') and (result['entities'] == 'Ok'):
print('Ok')
else:
print('Mistake')
print(result)
print(result['confidence'])
for sentence in wrongSentences:
resp = client.message(sentence)
responses.append(resp)
print(str(resp['_text']))
print(str(resp['outcomes'][0]['intent']))
print(str(resp['outcomes'][0]['confidence']))
print(str(resp['outcomes'][0]['entities'].keys()))
'''
Try the responses from wit in queryModule
'''
#Initializate the queryModules
currentDir=os.path.dirname(os.path.abspath(__file__))
desiredDir=os.path.join(currentDir,'modules','querylogic','modules')
moduleNames=os.listdir(desiredDir)
moduleNames=['modules.querylogic.modules.' + s for s in moduleNames]
moduleNames = [ name for name in moduleNames if not name.startswith('modules.querylogic.modules.__') ]
modules=[]
print(moduleNames)
print(desiredDir)
for i in range(0,len(moduleNames)):
moduleNames[i]=moduleNames[i][:-3]
print(moduleNames[i])
modules.append(__import__(moduleNames[i], fromlist=['']))
moduleInst=[]
for module in modules:
initModule=module.init_hook()
moduleInst.append(initModule)
params={'city':'Prague'}
#Feed the wit.ai responses icnto querymodule and print the answer
for wit in responses:
for module in moduleInst:
answer=module.query_resolution(wit['outcomes'][0]['intent'], wit['outcomes'][0], params)
if(answer!='query not recognised'):
print(answer)