-
Notifications
You must be signed in to change notification settings - Fork 3
/
Audio.py
373 lines (343 loc) · 19.2 KB
/
Audio.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
#input: voice
#output: dictionary orders {order, quantity}
#uses pyaudio to record sound to current directory
#uses AWS to move file to s3 storage
#uses AWS to transcribe audio stored in AWS s3 storage to text
#converts transcription to order
from __future__ import print_function
import time
import boto3
from boto3.s3.transfer import S3Transfer
import urllib
import json
import os
# import Audio_record
#TODO PER RUN:
#TODO 1. add keys
#TODO 2. update job uri
class Audio:
#get transcript of input
@staticmethod
def getTranscript():
#Record audio
# Audio_record.record_audio()
#TODO credentials
access_key = ''
key_id = ''
#set up AWS transcribe
transcribe = boto3.client('transcribe',
region_name='us-east-2',
aws_secret_access_key =access_key,
aws_access_key_id = key_id)
client = boto3.client('s3', 'us-west-2',
aws_secret_access_key =access_key,
aws_access_key_id = key_id)
transfer = S3Transfer(client)
transfer.upload_file('Recording1.wav', 'sound-joelmussell', 'Recording.wav')
#TODO
job_name = "Transcribe_1112"
job_uri = "https://s3.us-east-2.amazonaws.com/sound-joelmussell/Recording.wav"
#start transcription
transcribe.start_transcription_job(
TranscriptionJobName=job_name,
Media={'MediaFileUri': job_uri},
MediaFormat='wav',
LanguageCode='en-US'
)
#wait for AWS to respond
while True:
status = transcribe.get_transcription_job(TranscriptionJobName=job_name)
if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']:
break
print("AWS magicking...")
time.sleep(5)
#record transcribe output
text = transcribe.get_transcription_job(TranscriptionJobName=job_name)
#open json reults file and extract information
jsonText = urllib.request.urlopen(text['TranscriptionJob']['Transcript']['TranscriptFileUri']).read().decode('utf-8')
jsonDict = json.loads(jsonText)
#print(jsonDict['results'])
transcript = jsonDict['results']['transcripts'][0]['transcript']
#index = jsonDict.find('\"transcript\"') + 14
#transcript = transcript[index:]
#index = transcript.find('\"')
#transcript = transcript[:index]
#os.remove("Recording1.wav")
transcribe.delete_transcription_job(TranscriptionJobName=job_name)
'''
print(transcript)
'''
return transcript
#process transcript
#returns dict() with order
@staticmethod
def getOrder(transcript):
puctuation = {",", ".", "'", "?", "!"}
lowercase = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
#return value
order = {}
num_order_list = ["number 1", "====", "====",
"number 2", "number 5", "number 7", "====",
"number 4", "number 8", "number 6", "===="]
number_order_list = ["number one", "====", "====",
"number two", "number five", "number seven", "nugget",
"number four", "number eight", "number six", "===="]
#potential input values searched by program stored in parellel arrays in reverse order of processing
third_order_list = ["sandwich", "deluxe", "spicy chicken",
"====", "grilled chicken", "club", "number three",
"chicken strip", "wrap", "grilled nugget", "====", "mini",
"====", "bacon egg and cheese biscuit", "sausage egg and cheese biscuit",
"====", "multigrain bagel", "hash brown", "yogurt",
"fruit", "chicken egg and cheese bagel", "====",
"====", "muffin", "====",
"====", "====", "fries", "====",
"noodle", "tortilla", "super food", "apple sauce",
"====", "====", "slaw", "====", "chip",
"nugget kids", "kids chicken strip", "====",
"milkshake", "====","====",
"milk shake", "====", "====", "====",
"ice cream", "key lime", "sweet", "lemonade",
"coca cola", "doctor pepper", "water", "====",
"====","chocolate milk", "milk", "coffee", "====",
"====", "====", "iced tea",
"====", "====", "====", "====",
"====", "====", "===="]
#
alternative_order_list = ["chicken sandwich", "deluxe sandwich", "spicy chicken",
"spicy deluxe", "grilled chicken", "club", "====",
"====", "wrap", "====", "====", "mini",
"====", "bacon egg and cheese biscuit", "sausage egg and cheese biscuit",
"butter biscuit", "sunflower bagel", "====", "yogurt",
"fruit", "chicken egg and cheese bagel", "hash brown burrito",
"hash brown bowl", "====", "bacon egg and cheese muffin",
"sausage egg and cheese muffin", "====", "fries", "====",
"noodle soup", "tortilla soup", "superfood", "apple sauce",
"====", "====", "slaw", "cron bread", "chip",
"====", "chicken strip kids", "grilled nugget kids",
"chocolate milk shake", "cookies and cream milk shake", "strawberry milk shake",
"vanilla milk shake", "====", "====", "cookie",
"ice-cream", "key lime", "====", "lemonade",
"coke", "doctor pepper", "water", "====",
"====","chocolate milk", "====", "coffee", "====",
"====", "====", "non sweet",
"chick-fil-a sauce", "====", "====", "====",
"====", "barbeque", "===="]
#
order_list = ["chick fil 1 sandwich", "====", "spicy chicken sandwich",
"spicy deluxe sandwich", "grilled chicken sandwich", "grilled chicken club", "====",
"====", "grilled cool wrap", "grilled chicken nugget", "chicken biscuit", "chicken mini",
"egg white grill", "bacon, egg and cheese biscuit", "sausage, egg and cheese biscuit",
"buttered biscuit", "sunflower multigrain bagel", "====", "greek yogurt parfait",
"fruit cup", "chicken, egg and cheese bagel", "hash brown scramble burrito",
"hash brown scramble bowl", "english muffin", "bacon, egg and cheese muffin",
"sausage, egg and cheese muffin", "cobb salad", "waffle potato fries", "side salad",
"chicken noodle soup", "chicken tortilla soup", "superfood side", "apple sauce",
"carrot raisin salad", "chicken salad", "cole slaw", "cornbread", "potato chip",
"kids nugget", "kids chicken strip", "kids grilled nugget",
"chocolate milkshake", "cookies and cream milkshake", "strawberry milkshake",
"vanilla milkshake", "frosted coffee", "frosted lemonade", "chocolate chunk cookie",
"ice dream cone", "key lime", "====", "====",
"coca-cola", "dr pepper", "bottled water", "apple juice",
"orange", "====", "white milk", "====", "iced coffee",
"gallon", "diet lemonade", "unsweet",
"chick fil 1 sauce", "polynesian", "honey mustard", "ranch",
"buffalo", "barbecue", "sriracha"]
#tag values to replace serched values
orders = ["CHICKEN=SANDWICH", "DELUXE=SANDWICH", "SPICY=CHICKEN=SANDWICH",
"SPICY=DELUXE=SANDWICH", "GRILLED=CHICKEN=SANDWICH", "GRILLED=CHICKEN=CLUB", "NUGGETS",
"CHICK-N-STRIPS", "GRILLED=COOL=WRAP", "GRILLED=NUGGETS", "CHICKEN=BISCUIT", "CHICK-N-MINIS",
"EGG=WHITE=GRILL", "BACON=EGG=&=CHEESE=BISCUIT", "SAUSAGE=EGG=&=CHEESE=BISCUIT",
"BUTTERED=BISCUIT", "SUNFLOWER=MULTIGRAIN=BAGEL", "HASH=BROWNS", "GREEK=YOGURT=PARFAIT",
"FRUIT=CUP", "CHICKEN=EGG=&=CHEESE=BAGEL", "HASH=BROWN=SCRAMBLE=BURRITO",
"HASH=BROWN=SCRAMBLE=BOWL", "ENGLISH=MUFFIN", "BACON=EGG=&=CHEESE=MUFFIN",
"SAUSAGE, EGG=&=CHEESE=MUFFIN", "COBB=SALAD", "WAFFLE=POTATO=FRIES", "SIDE=SALAD",
"CHICKEN=NOODLE=SOUP", "CHICKEN=TORTILLA=SOUP", "SUPERFOOD=SIDE", "BUDDY'S=APPLE=SAUCE",
"CARROT=RAISIN=SALAD", "CHICKEN=SALAD", "COLE=SLAW", "CORNBREAD", "WAFFLE=POTATO=CHIPS",
"NUGGET=KID'S=MEAL", "CHICK-N-STRIPS=KID'S=MEAL", "GRILLED=NUGGETS=KID'S=MEAL",
"CHOCOLATE=MILKSHAKE", "COOKIES=&=CREAM=MILKSHAKE", "STRAWBERRY=MILKSHAKE",
"VANILLA=MILKSHAKE", "FROSTED=COFFEE", "FROSTED=LEMONADE", "CHOCOLATE=CHUNK=COOKIE",
"ICEDREAM=CONE", "FROSTED=KEY=LIME", "FRESHLY-BREWED=ICED=TEA=SWEETENED", "LEMONADE",
"COCA-COLA", "DR=PEPPER", "DASANI=BOTTLED=WATER", "HONEST=KIDS=APPLE=JUICE",
"SIMPLY=ORANGE", "1%=CHOCOLATE=MILK", "1%=WHITE=MILK", "COFFEE", "ICED=COFFEE",
"GALLON=BEVERAGES", "CHICK-FIL-A=DIET=LEMONADE", "FRESHLY-BREWED=ICED=TEA=UNSWEETENED",
"CHICK-FIL-A=SAUCE", "POLYNESIAN=SAUCE", "HONEY=MUSTARD=SAUCE", "GARDEN=HERB=RANCH=SAUCE",
"ZESTY=BUFFALO=SAUCE", "BARBEQUE=SAUCE", "SRIRACHA=SAUCE"]
#tag values for drinks
drinks = ["CHOCOLATE=MILKSHAKE", "COOKIES=&=CREAM=MILKSHAKE", "STRAWBERRY=MILKSHAKE",
"VANILLA=MILKSHAKE", "FROSTED=COFFEE", "FROSTED=LEMONADE", "CHOCOLATE=CHUNK=COOKIE",
"ICEDREAM=CONE", "FROSTED=KEY=LIME", "FRESHLY-BREWED=ICED=TEA=SWEETENED", "LEMONADE",
"COCA-COLA", "DR=PEPPER", "DASANI=BOTTLED=WATER", "HONEST=KIDS=APPLE=JUICE",
"SIMPLY=ORANGE", "1%=CHOCOLATE=MILK", "1%=WHITE=MILK", "COFFEE", "ICED=COFFEE",
"GALLON=BEVERAGES", "CHICK-FIL-A=DIET=LEMONADE", "FRESHLY-BREWED=ICED=TEA=UNSWEETENED"]
side_output = ["Waffle Potato Fries", "Waffle Potato Fries", "Waffle Potato Fries",
"Waffle Potato Fries", "Waffle Potato Fries", "Waffle Potato Fries", "Waffle Potato Fries",
"Waffle Potato Fries", "Waffle Potato Fries", "Waffle Potato Fries", "Hash Browns", "Hash Browns",
"Hash Browns", "Hash Browns", "Hash Browns",
"Hash Browns", "Hash Browns", "====", "Hash Browns",
"Hash Browns", "Hash Browns", "Hash Brown",
"Hash Brown", "English Muffin", "Hash Browns",
"Hash Browns", "====", "====", "====",
"====", "====", "====", "====",
"====", "====", "====", "====", "====",
"Waffle Potato Fries", "Waffle Potato Fries", "Waffle Potato Fries",
"====", "====", "====",
"====", "====", "====", "====",
"====", "====", "====", "====",
"====", "====", "====", "====",
"====", "====", "====", "====", "====",
"====", "====", "====",
"====", "====", "====", "====",
"====", "====", "===="]
#output array
order_output = ["Chicken Sandwich", "Deluxe Sandwich", "Spicy Chicken Sandwich",
"Spicy Deluxe Sandwich", "Grilled Chicken Sandwich", "Grilled Chicken Club", "Nuggets",
"Chick-n-Strips", "Grilled Cool Wrap", "Grilled Nuggets", "Chicken Biscuit", "Chick-n-Minis",
"Egg White Grill", "Bacon, Egg & Cheese Biscuit", "Sausage, Egg & Cheese Biscuit",
"Buttered Biscuit", "Sunflower Multigrain Bagel", "Hash Browns", "Greek Yogurt Parfait",
"Fruit Cup", "Chicken, Egg & Cheese Bagel", "Hash Brown Scramble Burrito",
"Hash Brown Scramble Bowl", "English Muffin", "Bacon, Egg & Cheese Muffin",
"Sausage, Egg & Cheese Muffin", "Cobb Salad", "Waffle Potato Fries", "Side Salad",
"Chicken Noodle Soup", "Chicken Tortilla Soup", "Superfood Side", "Buddy's Apple Sauce",
"Carrot Raisin Salad", "Chicken Salad", "Cole Slaw", "Cornbread", "Waffle Potato Chips",
"Nugget Kid's Meal", "Chick-n-Strips Kid's Meal", "Grilled Nuggets Kid's Meal",
"Chocolate Milkshake", "Cookies & Cream Milkshake", "Strawberry Milkshake",
"Vanilla Milkshake", "Frosted Coffee", "Frosted Lemonade", "Chocolate Chunk Cookie",
"Icedream Cone", "Frosted Key Lime", "Freshly-Brewed Iced Tea Sweetened", "Lemonade",
"Coca-Cola", "Dr Pepper", "DASANI Bottled Water", "Honest Kids Apple Juice",
"Simply Orange", "1% Chocolate Milk", "1% White Milk", "Coffee", "Iced Coffee",
"Gallon Beverages", "Chick-fil-A Diet Lemonade", "Freshly-Brewed Iced Tea Unsweetened",
"Chick-fil-A Sauce", "Polynesian Sauce", "Honey Mustard Sauce", "Garden Herb Ranch Sauce",
"Zesty Buffalo Sauce", "Barbeque Sauce", "Sriracha Sauce"]
#quantities array
quantities = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
num_quantities = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
QUANTITIES = ["ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"]
operators = ["meal", "male", "combo"]
OPERATORS = ["MEAL", "MEAL", "MEAL"]
#preliminary translate
translate = [" suraj a ", " suraj ", " sake ", " ship", " fry ", " grill ", " girl ", " rap ", " toe luck ", " multi green ", " multi grain ", " sun flower ",
" sunfire ", " flour ", " some ", " son ", " basil ", " babel ", " scrambled ", " browns ", " agon ", " to ", " too ", " ate ",
" for ", " a ", " an ", " the ", " has ground ", " cara reason ", " nuggets ", " strips ", " kid ", " chicken nugget ",
" ice ", "sweet iced tea ", " source ", " checking strap ", " the luck ", " com ", " Minnie", " uh ", " um ", " suite ", " no get "
" chicken and ", " chick and ", " holland asian ", " sarasota ", " free "]
TRANSLATE = [" sriracha ", " sriracha ", " shake ", " chip", " fries ", " grilled ", " grilled ", " wrap ", " deluxe ", " multigrain ", " multigrain ", " sunflower ",
" sun flower ", " flower ", " sun ", " sun ", " bagel ", " bagel ", " scramble ", " brown ", " egg and ", " 2 ", " 2 ", " 8 ",
" 4 ", " 1 ", " 1 ", " 1 ", " hash browns ", " carrot raisin ", " nugget ", " strip ", " kids ", " nugget ",
" iced ", "sweet ", " sauce ", " chicken strip ", " deluxe ", " corn ", " mini", " ", " ", " sweet ", "nugget"
" chicken ", " chicken ", " polynesian ", " sriracha ", " 3 "]
#takes the transcription and converts to lowercase, then converts any word of interest to an uppercase tag indicating that it is
#important. Then erases the remaining lowercase letters
#convert to lowercase for processing: lowercase means unimportant
transcript = transcript.lower() + ' '
# print(transcript)
#remove punctuation
for char in puctuation:
transcript = transcript.replace(char,'')
# print(transcript)
#preliminary translate
for y in range(len(translate)):
transcript = transcript.replace(translate[y],TRANSLATE[y])
# print(transcript)
#
for y in range(len(translate)):
transcript = transcript.replace(translate[y],TRANSLATE[y])
# print(transcript)
#replace words of interest with order tags
for y in range(len(order_list)):
transcript = transcript.replace(order_list[y],orders[y])
# print(transcript)
#
for y in range(len(alternative_order_list)):
transcript = transcript.replace(alternative_order_list[y],orders[y])
# print(transcript)
#
for y in range(len(third_order_list)):
transcript = transcript.replace(third_order_list[y],orders[y])
# print(transcript)
#
for y in range(len(number_order_list)):
transcript = transcript.replace(number_order_list[y],orders[y])
# print(transcript)
#
for y in range(len(num_order_list)):
transcript = transcript.replace(num_order_list[y],orders[y])
# print(transcript)
#replace words of interest with number tags #applied after number_order_list to avoid conflicts
for y in range(len(quantities)):
transcript = transcript.replace(quantities[y],QUANTITIES[y])
# print(transcript)
#
for y in range(len(num_quantities)):
transcript = transcript.replace(num_quantities[y],QUANTITIES[y])
# print(transcript)
#finds the word 'to' in transcription to prevent confusion with two
for y in range(len(operators)):
transcript = transcript.replace(operators[y],OPERATORS[y])
# print(transcript)
#removes all lowercase letters that have not been converted to an order or number tag
for char in lowercase:
transcript = transcript.replace(char,'')
# print(transcript)
#take the filtered order data and convert to dictionary orders{} containing {order, quantity}
order_index = 1
qu = 1
last_qu = 1
word = ""
last_word = ""
drinks_left = 0
#for every order/quantity
while True:
#find next word
index = transcript.find(' ')
if index == -1:
word = transcript
elif index == 0:
transcript = transcript[1:]
continue
else:
word = transcript[:index]
#count drinks
if(word in drinks):
if(last_word in OPERATORS):
qu = last_qu
drinks_left = drinks_left - qu
if(last_word in drinks):
qu = 1
last_qu = 1
#compare word to list of all order tags and add to orders dict with quantity (if no quantity use 1)
if(word in orders):
order_index = orders.index(word)
if order_output[order_index] in order:
order[order_output[order_index]] = order[order_output[order_index]] + qu
else:
order[order_output[order_index]] = qu
last_qu = qu
qu = 1
#compare word to list of all order tags and update qu to indicate most recent quantity
elif(word in QUANTITIES):
qu = QUANTITIES.index(word)+1
last_qu = qu
#deals with meals
elif(word in OPERATORS and side_output[order_index] != "===="):
drinks_left = drinks_left + last_qu
if side_output[order_index] in order:
order[side_output[order_index]] = order[side_output[order_index]] + last_qu
else:
order[side_output[order_index]] = last_qu
#if no word left, end loop #if word left delete processed word
if index == -1:
break
else:
transcript = transcript[index+1:]
last_word = word
if drinks_left > 0:
if "Coca-Cola" in order:
order["Coca-Cola"] = order["Coca-Cola"] + drinks_left
else:
order["Coca-Cola"] = (drinks_left)
return order
if __name__ == "__main__":
# customerTest = Customer()
order = Audio.getOrder(Audio.getTranscript())
#order = Audio.getOrder("Hi. Can I have a kids? Nuggets meal?")
print(order)