This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanother_dialogue.py
367 lines (290 loc) · 11.5 KB
/
another_dialogue.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
# %%
import random
import nltk
nltk.download('stopwords')
nltk.download('punkt')
from nltk.corpus import stopwords
from PizzaConfig import PizzaConfig
import speech_recognition as sr
import pyttsx3
from another_constants import PARSER, BODENPARSER, ALLESPARSER, MENUEPARSER, SORTEN
# %%
def generate_custom_stop_set():
"""Generates and returns custom german set of stopwords"""
# get stopwords
german_stop_words = stopwords.words('german')
german_stop_words.extend(('sie', 'bitte', 'guten', 'morgen', 'möchte', 'hätte', 'hallo', 'bestellen', 'her', 'gib',
'mir', 'ne', 'will', 'drauf', 'darauf', 'danke', 'gerne', 'können', 'zeigen', 'nehmen',
'sehen', 'nehme', 'tag', 'lieber'))
# make it a set to be faster
german_stop_set = set(german_stop_words)
# remove some needed stopwords from set
needed_words = ['mit', 'ohne', 'kein', 'extra', 'nicht', 'oder']
german_stop_set = [word for word in german_stop_set if word not in needed_words]
return german_stop_set
def rand_greeting():
"""Generates a random greeting from two given array and returns that greeting as string."""
greetings = ['Guten Morgen', 'Hallo']
question = [', was kann ich fuer Sie tun?', '!']
return random.choice(greetings) + random.choice(question)
def rand_farewell():
"""Generates a random farewell from a given array and returns that farewell as string."""
farewells = ['Ihre Pizza wird in 5 Minuten geliefert!', 'Gut, kommt.',
'Die Pizza wird Ihnen in 5 Minuten geliefert.', 'Danke fuer Ihre Bestellung']
return random.choice(farewells)
def rand_product():
"""Generates a random question from a given array and returns that question as string."""
nachfrage = ["Was moechten Sie denn?", "Was wollen Sie bestellen? Wir haben Pizza, Pizzabroetchen und Calzone."]
return random.choice(nachfrage)
def rand_sorte():
"""Generates a random question from a given array and returns that question as string."""
nachfrage = ["Was fuer eine Sorte wollen Sie denn?", "Welche Sorte haetten Sie denn gerne?",
"Was fuer eine?"]
return random.choice(nachfrage)
def rand_alles():
"""Generates a random question from a given array and returns that question as string."""
nachfrage = ["Haben Sie noch einen Wunsch?", "Kann ich noch etwas fuer Sie tun?",
"Noch etwas?"]
return random.choice(nachfrage)
def rand_boden():
"""Generates a random question from a given array and returns that question as string."""
boden = ["Wuenschen Sie einen duennen, dicken oder normalen Boden?", "Okay, duenner oder dicker Boden?"]
return random.choice(boden)
def my_listen(source, engine, r):
"""STT: Listens to microphone and returns recognized string."""
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
recognized = ""
try:
recognized = r.recognize_google(audio, language="de-de")
except:
engine.say("Entschuldigung, das habe ich nicht verstanden. Bitte wiederholen Sie, was Sie gesagt haben.")
engine.runAndWait()
recognized = my_listen(source, engine, r)
print(recognized)
return recognized
def string_works(string_input, stop_set):
"""Convert string to lower case, tokenize words, remove words from stop-set, return list of words"""
string_input = string_input.lower()
string_tokenized = nltk.word_tokenize(string_input)
tokenized_output = [word for word in string_tokenized if word not in stop_set]
return tokenized_output
def analyse(text, pizza, engine, source, r, german_stop_set):
"""Parse text and add features to pizza-object, if relevant information is given.
Open menu-dialogue if asked for."""
trees = PARSER.parse(text)
bigram = []
for tree in trees:
bigram.append(tree.pos())
print(bigram)
for unnoetig in bigram:
for pos in unnoetig:
if pos[1] == "PRODUCT":
pizza.set_product(pos[0])
if pos[1] == "SORTE":
pizza.set_sorte(pos[0])
if pos[1] == "MBELAG":
pizza.set_extra(pos[0])
if pos[1] == "OBELAG":
pizza.set_out(pos[0])
if pos[1] == "BODENART":
pizza.set_boden(pos[0])
if pos[1] == "MENUE":
menue_dialog(engine, source, r, german_stop_set)
if pos[1] == "ODER":
op1 = ""
op2 = ""
for pos1 in unnoetig:
if pos1[1] == "PRODUCT1":
op1 = pos1[0]
if pos1[1] == "PRODUCT2":
op2 = pos1[0]
engine.say("Moechten Sie lieber " + op1 + " oder " + op2 + "?")
engine.runAndWait()
print("Moechten Sie lieber " + op1 + " oder " + op2 + "?")
satz = my_listen(source, engine, r)
satz = string_works(satz, german_stop_set)
analyse(satz, pizza, engine, source, r, german_stop_set)
def analyse_boden(text, pizza):
"""Parse text and add Boden-feature to pizza-object, if relevant information is given."""
trees = BODENPARSER.parse(text)
bigram = []
for tree in trees:
bigram.append(tree.pos())
for unnoetig in bigram:
for pos in unnoetig:
if pos[1] == "BODEN":
pizza.set_boden = pos[0]
def analyse_alles(text, pizza):
"""Parse text and return 0 if customer is satisfied, 1 if customer wants to add anything to order."""
trees = ALLESPARSER.parse(text)
bigram = []
alles = 0
for tree in trees:
bigram.append(tree.pos())
for unnoetig in bigram:
for pos in unnoetig:
if pos[1] == "JA":
alles = 0
if pos[1] == "NEIN":
alles = 0
if pos[1] == "MB":
pizza.set_extra(pos[0])
alles = 1
if pos[1] == "OB":
pizza.set_out(pos[0])
alles = 1
return alles
def analyse_menue(text):
"""Parse text and return relevant menu-options"""
trees = MENUEPARSER.parse(text)
bigram = []
for tree in trees:
bigram.append(tree.pos())
menue = []
for unnoetig in bigram:
for pos in unnoetig:
if pos[1] == "VEG":
# show veggie menue
for sorte in SORTEN:
if sorte[0] == 0:
menue.append(sorte[1])
if pos[1] == "MB":
# show menue with topping
for sorte in SORTEN:
if pos[0] in sorte:
menue.append(sorte[1])
if pos[1] == "OB":
# show menue without topping
for sorte in SORTEN:
if pos[0] not in sorte:
menue.append(sorte[1])
if pos[1] == "NEIN":
for sorte in SORTEN:
menue.append(sorte)
return menue
def check_complete(pizza):
"""returns an Array of Boolean Values according to set Variables in given PizzaConfig Object"""
product = False
sorte = False
boden = False
if (pizza.get_sorte() != ''):
sorte = True
if (pizza.get_boden() != 'normal'):
boden = True
if (pizza.get_product() != ''):
product = True
return [sorte, boden, product]
def menue_dialog(engine, source, r, german_stop_set):
"""Ask for preferences for menu, call analyse_menue(), give relevant options from return value"""
engine.say("Haben Sie besondere Vorlieben?")
engine.runAndWait()
print("Haben Sie besondere Vorlieben?")
satz = my_listen(source, engine, r)
satz = string_works(satz, german_stop_set)
menue = analyse_menue(satz)
separator = ", "
try:
menue = separator.join(menue)
except:
engine.say("So etwas haben wir leider nicht. Versuchen Sie es mit etwas anderem.")
engine.runAndWait()
menue_dialog(engine, source, r, german_stop_set)
engine.say("Da haben wir " + menue + ".")
engine.runAndWait()
print("Da haben wir " + menue + ".")
def say_begruessung(engine):
"""Say greeting from randomised greetings"""
greeting = rand_greeting()
engine.say(greeting)
print(greeting)
engine.runAndWait()
def ask_product(engine):
"""Ask for product - randomised question"""
nachfrage = rand_product()
engine.say(nachfrage)
print(nachfrage)
engine.runAndWait()
def ask_sorte(engine):
"""Ask for type - randomised question"""
nachfrage = rand_sorte()
engine.say(nachfrage)
print(nachfrage)
engine.runAndWait()
def ask_boden(engine):
"""Ask for base - randomised question"""
boden = rand_boden()
engine.say(boden)
print(boden)
engine.runAndWait()
def ask_alles(engine):
"""Ask if customer is done - randomised question"""
alles = rand_alles()
engine.say(alles)
print(alles)
engine.runAndWait()
def say_kommt(engine):
"""Say farewell from randomised farewells"""
farewell = rand_farewell()
engine.say(farewell)
print(farewell)
engine.runAndWait()
def main():
# initialise text-to-speech-engine
engine = pyttsx3.init()
engine.runAndWait()
# initialise speech-recognition
r = sr.Recognizer()
german_stop_set = generate_custom_stop_set()
running = 1
say_begruessung(engine)
while (running):
with sr.Microphone() as source:
# initialise pizza object
pizza = PizzaConfig.PizzaConfig()
# listen for initial order
satz = my_listen(source, engine, r)
satz = string_works(satz, german_stop_set)
# analyse order
analyse(satz, pizza, engine, source, r, german_stop_set)
complete = check_complete(pizza)
# debug print
print(complete)
# check if product is set and if not ask for it
while (complete[2] == False):
ask_product(engine)
satz = my_listen(source, engine, r)
satz = string_works(satz, german_stop_set)
analyse(satz, pizza, engine, source, r, german_stop_set)
complete = check_complete(pizza)
# if type and product were given, ask for base
if (complete[0] == True and pizza.get_product() == 'Pizza'):
ask_boden(engine)
satz = my_listen(source, engine, r)
satz = string_works(satz, german_stop_set)
analyse_boden(satz, pizza)
# check if type is set and if not ask for it
while (complete[0] == False):
ask_sorte(engine)
satz = my_listen(source, engine, r)
satz = string_works(satz, german_stop_set)
analyse(satz, pizza, engine, source, r, german_stop_set)
complete = check_complete(pizza)
# does the customer want to add anything? yes -> change and reask, no -> go on
running = 1
while(running):
ask_alles(engine)
satz = my_listen(source, engine, r)
satz = string_works(satz, german_stop_set)
running = analyse_alles(satz, pizza)
say_kommt(engine)
# debug print
product = pizza.get_product()
sorte = pizza.get_sorte()
extra = pizza.get_extra()
out = pizza.get_out()
boden = pizza.get_boden()
print(product, sorte, extra, out, boden)
# %%
main()
# %%