-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
427 lines (349 loc) · 17.9 KB
/
main.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from SPARQLWrapper import SPARQLWrapper,JSON
import rdflib
from owlready2 import *
from speech2Text import SpeechToText
from text2speech import Text2Speach as t2s
from statemachine import StateMachine, State
from random import *
import time
import _thread
import json
import random
itsSuitableForPublic = True
phyisical = False
class DialogManager(StateMachine):
#states
welcome = State('Welcome',initial = True)
basicData = State('BasicData')
randomSolution = State('RandomSolution')
randomSolutionSolving = State('RandomSolutionSolving')
askingFor = State('AskingFor')
askingForSecond = State('AskingForSecond')
askingForSolving = State('AskingForolving')
listenForSolution = State('ListenForSolution')
listening = State('Listening')
completed = State('Completed')
end = State('End')
#transitions
transBasicData = welcome.to(basicData)
transFromBasicDataToRandomSolution = basicData.to(randomSolution)
transFromBasicDataToAskingFor = basicData.to(askingFor)
transFromListenForSolution = basicData.to(listenForSolution)
transFromBasicDataToBasicData = basicData.to(basicData)
transFromBasicDataToEnd = basicData.to(end)
transFromAskingForToAskingForSecond = askingFor.to(askingForSecond)
transFromAskingForToAskingFor = askingFor.to(askingFor)
transFromAskingSecondToAskingForSecond = askingForSecond.to(askingForSecond)
transFromAskingSecondToAskingForSolving = askingForSecond.to(askingForSolving)
transFromAskingForSolvingToAskingForSolving = askingForSolving.to(askingForSolving)
transFromAskingForSolvingToCompleted = askingForSolving.to(completed)
transFromRandomSolutionToRandomSolution = randomSolution.to(randomSolution)
transFromRandomSolutionToBasicData = randomSolution.to(basicData)
transFromRandomSolutionToRandomSolutionSolving = randomSolution.to(randomSolutionSolving)
transFromRandomSolutionSolvingToRandomSolutionSolving = randomSolutionSolving.to(randomSolutionSolving)
transFromRandomSolutionSolvingToCompleted = randomSolutionSolving.to(completed)
transFromListenSolutionToListenSolution = listenForSolution.to(listenForSolution)
transFromLitenSolutionToBasicData = listenForSolution.to(basicData)
transFromListenSolutionToListening = listenForSolution.to(listening)
transFromListeningToListening = listening.to(listening)
transFromListeningToCompleted = listening.to(completed)
transFromCompletedToEnd = completed.to(end)
transFromCompletedToCompleted = completed.to(completed)
transFromCompletedToBasicData = completed.to(basicData)
def on_enter_basicData(self):
speak(typeOfMessage='askHowAreYou')
def on_enter_randomSolution(self):
speak(typeOfMessage='randomSolutionCheck')
def on_enter_randomSolutionSolving(self):
onto_searched = onto.search(techniqueName = "*")
randomSolution = randint(0,len(onto_searched))
print(randomSolution)
randomSolutionFound =onto_searched[randomSolution]
print(randomSolutionFound.techniqueName)
speakResult(randomSolutionFound.techniqueName[0],randomSolutionFound.techniqueName[0])
speakResult(randomSolutionFound.techniqueScript[0],randomSolutionFound.techniqueName[0]+"description")
speakResult("Was it helpful?", "helpful")
def on_enter_listenForSolution(self):
speak(typeOfMessage ='listenForProposalCheck')
def on_enter_listening(self):
speak(typeOfMessage = 'listening')
def on_enter_completed(self):
speak(typeOfMessage = 'completed')
def on_enter_askingFor(self):
speak(typeOfMessage = 'askingFor')
def on_enter_askingForSecond(self):
speak(typeOfMessage = 'askingForSecond')
def on_enter_askingForSolving(self):
speak(typeOfMessage = 'askingForSolving')
onto_searched = onto.search(techniqueName ="*")
for i in range(len(onto_searched)):
if onto_searched[i].physicalActivityInvolved[0] == (phyisical) and onto_searched[i].suitableForPublicEnv[0] == (itsSuitableForPublic) :
print(onto_searched[i].physicalActivityInvolved[0] == (phyisical), onto_searched[i].suitableForPublicEnv[0] == (itsSuitableForPublic))
speakResult(onto_searched[i].techniqueName[0],onto_searched[i].techniqueName[0])
speakResult(" Was it helpful?", "helpful")
def dataInitialization():
with open('server_questions.json') as f:
return json.load(f)
dm = DialogManager()
data = dataInitialization()
recognizer = SpeechToText()
onto = get_ontology("resources/root-ontology.owl").load()
def speech_thread (recognizer):
while recognizer.iterate == 1:
recognizer.getAudio()
print("++++++++++++++++++++++")
def getAudioTosay(typeOfMessage):
return data[typeOfMessage][random.randint(0,len(data[typeOfMessage])-1)]
def speak(typeOfMessage):
t2s.speak(sentence=getAudioTosay(typeOfMessage=typeOfMessage),fileName=str(time.time())+typeOfMessage)
def speakResult(msg,name):
t2s.speak(msg,name)
def main():
recognizer.iterate = 0
currentState = "noInitialized"
speak(typeOfMessage='welcome')
dm.transBasicData()
print(dm.current_state)
##_thread.start_new_thread(speech_thread,( recognizer,))
while dm.current_state != dm.end:
if dm.current_state == dm.basicData :
solution = 0
while solution == 0:
basicDataDoc = recognizer.getAudio()
print(basicDataDoc)
for token in basicDataDoc:
# print(token.text, token.pos_, token.dep_)
if token.text == "random" and token.dep_ == "amod":
print("he wants to make a random solution")
solution =1
elif token.text == "ask" or token.text == "asked":
print ( "he wants to be asked")
solution = 2
elif token.text == "repeat":
print( "he wants to repeat the sentence")
solution = -1
elif( token.text == "tell"):
print("tells a technique")
solution = 3
elif( token.text == "finish" or token.text == "done"):
print("exit")
solution = 4
if solution == 0 :
print( "didn't got anything")
speak(typeOfMessage='HasntUndestood')
if solution == 1:
dm.transFromBasicDataToRandomSolution()
elif solution == 2:
dm.transFromBasicDataToAskingFor()
elif solution == 3:
dm.transFromListenForSolution()
elif solution == -1:
dm.transFromBasicDataToBasicData()
elif solution == 4:
dm.transFromBasicDataToEnd()
if dm.current_state == dm.randomSolution:
random_agree = 0
while random_agree == 0:
listened = recognizer.getAudio()
for token in listened:
#print(token.text, token.pos_, token.dep_)
if token.text == "yes":
print("AGREE, SAID A RANDOM SOLUTION")
random_agree =1
elif token.text == "no":
print ( "DISAGREE, WASNT A RANDOM SOLUTION")
random_agree = 2
elif token.text == "repeat":
print( "REPEAT, REPEAT THE SENTENCE")
random_agree = -1
if random_agree == 0 :
print( "THE AGENT HASN'T UNDESTOOD")
speak(typeOfMessage='HasntUndestood')
if random_agree == 1:
speak(typeOfMessage='HasUndestood')
dm.transFromRandomSolutionToRandomSolutionSolving()
elif random_agree == 2:
dm.transFromRandomSolutionToBasicData()
elif random_agree == -1:
dm.transFromRandomSolutionToRandomSolution()
if dm.current_state == dm.randomSolutionSolving :
random_finished = 0
while random_finished == 0:
listened = recognizer.getAudio()
for token in listened:
#print(token.text, token.pos_, token.dep_)
if token.text == "yes":
print("USER SAID IT WAS HELPFUL")
random_finished =1
elif token.text == "no":
print ( "USER SAID IT WASNT HELPFUL")
random_finished = 2
elif token.text == "another":
print( "USER WANTS TO TRY ANOTHER RANDOM SOLUTION")
random_finished = -1
if random_finished == 0 :
print( "DIDN'T UNDESTOOD")
speak(typeOfMessage='HasntUndestood')
if random_finished == 1 :
dm.transFromRandomSolutionSolvingToCompleted()
if random_finished == 2 :
dm.transFromRandomSolutionSolvingToCompleted()
if random_finished == -1 :
dm.transFromRandomSolutionSolvingToRandomSolutionSolving()
if dm.current_state == dm.completed:
can_leave = 0
while can_leave == 0:
listened = recognizer.getAudio()
for token in listened:
#print(token.text, token.pos_, token.dep_)
if token.text == "yes":
print("USER SAID WE CAN DO OTHER THINGS")
can_leave =1
elif token.text == "no":
print ( "USER SAID WE CAN LEAVE")
can_leave = 2
elif token.text == "repeat":
print( "USER WANTS TO REPEAT THAT SENTENCE")
can_leave = -1
if can_leave == 0 :
print( "DIDN'T UNDESTOOD")
speak(typeOfMessage='HasntUndestood')
if can_leave == 1 :
dm.transFromCompletedToBasicData()
if can_leave == 2 :
dm.transFromCompletedToEnd()
if can_leave == -1 :
dm.transFromCompletedToCompleted()
if dm.current_state == dm.listenForSolution:
listen_agree = 0
while listen_agree == 0:
listened = recognizer.getAudio()
for token in listened:
#print(token.text, token.pos_, token.dep_)
if token.text == "yes":
print("LISTEN_AGREE")
listen_agree =1
elif token.text == "no":
print ( "LISTEN_DISAGREE")
listen_agree = 2
elif token.text == "repeat":
print( "LISTEN_REPEAT")
listen_agree = -1
if listen_agree == 0 :
print( "LISTEN_DIDN'T UNDESTOOD")
speak(typeOfMessage='HasntUndestood')
if listen_agree == 1:
dm.transFromListenSolutionToListening()
elif listen_agree == 2:
dm.transFromLitenSolutionToBasicData()
elif listen_agree == -1:
dm.transFromListenSolutionToListenSolution()
if dm.current_state == dm.listening:
proposal_told = 0
while proposal_told == 0:
listened = recognizer.getAudio()
print(str(listened).lower())
onto_searched = onto.search(techniqueName = str(listened).lower())
print(onto_searched)
for i in range(len(onto_searched)):
print(onto_searched[0].techniqueName[0].lower())
speakResult(onto_searched[0].techniqueName[0],onto_searched[0].techniqueName[0])
speakResult(onto_searched[0].techniqueScript[0],onto_searched[0].techniqueName[0]+"description")
proposal_told = 1
if len(listened) > 0:
print(listened)
if proposal_told == 0 :
print( "LISTEN_DIDN'T UNDESTOOD")
speak(typeOfMessage='HasntUndestood')
if proposal_told == 1:
dm.transFromListeningToCompleted()
elif proposal_told == 2:
dm.transFromLitenSolutionToBasicData()
elif proposal_told == -1:
dm.transFromListenSolutionToListenSolution()
if dm.current_state == dm.askingFor:
solution = 0
while solution == 0:
listened = recognizer.getAudio()
print(listened)
for token in listened:
# print(token.text, token.pos_, token.dep_)
if token.text == "yes":
print("USER WANTS A PUBLIC SUITABLE SOLUTION")
solution =1
elif token.text == "no":
print ( "USER WANTS A PRIVATE SOLUTION")
solution = -1
elif token.text == "repeat":
print( "he wants to repeat the sentence")
solution = -2
if solution == 0 :
print( "didn't got anything")
speak(typeOfMessage='HasntUndestood')
if solution == 1:
dm.transFromAskingForToAskingForSecond()
itsSuitableForPublic = True
elif solution == -1:
dm.transFromAskingForToAskingForSecond()
itsSuitableForPublic = False
elif solution == -2:
dm.transFromAskingForToAskingFor()
if dm.current_state == dm.askingForSecond:
solution = 0
while solution == 0:
listened = recognizer.getAudio()
print(listened)
for token in listened:
# print(token.text, token.pos_, token.dep_)
if token.text == "yes":
print("USER WANTS A PHYSICAL SOLUTION")
solution =1
elif token.text == "no":
print ( "USER WANTS A STATIC SOLUTION")
solution = -1
elif token.text == "repeat":
print( "he wants to repeat the sentence")
solution = -2
if solution == 0 :
print( "didn't got anything")
speak(typeOfMessage='HasntUndestood')
if solution == 1:
dm.transFromAskingSecondToAskingForSolving()
phyisical = True
elif solution == -1:
dm.transFromAskingSecondToAskingForSolving()
phyisical = False
elif solution == -2:
dm.transFromAskingForToAskingFor()
if dm.current_state == dm.askingForSolving:
solution = 0
while solution == 0:
listened = recognizer.getAudio()
print(listened)
for token in listened:
# print(token.text, token.pos_, token.dep_)
if token.text == "yes":
print("WAS USEFUL")
solution =1
elif token.text == "no":
print ( "WASNT USEFUL")
solution = -1
elif token.text == "repeat":
print( "he wants to repeat the sentence")
solution = -2
if solution == 0 :
print( "didn't got anything")
speak(typeOfMessage='HasntUndestood')
if solution == 1:
dm.transFromAskingForSolvingToCompleted()
elif solution == -1:
dm.transFromAskingForSolvingToCompleted()
elif solution == -2:
dm.transFromAskingForSolvingToCompleted()
speak(typeOfMessage = "GoodBye")
if __name__ == '__main__':
import sys
main()