2
0

ps_pizza.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Anthony Minessale II <anthm@freeswitch.org>
  27. *
  28. *
  29. * pizza.js ASR Demonstration Application
  30. *
  31. */
  32. include("js_modules/SpeechTools.jm");
  33. function on_dtmf(a, b, c) {}
  34. var dft_min = 40;
  35. var dft_confirm = 70;
  36. /***************** Initialize The Speech Detector *****************/
  37. var asr = new SpeechDetect(session, "pocketsphinx");
  38. /***************** Be more verbose *****************/
  39. asr.debug = 1;
  40. /***************** Set audio params *****************/
  41. asr.setAudioBase("/root/pizza/");
  42. asr.setAudioExt(".wav");
  43. /***************** Unload the last grammar whenever we activate a new one *****************/
  44. asr.AutoUnload = true;
  45. /***************** Create And Configure The Pizza *****************/
  46. var pizza = new Object();
  47. /***************** Delivery Or Take-Out? *****************/
  48. pizza.orderObtainer = new SpeechObtainer(asr, 1, 5000);
  49. pizza.orderObtainer.setGrammar("pizza_order", "", "result.interpretation.input", dft_min, dft_confirm, true);
  50. pizza.orderObtainer.setTopSound("GP-DeliveryorTakeout");
  51. pizza.orderObtainer.setBadSound("GP-NoDeliveryorTake-out");
  52. pizza.orderObtainer.addItemAlias("Delivery", "Delivery");
  53. pizza.orderObtainer.addItemAlias("Takeout,Pickup", "Pickup");
  54. /***************** What Size? *****************/
  55. pizza.sizeObtainer = new SpeechObtainer(asr, 1, 5000);
  56. pizza.sizeObtainer.setGrammar("pizza_size", "", "result.interpretation.input", dft_min, dft_confirm, true);
  57. pizza.sizeObtainer.setTopSound("GP-Size");
  58. pizza.sizeObtainer.setBadSound("GP-NI");
  59. pizza.sizeObtainer.addItemAlias("^Extra\\s*Large", "ExtraLarge");
  60. pizza.sizeObtainer.addItemAlias("^Large$", "Large");
  61. pizza.sizeObtainer.addItemAlias("^Medium$", "Medium");
  62. pizza.sizeObtainer.addItemAlias("^Small$", "Small");
  63. pizza.sizeObtainer.addItemAlias("^Humongous$,^Huge$,^Totally\\s*Humongous$,^Totally", "TotallyHumongous");
  64. /***************** What Type Of Crust? *****************/
  65. pizza.crustObtainer = new SpeechObtainer(asr, 1, 5000);
  66. pizza.crustObtainer.setGrammar("pizza_crust", "", "result.interpretation.input", dft_min, dft_confirm, true);
  67. pizza.crustObtainer.setTopSound("GP-Crust");
  68. pizza.crustObtainer.setBadSound("GP-NI");
  69. pizza.crustObtainer.addItemAlias("^Hand\\s*Tossed$,^Tossed$", "HandTossed");
  70. pizza.crustObtainer.addItemAlias("^Chicago\\s*style$,^Chicago$", "Pan");
  71. pizza.crustObtainer.addItemAlias("^Deep,^Pan,^Baked", "Pan");
  72. pizza.crustObtainer.addItemAlias("^New\\s*York,^Thin", "Thin");
  73. /***************** Specialty Or Custom? *****************/
  74. pizza.typeObtainer = new SpeechObtainer(asr, 1, 5000);
  75. pizza.typeObtainer.setGrammar("pizza_type", "", "result.interpretation.input", dft_min, dft_confirm, true);
  76. pizza.typeObtainer.setTopSound("GP-SpecialtyorCustom");
  77. pizza.typeObtainer.setBadSound("GP-NI");
  78. pizza.typeObtainer.addItemAlias("^Specialty$,^Specialty\\s*pizza$", "Specialty");
  79. pizza.typeObtainer.addItemAlias("^pick", "Custom");
  80. /***************** Which Specialty? *****************/
  81. pizza.specialtyObtainer = new SpeechObtainer(asr, 1, 5000);
  82. pizza.specialtyObtainer.setGrammar("pizza_specialty", "", "result.interpretation.input", dft_min, dft_confirm, true);
  83. pizza.specialtyObtainer.setTopSound("GP-SpecialtyList");
  84. pizza.specialtyObtainer.setBadSound("GP-NI");
  85. pizza.specialtyObtainer.addItemAlias("^Hawaii,^Hawaiian", "Hawaiian");
  86. pizza.specialtyObtainer.addItemAlias("^Meat", "MeatLovers");
  87. pizza.specialtyObtainer.addItemAlias("Pickle,^World", "Pickle");
  88. pizza.specialtyObtainer.addItemAlias("^Salvador,^Dolly,^Dali", "Dali");
  89. pizza.specialtyObtainer.addItemAlias("^Veg", "Vegetarian");
  90. /***************** What Toppings? *****************/
  91. pizza.toppingsObtainer = new SpeechObtainer(asr, 1, 5000);
  92. pizza.toppingsObtainer.setGrammar("pizza_toppings", "", "result.interpretation.input", dft_min, dft_confirm, true);
  93. pizza.toppingsObtainer.setTopSound("GP-Toppings");
  94. pizza.toppingsObtainer.setBadSound("GP-NI");
  95. pizza.toppingsObtainer.addItemAlias("anchovie,anchovies", "anchovies");
  96. pizza.toppingsObtainer.addItemAlias("artichoke,artichockes", "artichoke");
  97. pizza.toppingsObtainer.addItemAlias("canadian\\s*bacon", "canadianbacon");
  98. pizza.toppingsObtainer.addItemAlias("everything", "everything");
  99. pizza.toppingsObtainer.addItemAlias("extra\\s*cheese", "extracheese");
  100. pizza.toppingsObtainer.addItemAlias("garlic", "garlic");
  101. pizza.toppingsObtainer.addItemAlias("goat\\s*cheese", "goatcheese");
  102. pizza.toppingsObtainer.addItemAlias("bell\\s*pepper,bell\\s*peppers", "bellpepper");
  103. pizza.toppingsObtainer.addItemAlias("mango", "mango");
  104. pizza.toppingsObtainer.addItemAlias("mushroom,mushrooms", "mushroom");
  105. pizza.toppingsObtainer.addItemAlias("olives", "olives");
  106. pizza.toppingsObtainer.addItemAlias("onion,onions", "onions");
  107. pizza.toppingsObtainer.addItemAlias("pepperoni", "pepperoni");
  108. pizza.toppingsObtainer.addItemAlias("pickle,pickles", "pickle");
  109. pizza.toppingsObtainer.addItemAlias("pineapple", "pineapple");
  110. pizza.toppingsObtainer.addItemAlias("salami", "salami");
  111. pizza.toppingsObtainer.addItemAlias("sausage", "sausage");
  112. pizza.toppingsObtainer.addItemAlias("shrimp", "shrimp");
  113. pizza.toppingsObtainer.addItemAlias("spinich", "spinich");
  114. pizza.toppingsObtainer.addItemAlias("ham", "ham");
  115. /***************** Change Delivery Or Size Or Crust, Add/Rem Toppings Or Start Over *****************/
  116. pizza.arsoObtainer = new SpeechObtainer(asr, 1, 5000);
  117. pizza.arsoObtainer.setGrammar("pizza_arso", "", "result.interpretation.input", dft_min, 50, true);
  118. pizza.arsoObtainer.setTopSound("GP-ARSO");
  119. pizza.arsoObtainer.setBadSound("GP-NI");
  120. pizza.arsoObtainer.addItemAlias("^delivery$", "delivery");
  121. pizza.arsoObtainer.addItemAlias("^size$", "size");
  122. pizza.arsoObtainer.addItemAlias("^crust$", "crust");
  123. pizza.arsoObtainer.addItemAlias("^start\\s*over$", "startover");
  124. pizza.arsoObtainer.addItemAlias("^add\\s*", "add_topping");
  125. pizza.arsoObtainer.addItemAlias("^remove\\s*", "rem_topping");
  126. /***************** Yes? No? Maybe So? *****************/
  127. pizza.yesnoObtainer = new SpeechObtainer(asr, 1, 5000);
  128. pizza.yesnoObtainer.setGrammar("pizza_yesno", "", "result.interpretation.input", dft_min, 20, true);
  129. pizza.yesnoObtainer.setBadSound("GP-NI");
  130. pizza.yesnoObtainer.addItemAlias("^yes,^correct", "yes");
  131. pizza.yesnoObtainer.addItemAlias("^no", "no");
  132. /***************** Get Some Information *****************/
  133. pizza.get = function(params, confirm) {
  134. for(;;) {
  135. if (!session.ready()) {
  136. return false;
  137. }
  138. var main_items = params.run();
  139. if (confirm && params.needConfirm) {
  140. pizza.yesnoObtainer.setTopSound("Confirm" + main_items[0]);
  141. var items = pizza.yesnoObtainer.run();
  142. if (items[0] == "yes") {
  143. break;
  144. }
  145. } else {
  146. break;
  147. }
  148. }
  149. return main_items;
  150. };
  151. /***************** Is This Right? *****************/
  152. pizza.check = function () {
  153. if (!session.ready()) {
  154. return false;
  155. }
  156. asr.streamFile("GP-You_ordered_a");
  157. asr.streamFile(pizza.size);
  158. asr.streamFile(pizza.crust);
  159. if (pizza.type == "Specialty") {
  160. asr.streamFile(pizza.specialty);
  161. asr.streamFile("pizza");
  162. } else {
  163. asr.streamFile("pizza");
  164. asr.streamFile("GP-With");
  165. for (key in pizza.toppings) {
  166. if (pizza.toppings[key] == "add") {
  167. asr.streamFile(key);
  168. }
  169. }
  170. }
  171. pizza.yesnoObtainer.setTopSound("GP-WasThisRight");
  172. items = pizza.yesnoObtainer.run();
  173. return items[0] == "yes" ? true : false;
  174. };
  175. /***************** Let's Remove The Toppings *****************/
  176. pizza.clearToppings = function() {
  177. if (!session.ready()) {
  178. return false;
  179. }
  180. if (pizza.toppings) {
  181. delete pizza.toppings;
  182. }
  183. pizza.have_toppings = false;
  184. pizza.toppings = new Array();
  185. }
  186. /***************** Clean Slate *****************/
  187. pizza.init = function() {
  188. if (!session.ready()) {
  189. return false;
  190. }
  191. pizza.add_rem = "add";
  192. pizza.order = pizza.size = pizza.crust = pizza.type = false;
  193. pizza.toppingsObtainer.setTopSound("GP-Toppings");
  194. pizza.specialty = false;
  195. pizza.clearToppings();
  196. pizza.said_greet = false;
  197. }
  198. /***************** Welcome! *****************/
  199. pizza.greet = function () {
  200. if (!session.ready()) {
  201. return false;
  202. }
  203. if (!pizza.said_greet) {
  204. asr.streamFile("GP-Greeting");
  205. pizza.said_greet = true;
  206. }
  207. };
  208. /***************** Collect Order Type *****************/
  209. pizza.getOrder = function() {
  210. if (!session.ready()) {
  211. return false;
  212. }
  213. if (!pizza.order) {
  214. var items = pizza.get(pizza.orderObtainer, true);
  215. pizza.order = items[0];
  216. }
  217. };
  218. /***************** Collect Size *****************/
  219. pizza.getSize = function() {
  220. if (!session.ready()) {
  221. return false;
  222. }
  223. if (!pizza.size) {
  224. var items = pizza.get(pizza.sizeObtainer, true);
  225. pizza.size = items[0];
  226. }
  227. };
  228. /***************** Collect Crust *****************/
  229. pizza.getCrust = function() {
  230. if (!session.ready()) {
  231. return false;
  232. }
  233. if (!pizza.crust) {
  234. var items = pizza.get(pizza.crustObtainer, true);
  235. pizza.crust = items[0];
  236. }
  237. };
  238. /***************** Collect Pizza Type *****************/
  239. pizza.getType = function() {
  240. if (!session.ready()) {
  241. return false;
  242. }
  243. if (!pizza.type) {
  244. var items = pizza.get(pizza.typeObtainer, true);
  245. pizza.type = items[0];
  246. }
  247. };
  248. /***************** Collect Toppings *****************/
  249. pizza.getToppings = function() {
  250. if (!session.ready()) {
  251. return false;
  252. }
  253. if (pizza.type == "Specialty" && !pizza.specialty) {
  254. var items = pizza.get(pizza.specialtyObtainer, true);
  255. pizza.specialty = items[0];
  256. pizza.have_toppings = true;
  257. } else if (!pizza.have_toppings) {
  258. toppings = pizza.get(pizza.toppingsObtainer, false);
  259. for(x = 0; x < toppings.length; x++) {
  260. pizza.toppings[toppings[x]] = pizza.add_rem;
  261. }
  262. pizza.have_toppings = true;
  263. }
  264. };
  265. /***************** Modify Pizza If You Don't Like It *****************/
  266. pizza.fix = function() {
  267. if (!session.ready()) {
  268. return false;
  269. }
  270. asr.streamFile("GP-Wanted-No");
  271. arso = pizza.get(pizza.arsoObtainer, false);
  272. for (x = 0; x < arso.length; x++) {
  273. if (arso[x] == "delivery") {
  274. pizza.order = false;
  275. } else if (arso[x] == "size") {
  276. pizza.size = false;
  277. } else if (arso[x] == "crust") {
  278. pizza.crust = false;
  279. } else if (arso[x] == "startover") {
  280. pizza.init();
  281. } else {
  282. if (pizza.type == "Specialty") {
  283. asr.streamFile("GP-ChangeSpec");
  284. pizza.type = false;
  285. pizza.clearToppings();
  286. } else {
  287. pizza.have_toppings = false;
  288. if (arso[x] == "add_topping") {
  289. pizza.add_rem = "add";
  290. pizza.toppingsObtainer.setTopSound("GP-Adding");
  291. } else {
  292. pizza.add_rem = "rem";
  293. pizza.toppingsObtainer.setTopSound("GP-Remove");
  294. }
  295. }
  296. }
  297. }
  298. };
  299. /***************** Tie It All Together *****************/
  300. pizza.run = function() {
  301. pizza.init();
  302. for(;;) {
  303. if (!session.ready()) {
  304. break;
  305. }
  306. pizza.greet();
  307. pizza.getOrder();
  308. pizza.getSize();
  309. pizza.getCrust();
  310. pizza.getType();
  311. pizza.getToppings();
  312. if (pizza.check()) {
  313. asr.streamFile(pizza.order);
  314. break;
  315. } else {
  316. pizza.fix();
  317. }
  318. }
  319. };
  320. /***************** Begin Program *****************/
  321. session.answer();
  322. pizza.run();
  323. asr.stop();