forked from signalwire/freeswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps_pizza.js
More file actions
358 lines (323 loc) · 11.6 KB
/
Copy pathps_pizza.js
File metadata and controls
358 lines (323 loc) · 11.6 KB
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
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <[email protected]>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <[email protected]>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <[email protected]>
*
*
* pizza.js ASR Demonstration Application
*
*/
include("js_modules/SpeechTools.jm");
function on_dtmf(a, b, c) {}
var dft_min = 40;
var dft_confirm = 70;
/***************** Initialize The Speech Detector *****************/
var asr = new SpeechDetect(session, "pocketsphinx");
/***************** Be more verbose *****************/
asr.debug = 1;
/***************** Set audio params *****************/
asr.setAudioBase("/root/pizza/");
asr.setAudioExt(".wav");
/***************** Unload the last grammar whenever we activate a new one *****************/
asr.AutoUnload = true;
/***************** Create And Configure The Pizza *****************/
var pizza = new Object();
/***************** Delivery Or Take-Out? *****************/
pizza.orderObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.orderObtainer.setGrammar("pizza_order", "", "result.interpretation.input", dft_min, dft_confirm, true);
pizza.orderObtainer.setTopSound("GP-DeliveryorTakeout");
pizza.orderObtainer.setBadSound("GP-NoDeliveryorTake-out");
pizza.orderObtainer.addItemAlias("Delivery", "Delivery");
pizza.orderObtainer.addItemAlias("Takeout,Pickup", "Pickup");
/***************** What Size? *****************/
pizza.sizeObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.sizeObtainer.setGrammar("pizza_size", "", "result.interpretation.input", dft_min, dft_confirm, true);
pizza.sizeObtainer.setTopSound("GP-Size");
pizza.sizeObtainer.setBadSound("GP-NI");
pizza.sizeObtainer.addItemAlias("^Extra\\s*Large", "ExtraLarge");
pizza.sizeObtainer.addItemAlias("^Large$", "Large");
pizza.sizeObtainer.addItemAlias("^Medium$", "Medium");
pizza.sizeObtainer.addItemAlias("^Small$", "Small");
pizza.sizeObtainer.addItemAlias("^Humongous$,^Huge$,^Totally\\s*Humongous$,^Totally", "TotallyHumongous");
/***************** What Type Of Crust? *****************/
pizza.crustObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.crustObtainer.setGrammar("pizza_crust", "", "result.interpretation.input", dft_min, dft_confirm, true);
pizza.crustObtainer.setTopSound("GP-Crust");
pizza.crustObtainer.setBadSound("GP-NI");
pizza.crustObtainer.addItemAlias("^Hand\\s*Tossed$,^Tossed$", "HandTossed");
pizza.crustObtainer.addItemAlias("^Chicago\\s*style$,^Chicago$", "Pan");
pizza.crustObtainer.addItemAlias("^Deep,^Pan,^Baked", "Pan");
pizza.crustObtainer.addItemAlias("^New\\s*York,^Thin", "Thin");
/***************** Specialty Or Custom? *****************/
pizza.typeObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.typeObtainer.setGrammar("pizza_type", "", "result.interpretation.input", dft_min, dft_confirm, true);
pizza.typeObtainer.setTopSound("GP-SpecialtyorCustom");
pizza.typeObtainer.setBadSound("GP-NI");
pizza.typeObtainer.addItemAlias("^Specialty$,^Specialty\\s*pizza$", "Specialty");
pizza.typeObtainer.addItemAlias("^pick", "Custom");
/***************** Which Specialty? *****************/
pizza.specialtyObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.specialtyObtainer.setGrammar("pizza_specialty", "", "result.interpretation.input", dft_min, dft_confirm, true);
pizza.specialtyObtainer.setTopSound("GP-SpecialtyList");
pizza.specialtyObtainer.setBadSound("GP-NI");
pizza.specialtyObtainer.addItemAlias("^Hawaii,^Hawaiian", "Hawaiian");
pizza.specialtyObtainer.addItemAlias("^Meat", "MeatLovers");
pizza.specialtyObtainer.addItemAlias("Pickle,^World", "Pickle");
pizza.specialtyObtainer.addItemAlias("^Salvador,^Dolly,^Dali", "Dali");
pizza.specialtyObtainer.addItemAlias("^Veg", "Vegetarian");
/***************** What Toppings? *****************/
pizza.toppingsObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.toppingsObtainer.setGrammar("pizza_toppings", "", "result.interpretation.input", dft_min, dft_confirm, true);
pizza.toppingsObtainer.setTopSound("GP-Toppings");
pizza.toppingsObtainer.setBadSound("GP-NI");
pizza.toppingsObtainer.addItemAlias("anchovie,anchovies", "anchovies");
pizza.toppingsObtainer.addItemAlias("artichoke,artichockes", "artichoke");
pizza.toppingsObtainer.addItemAlias("canadian\\s*bacon", "canadianbacon");
pizza.toppingsObtainer.addItemAlias("everything", "everything");
pizza.toppingsObtainer.addItemAlias("extra\\s*cheese", "extracheese");
pizza.toppingsObtainer.addItemAlias("garlic", "garlic");
pizza.toppingsObtainer.addItemAlias("goat\\s*cheese", "goatcheese");
pizza.toppingsObtainer.addItemAlias("bell\\s*pepper,bell\\s*peppers", "bellpepper");
pizza.toppingsObtainer.addItemAlias("mango", "mango");
pizza.toppingsObtainer.addItemAlias("mushroom,mushrooms", "mushroom");
pizza.toppingsObtainer.addItemAlias("olives", "olives");
pizza.toppingsObtainer.addItemAlias("onion,onions", "onions");
pizza.toppingsObtainer.addItemAlias("pepperoni", "pepperoni");
pizza.toppingsObtainer.addItemAlias("pickle,pickles", "pickle");
pizza.toppingsObtainer.addItemAlias("pineapple", "pineapple");
pizza.toppingsObtainer.addItemAlias("salami", "salami");
pizza.toppingsObtainer.addItemAlias("sausage", "sausage");
pizza.toppingsObtainer.addItemAlias("shrimp", "shrimp");
pizza.toppingsObtainer.addItemAlias("spinich", "spinich");
pizza.toppingsObtainer.addItemAlias("ham", "ham");
/***************** Change Delivery Or Size Or Crust, Add/Rem Toppings Or Start Over *****************/
pizza.arsoObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.arsoObtainer.setGrammar("pizza_arso", "", "result.interpretation.input", dft_min, 50, true);
pizza.arsoObtainer.setTopSound("GP-ARSO");
pizza.arsoObtainer.setBadSound("GP-NI");
pizza.arsoObtainer.addItemAlias("^delivery$", "delivery");
pizza.arsoObtainer.addItemAlias("^size$", "size");
pizza.arsoObtainer.addItemAlias("^crust$", "crust");
pizza.arsoObtainer.addItemAlias("^start\\s*over$", "startover");
pizza.arsoObtainer.addItemAlias("^add\\s*", "add_topping");
pizza.arsoObtainer.addItemAlias("^remove\\s*", "rem_topping");
/***************** Yes? No? Maybe So? *****************/
pizza.yesnoObtainer = new SpeechObtainer(asr, 1, 5000);
pizza.yesnoObtainer.setGrammar("pizza_yesno", "", "result.interpretation.input", dft_min, 20, true);
pizza.yesnoObtainer.setBadSound("GP-NI");
pizza.yesnoObtainer.addItemAlias("^yes,^correct", "yes");
pizza.yesnoObtainer.addItemAlias("^no", "no");
/***************** Get Some Information *****************/
pizza.get = function(params, confirm) {
for(;;) {
if (!session.ready()) {
return false;
}
var main_items = params.run();
if (confirm && params.needConfirm) {
pizza.yesnoObtainer.setTopSound("Confirm" + main_items[0]);
var items = pizza.yesnoObtainer.run();
if (items[0] == "yes") {
break;
}
} else {
break;
}
}
return main_items;
};
/***************** Is This Right? *****************/
pizza.check = function () {
if (!session.ready()) {
return false;
}
asr.streamFile("GP-You_ordered_a");
asr.streamFile(pizza.size);
asr.streamFile(pizza.crust);
if (pizza.type == "Specialty") {
asr.streamFile(pizza.specialty);
asr.streamFile("pizza");
} else {
asr.streamFile("pizza");
asr.streamFile("GP-With");
for (key in pizza.toppings) {
if (pizza.toppings[key] == "add") {
asr.streamFile(key);
}
}
}
pizza.yesnoObtainer.setTopSound("GP-WasThisRight");
items = pizza.yesnoObtainer.run();
return items[0] == "yes" ? true : false;
};
/***************** Let's Remove The Toppings *****************/
pizza.clearToppings = function() {
if (!session.ready()) {
return false;
}
if (pizza.toppings) {
delete pizza.toppings;
}
pizza.have_toppings = false;
pizza.toppings = new Array();
}
/***************** Clean Slate *****************/
pizza.init = function() {
if (!session.ready()) {
return false;
}
pizza.add_rem = "add";
pizza.order = pizza.size = pizza.crust = pizza.type = false;
pizza.toppingsObtainer.setTopSound("GP-Toppings");
pizza.specialty = false;
pizza.clearToppings();
pizza.said_greet = false;
}
/***************** Welcome! *****************/
pizza.greet = function () {
if (!session.ready()) {
return false;
}
if (!pizza.said_greet) {
asr.streamFile("GP-Greeting");
pizza.said_greet = true;
}
};
/***************** Collect Order Type *****************/
pizza.getOrder = function() {
if (!session.ready()) {
return false;
}
if (!pizza.order) {
var items = pizza.get(pizza.orderObtainer, true);
pizza.order = items[0];
}
};
/***************** Collect Size *****************/
pizza.getSize = function() {
if (!session.ready()) {
return false;
}
if (!pizza.size) {
var items = pizza.get(pizza.sizeObtainer, true);
pizza.size = items[0];
}
};
/***************** Collect Crust *****************/
pizza.getCrust = function() {
if (!session.ready()) {
return false;
}
if (!pizza.crust) {
var items = pizza.get(pizza.crustObtainer, true);
pizza.crust = items[0];
}
};
/***************** Collect Pizza Type *****************/
pizza.getType = function() {
if (!session.ready()) {
return false;
}
if (!pizza.type) {
var items = pizza.get(pizza.typeObtainer, true);
pizza.type = items[0];
}
};
/***************** Collect Toppings *****************/
pizza.getToppings = function() {
if (!session.ready()) {
return false;
}
if (pizza.type == "Specialty" && !pizza.specialty) {
var items = pizza.get(pizza.specialtyObtainer, true);
pizza.specialty = items[0];
pizza.have_toppings = true;
} else if (!pizza.have_toppings) {
toppings = pizza.get(pizza.toppingsObtainer, false);
for(x = 0; x < toppings.length; x++) {
pizza.toppings[toppings[x]] = pizza.add_rem;
}
pizza.have_toppings = true;
}
};
/***************** Modify Pizza If You Don't Like It *****************/
pizza.fix = function() {
if (!session.ready()) {
return false;
}
asr.streamFile("GP-Wanted-No");
arso = pizza.get(pizza.arsoObtainer, false);
for (x = 0; x < arso.length; x++) {
if (arso[x] == "delivery") {
pizza.order = false;
} else if (arso[x] == "size") {
pizza.size = false;
} else if (arso[x] == "crust") {
pizza.crust = false;
} else if (arso[x] == "startover") {
pizza.init();
} else {
if (pizza.type == "Specialty") {
asr.streamFile("GP-ChangeSpec");
pizza.type = false;
pizza.clearToppings();
} else {
pizza.have_toppings = false;
if (arso[x] == "add_topping") {
pizza.add_rem = "add";
pizza.toppingsObtainer.setTopSound("GP-Adding");
} else {
pizza.add_rem = "rem";
pizza.toppingsObtainer.setTopSound("GP-Remove");
}
}
}
}
};
/***************** Tie It All Together *****************/
pizza.run = function() {
pizza.init();
for(;;) {
if (!session.ready()) {
break;
}
pizza.greet();
pizza.getOrder();
pizza.getSize();
pizza.getCrust();
pizza.getType();
pizza.getToppings();
if (pizza.check()) {
asr.streamFile(pizza.order);
break;
} else {
pizza.fix();
}
}
};
/***************** Begin Program *****************/
session.answer();
pizza.run();
asr.stop();