Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using mmisharp;
  8. using Microsoft.Speech.Recognition;
  9.  
  10. namespace speechModality
  11. {
  12. public class SpeechMod
  13. {
  14. private SpeechRecognitionEngine sre;
  15. private Grammar gr;
  16. private Grammar toLoad;
  17. private Grammar loaded;
  18. private bool done;
  19. private bool grammarLoaded;
  20. public event EventHandler<SpeechEventArg> Recognized;
  21. private Tts t;
  22. protected virtual void onRecognized(SpeechEventArg msg)
  23. {
  24. EventHandler<SpeechEventArg> handler = Recognized;
  25. if (handler != null)
  26. {
  27. handler(this, msg);
  28. }
  29. }
  30.  
  31.  
  32. private LifeCycleEvents lce;
  33. private MmiCommunication mmic;
  34.  
  35.  
  36. private Queue<SpeechRecognizedEventArgs> eventQueue = new Queue<SpeechRecognizedEventArgs>();
  37.  
  38.  
  39.  
  40. public SpeechMod()
  41. {
  42. //init Text-To-Speech
  43. t = new Tts();
  44. //init LifeCycleEvents..
  45. lce = new LifeCycleEvents("ASR", "FUSION","speech-1", "acoustic", "command"); // LifeCycleEvents(string source, string target, string id, string medium, string mode)
  46. //mmic = new MmiCommunication("localhost",9876,"User1", "ASR"); //PORT TO FUSION - uncomment this line to work with fusion later
  47. mmic = new MmiCommunication("localhost", 8000, "User1", "ASR"); // MmiCommunication(string IMhost, int portIM, string UserOD, string thisModalityName)
  48.  
  49. mmic.Send(lce.NewContextRequest());
  50.  
  51. //load pt recognizer
  52. sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("pt-PT"));
  53. gr = new Grammar(Environment.CurrentDirectory + "\\ptG.grxml", "rootRule");
  54. sre.LoadGrammar(gr);
  55. grammarLoaded = false;
  56.  
  57.  
  58. sre.SetInputToDefaultAudioDevice();
  59. sre.RecognizeAsync(RecognizeMode.Multiple);
  60. sre.SpeechRecognized += Sre_SpeechRecognized;
  61. sre.SpeechHypothesized += Sre_SpeechHypothesized;
  62.  
  63. sre.RecognizerUpdateReached +=
  64. new EventHandler<RecognizerUpdateReachedEventArgs>(recognizer_RecognizerUpdateReached);
  65.  
  66. var fileSystemWatcher = new FileSystemWatcher();
  67.  
  68. fileSystemWatcher.Changed += FileSystemWatcher_Changed;
  69.  
  70.  
  71.  
  72.  
  73.  
  74. fileSystemWatcher.Path = Environment.CurrentDirectory + @"\..\..";
  75.  
  76. fileSystemWatcher.EnableRaisingEvents = true;
  77. }
  78.  
  79.  
  80. public void recognizer_RecognizerUpdateReached(object sender, RecognizerUpdateReachedEventArgs e)
  81. {
  82. if (done == false)
  83. {
  84. if (grammarLoaded == true)
  85. {
  86. sre.UnloadGrammar(loaded);
  87. Console.WriteLine("Grammar unloaded");
  88. }
  89. sre.LoadGrammar(toLoad);
  90. Console.WriteLine("Grammar loaded");
  91. grammarLoaded = true;
  92. done = true;
  93. loaded = toLoad;
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. }
  102.  
  103.  
  104. private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
  105. {
  106. Console.WriteLine("Pahthhhh ");
  107. GrammarBuilder builder = new GrammarBuilder();
  108. builder.AppendRuleReference(Environment.CurrentDirectory + @"\..\..\Restaurants.grxml", "main");
  109.  
  110.  
  111. toLoad = new Grammar(builder);
  112. done = false;
  113. sre.RequestRecognizerUpdate();
  114. }
  115.  
  116. private void Sre_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
  117. {
  118. onRecognized(new SpeechEventArg() { Text = e.Result.Text, Confidence = e.Result.Confidence, Final = false });
  119. }
  120.  
  121.  
  122. private bool processRequest(SpeechRecognizedEventArgs e)
  123. {
  124. if(eventQueue.Count == 0 || (eventQueue.Count!=0 && e.Result.Semantics.ToArray()[0].Value.Value.Equals("Confirm")))
  125. {
  126. switch (e.Result.Semantics.ToArray()[0].Value.Value)
  127. {
  128. // TODO: Decide wether to send (feeback and request)
  129. case "cidades":
  130. if (e.Result.Confidence < 0.20)
  131. {
  132. t.Speak("Não consegui entender, por favor repita");
  133. return false;
  134. }
  135. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  136. {
  137. // obtem informação se é mesmo isto
  138. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  139. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  140. eventQueue.Enqueue(e);
  141. return false;
  142. }
  143. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  144. {
  145. // procura e obtem info
  146. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  147. return true;
  148. }
  149. else
  150. {
  151. return true;
  152. }
  153.  
  154. case "Comidas":
  155. if (e.Result.Confidence < 0.20)
  156. {
  157. t.Speak("Não consegui entender, por favor repita");
  158. return false;
  159. }
  160. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  161. {
  162. // obtem informação se é mesmo isto
  163. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  164. t.Speak("Não consegui entender, Será que quis procurar por " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  165. eventQueue.Enqueue(e);
  166. return false;
  167. }
  168. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  169. {
  170. // procura e obtem info
  171. t.Speak("Estou a procura de " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  172. return true;
  173. }
  174. else
  175. {
  176. return true;
  177. }
  178.  
  179. case "Tipodeestabelecimento":
  180. if (e.Result.Confidence < 0.20)
  181. {
  182. t.Speak("Não consegui entender, por favor repita");
  183. return false;
  184. }
  185. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  186. {
  187. // obtem informação se é mesmo isto
  188. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  189. t.Speak("Não consegui entender, Será que quis procurar por " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  190. eventQueue.Enqueue(e);
  191. return false;
  192. }
  193. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  194. {
  195. // procura e obtem info
  196. t.Speak("Estou a procura de " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  197. return true;
  198. }
  199. else
  200. {
  201. return true;
  202. }
  203.  
  204. case "Limpar":
  205. if (e.Result.Confidence < 0.20)
  206. {
  207. t.Speak("Não consegui entender, por favor repita");
  208. return false;
  209. }
  210. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  211. {
  212. // obtem informação se é mesmo isto
  213. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  214. t.Speak("Não consegui entender, Será que quis limpar os filtros ?");
  215. eventQueue.Enqueue(e);
  216. return false;
  217. }
  218. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  219. {
  220. // procura e obtem info
  221. t.Speak("Estou a limpar os filtros ");
  222. return true;
  223. }
  224. else
  225. {
  226. return true;
  227. }
  228.  
  229. case "Confirm":
  230. if (e.Result.Semantics.ToArray()[1].Value.Value.ToString().Equals("Sim"))
  231. {
  232. e = eventQueue.Dequeue();
  233.  
  234. t.Speak("Estou a efectuar a acção pedida!!");
  235.  
  236. string json = "{ \"recognized\": [";
  237. foreach (var resultSemantic in e.Result.Semantics)
  238. {
  239. json += "\"" + resultSemantic.Value.Value + "\", ";
  240. }
  241. json = json.Substring(0, json.Length - 2);
  242. json += "] }";
  243.  
  244. var exNot = lce.ExtensionNotification(e.Result.Audio.StartTime + "", e.Result.Audio.StartTime.Add(e.Result.Audio.Duration) + "", e.Result.Confidence, json);
  245.  
  246. mmic.Send(exNot);
  247. return false;
  248. }
  249. else
  250. {
  251. t.Speak("Pesquisa cancelada, por fovor qual é a proxima ação");
  252. eventQueue.Dequeue();
  253. return false;
  254. }
  255.  
  256. case "Restaurantes":
  257. if (e.Result.Confidence < 0.20)
  258. {
  259. t.Speak("Não consegui entender, por favor repita");
  260. return false;
  261. }
  262. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  263. {
  264. // obtem informação se é mesmo isto
  265. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  266. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  267. eventQueue.Enqueue(e);
  268. return false;
  269. }
  270. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  271. {
  272. // procura e obtem info
  273. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  274. return true;
  275. }
  276. else
  277. {
  278. return true;
  279. }
  280.  
  281. case "TipodeReserva":
  282. if (e.Result.Confidence < 0.20)
  283. {
  284. t.Speak("Não consegui entender, por favor repita");
  285. return false;
  286. }
  287. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  288. {
  289. // obtem informação se é mesmo isto
  290. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  291. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  292. eventQueue.Enqueue(e);
  293. return false;
  294. }
  295. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  296. {
  297. // procura e obtem info
  298. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  299. return true;
  300. }
  301. else
  302. {
  303. return true;
  304. }
  305. case "Cozinhasepratos":
  306. if (e.Result.Confidence < 0.20)
  307. {
  308. t.Speak("Não consegui entender, por favor repita");
  309. return false;
  310. }
  311. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  312. {
  313. // obtem informação se é mesmo isto
  314. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  315. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  316. eventQueue.Enqueue(e);
  317. return false;
  318. }
  319. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  320. {
  321. // procura e obtem info
  322. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  323. return true;
  324. }
  325. else
  326. {
  327. return true;
  328. }
  329. case "Preco":
  330. if (e.Result.Confidence < 0.20)
  331. {
  332. t.Speak("Não consegui entender, por favor repita");
  333. return false;
  334. }
  335. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  336. {
  337. // obtem informação se é mesmo isto
  338. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  339. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  340. eventQueue.Enqueue(e);
  341. return false;
  342. }
  343. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  344. {
  345. // procura e obtem info
  346. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  347. return true;
  348. }
  349. else
  350. {
  351. return true;
  352. }
  353. case "Caracteristicasdorestaurante":
  354. if (e.Result.Confidence < 0.20)
  355. {
  356. t.Speak("Não consegui entender, por favor repita");
  357. return false;
  358. }
  359. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  360. {
  361. // obtem informação se é mesmo isto
  362. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  363. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  364. eventQueue.Enqueue(e);
  365. return false;
  366. }
  367. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  368. {
  369. // procura e obtem info
  370. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  371. return true;
  372. }
  373. else
  374. {
  375. return true;
  376. }
  377. case "Bonspara":
  378. if (e.Result.Confidence < 0.20)
  379. {
  380. t.Speak("Não consegui entender, por favor repita");
  381. return false;
  382. }
  383. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  384. {
  385. // obtem informação se é mesmo isto
  386. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  387. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  388. eventQueue.Enqueue(e);
  389. return false;
  390. }
  391. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  392. {
  393. // procura e obtem info
  394. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  395. return true;
  396. }
  397. else
  398. {
  399. return true;
  400. }
  401. case "Refeicoes":
  402. if (e.Result.Confidence < 0.20)
  403. {
  404. t.Speak("Não consegui entender, por favor repita");
  405. return false;
  406. }
  407. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  408. {
  409. // obtem informação se é mesmo isto
  410. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  411. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  412. eventQueue.Enqueue(e);
  413. return false;
  414. }
  415. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  416. {
  417. // procura e obtem info
  418. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  419. return true;
  420. }
  421. else
  422. {
  423. return true;
  424. }
  425. case "Restricoesalimentares":
  426. if (e.Result.Confidence < 0.20)
  427. {
  428. t.Speak("Não consegui entender, por favor repita");
  429. return false;
  430. }
  431. else if (e.Result.Confidence >= 0.20 && e.Result.Confidence < 0.60)
  432. {
  433. // obtem informação se é mesmo isto
  434. Console.WriteLine(e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  435. t.Speak("Não consegui entender, Será que quis procurar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  436. eventQueue.Enqueue(e);
  437. return false;
  438. }
  439. else if (e.Result.Confidence >= 0.60 && e.Result.Confidence < 0.80)
  440. {
  441. // procura e obtem info
  442. t.Speak("Estou a procura da cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  443. return true;
  444. }
  445. else
  446. {
  447. return true;
  448. }
  449.  
  450. }
  451.  
  452. }
  453. else
  454. {
  455. e = eventQueue.Peek();
  456. switch (e.Result.Semantics.ToArray()[0].Value.Value)
  457. {
  458. // TODO: Decide wether to send (feeback and request)
  459. case "cidades":
  460. t.Speak("Por favor confirme que quer pesquisar a cidade " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  461. break;
  462. case "Comidas":
  463. t.Speak("Por favor confirme que quer pesquisar pelo tipo de comida " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  464. break;
  465. case "Tipodeestabelecimento":
  466. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  467. break;
  468. case "Limpar":
  469. t.Speak("Por favor confirme que quer lempar todos os filtros" );
  470. break;
  471. case "Restaurantes":
  472. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  473. break;
  474. case "TipodeReserva":
  475. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  476. break;
  477. case "Cozinhasepratos":
  478. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  479. break;
  480. case "Preco":
  481. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  482. break;
  483. case "Caracteristicasdorestaurante":
  484. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  485. break;
  486. case "Bonspara":
  487. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  488. break;
  489. case "Refeicoes":
  490. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  491. break;
  492. case "Restricoesalimentares":
  493. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  494. break;
  495. case "Mais":
  496. t.Speak("Por favor confirme que quer pesquisar pelo tipo de estabelecimento " + e.Result.Semantics.ToArray()[1].Value.Value.ToString());
  497. break;
  498.  
  499. }
  500.  
  501.  
  502. }
  503.  
  504. return false;
  505.  
  506. }
  507.  
  508. private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  509. {
  510. onRecognized(new SpeechEventArg(){Text = e.Result.Text, Confidence = e.Result.Confidence, Final = true});
  511.  
  512. //SEND
  513. // IMPORTANT TO KEEP THE FORMAT {"recognized":["SHAPE","COLOR"]}
  514. string json = "{ \"recognized\": [";
  515. foreach (var resultSemantic in e.Result.Semantics)
  516. {
  517. json+= "\"" + resultSemantic.Value.Value +"\", ";
  518. }
  519. json = json.Substring(0, json.Length - 2);
  520. json += "] }";
  521.  
  522. var exNot = lce.ExtensionNotification(e.Result.Audio.StartTime+"", e.Result.Audio.StartTime.Add(e.Result.Audio.Duration)+"",e.Result.Confidence, json);
  523.  
  524. bool pR = processRequest(e);
  525.  
  526. if (pR)
  527. {
  528. mmic.Send(exNot);
  529. }
  530.  
  531. }
  532. }
  533. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement