-
Notifications
You must be signed in to change notification settings - Fork 3
/
Audio_clean.py
277 lines (255 loc) · 14.4 KB
/
Audio_clean.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
#input: audio file 'Recording1.mp3' in current directory
#output: dictionary orders {order, quantity}
#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
from Customer import Customer_Order
class Audio:
def __init__(self):
self.menu = ["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", "Waffel 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"]
#get transcript of input
# @staticmethod
def getTranscript(self):
#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.mp3', 'sound-joelmussell', 'Recording.mp3')
job_name = "Transcribe63"
job_uri = "https://s3.us-east-2.amazonaws.com/sound-joelmussell/Recording.mp3"
#start transcription
transcribe.start_transcription_job(
TranscriptionJobName=job_name,
Media={'MediaFileUri': job_uri},
MediaFormat='mp3',
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("Not ready yet...")
time.sleep(5)
#record transcribe output
text = transcribe.get_transcription_job(TranscriptionJobName=job_name)
#open json reults file and extract information
jsonText = urllib.urlopen(text['TranscriptionJob']['Transcript']['TranscriptFileUri']).read()
index = jsonText.find('\"transcript\"') + 14
transcript = jsonText[index:]
index = transcript.find('\"')
transcript = transcript[:index]
os.remove("Recording1.mp3")
transcribe.delete_transcription_job(TranscriptionJobName=job_name)
# print(transcript)
return transcript
#process transcript
#returns dict() with order
# @staticmethod
def getOrder(self, transcript):
puctuation = {",", ".", "?", "!"}
lowercase = {"'s", "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 = {}
number_order_list = ["number one", "====", "====",
"number two", "number five", "number seven", "number three",
"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", "====",
"strip", "wrap", "====", "====", "mini",
"====", "bacon egg and cheese biscuit", "sausage egg and cheese biscuit",
"====", "sunflower bagel", "====", "yogurt",
"fruit", "chicken egg and cheese bagel", "hash brown burrito",
"hash brown bowl", "muffin", "bacon egg and cheese muffin",
"sausage egg and cheese muffin", "====", "fries", "====",
"noodle", "tortilla", "superfood", "apple sauce",
"====", "====", "slaw", "====", "chip",
"kid's nugget", "kid's chicken strip", "kid's grilled nugget",
"chocolate milk shake", "cookies and cream milk shake", "strawberry milk shake",
"milk shake", "====", "====", "====",
"ice-cream", "key lime", "sweet", "lemonade",
"coke", "doctor pepper", "water", "====",
"====","chocolate milk", "milk", "coffee", "====",
"====", "====", "unsweet",
"====", "====", "====", "====",
"====", "====", "===="]
#
alternative_order_list = ["sandwich", "deluxe", "spicy chicken",
"====", "grilled chicken", "club", "nugget",
"strip", "wrap", "grilled nugget", "====", "mini",
"====", "bacon egg and cheese biscuit", "sausage egg and cheese biscuit",
"====", "sunflower bagel", "====", "yogurt",
"fruit", "chicken egg and cheese bagel", "hash brown burrito",
"hash brown bowl", "muffin", "bacon egg and cheese muffin",
"sausage egg and cheese muffin", "====", "fries", "====",
"noodle soup", "tortilla soup", "superfood", "apple sauce",
"====", "====", "slaw", "====", "chip",
"kid's nugget", "kid's chicken strip", "kid's grilled nugget",
"chocolate milk shake", "cookies and cream milk shake", "strawberry milk shake",
"vanilla milk shake", "====", "====", "cookie",
"ice-cream", "key lime", "sweet tea", "lemonade",
"coke", "doctor pepper", "water", "====",
"====","chocolate milk", "milk", "coffee", "====",
"====", "====", "unsweet tea",
"====", "====", "====", "====",
"====", "====", "===="]
#
order_list = ["====", "====", "spicy chicken sandwich",
"spicy deluxe", "grilled chicken sandwich", "grilled chicken club", "chicken nugget",
"chicken strip", "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", "hash brown", "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", "buddy's apple sauce",
"carrot raisin salad", "chicken salad", "cole slaw", "cornbread", "potato chip",
"nuggets kid's", "chicken strips kid's", "grilled nuggets kid's",
"chocolate milkshake", "cookies and cream milkshake", "strawberry milkshake",
"vanilla milkshake", "frosted coffee", "frosted lemonade", "chocolate chunk cookie",
"ice dream cone", "key lime", "iced tea sweetened", "====",
"coca-cola", "dr pepper", "bottled water", "apple juice",
"orange", "====", "white milk", "====", "iced coffee",
"gallon", "diet lemonade", "iced tea unsweetened",
"chick-fil-a sauce", "polynesian", "honey mustard", "ranch sauce",
"buffalo", "barbeque", "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"]
#output array
order_output = self.menu
#quantities array
quantities = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
QUANTITIES = ["ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"]
#operators array
operators = ["to"]#"with", "without", "and", "no", "not", "meal"}
OPERATORS = ["TO"]#"with", "without", "and", "no", "not", "meal"}
#takes the transcription and converts to lowercase, then converts any wword 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)
#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)
#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)
#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}
q = False
qu = 1
#for every order/quantity
while True:
#find next word
index = transcript.find(' ')
if index == -1:
word = transcript
else:
word = transcript[:index]
#compare word to list of all order tags and add to orders dict with quantity (if no quantity use 1)
if(word in orders):
if(q == False):
qu = 1
order[order_output[orders.index(word)]] = qu
q = False
#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
q = True
#if no qunatity but 'to' is found assume it is 2
elif(word == 'TO' and q == False):
qu = 2
q = True
#if no word left, end loop #if word left delete processed word
if index == -1:
break
else:
transcript = transcript[index+1:]
return order
def return_actual_order(self):
'''
:return: customer order object
'''
order = self.getOrder('Can I get three number two meal with four cookies and cream milkshake and a chicken biscuit please')
actual_order = Customer_Order(order, menu=self.menu)
return actual_order
if __name__ == "__main__":
# customerTest = Customer()
#Audio.getOrder(Audio.getTranscript())
a = Audio()
# asef = a.getTranscript()
order = a.return_actual_order()
print(order)