Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. /*
  2. * This is a JavaScript Scratchpad.
  3. *
  4. * Enter some JavaScript, then Right Click or choose from the Execute Menu:
  5. * 1. Run to evaluate the selected text (Ctrl+R),
  6. * 2. Inspect to bring up an Object Inspector on the result (Ctrl+I), or,
  7. * 3. Display to insert the result in a comment after the selection. (Ctrl+L)
  8. */
  9.  
  10. var SpeechRecognition = SpeechRecognition;// || webkitSpeechRecognition
  11. var SpeechGrammarList = SpeechGrammarList;// || webkitSpeechGrammarList
  12. var SpeechRecognitionEvent = SpeechRecognitionEvent;// || webkitSpeechRecognitionEvent
  13.  
  14. var colors = [ 'aqua' , 'azure' , 'beige', 'bisque', 'black', 'blue', 'brown', 'chocolate', 'coral', 'crimson', 'cyan', 'fuchsia', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'indigo', 'ivory', 'khaki', 'lavender', 'lime', 'linen', 'magenta', 'maroon', 'moccasin', 'navy', 'olive', 'orange', 'orchid', 'peru', 'pink', 'plum', 'purple', 'red', 'salmon', 'sienna', 'silver', 'snow', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'white', 'yellow'];
  15. var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + colors.join(' | ') + ' ;'
  16.  
  17. var recognition = new SpeechRecognition();
  18. var speechRecognitionList = new SpeechGrammarList();
  19. speechRecognitionList.addFromString(grammar, 1);
  20. recognition.grammars = speechRecognitionList;
  21. //recognition.continuous = false;
  22. recognition.lang = 'en-US';
  23. recognition.interimResults = false;
  24. recognition.maxAlternatives = 1;
  25.  
  26. var colorHTML= '';
  27. colors.forEach(function(v, i, a){
  28. console.log(v, i);
  29. colorHTML += '<span style="background-color:' + v + ';"> ' + v + ' </span>';
  30. });
  31.  
  32. /* ------------ ISSUE HERE --------------------------------------------------------------------------- */
  33. recognition.start();
  34. console.log('Ready to receive a color command.');
  35. /* ---------------------------------------------------------------------------------------------------- */
  36.  
  37. recognition.onresult = function(event) {
  38. var last = event.results.length - 1;
  39. var color = event.results[last][0].transcript;
  40. console.log('Confidence: ' + event.results[0][0].confidence);
  41. }
  42.  
  43. recognition.onspeechend = function() {
  44. recognition.stop();
  45. }
  46.  
  47. recognition.onnomatch = function(event) {
  48. diagnostic.textContent = "I didn't recognise that color.";
  49. }
  50.  
  51. recognition.onerror = function(event) {
  52. diagnostic.textContent = 'Error occurred in recognition: ' + event.error;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement