Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # EGHV stands for Events, Grammars, Handlers & Validations
  2. Recognee = (EGHV, language = 'ru-RU', continuous = true) ->
  3.  
  4.     # Public properties
  5.     this.language = language;
  6.     this.continuous = continuous;
  7.    
  8.     # Private one
  9.     recognition = null
  10.  
  11.     # Settings
  12.     setRecognition = () ->
  13.         SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
  14.         SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent
  15.  
  16.         recognition = new SpeechRecognition
  17.  
  18.     setLanguage = (_this) ->
  19.         recognition.lang = _this.language
  20.  
  21.     setGrammar = () ->
  22.         SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
  23.         words = []
  24.         for event of EGHV
  25.             EGHV[event].grammar.forEach (word) -> words.push word
  26.         speechList = new SpeechGrammarList
  27.         grammar = '#JSGF V1.0; grammar playercontrols; public <player> = ' + words.join(' | ') + ' ;';
  28.         speechList.addFromString grammar
  29.         recognition.grammars = speechList
  30.  
  31.     makeContinuous = (_this) ->
  32.         if _this.continuous
  33.             recognition.addEventListener "end", () -> recognition.start()
  34.  
  35.     # Internal functions
  36.  
  37.     # Common event handling
  38.     handle = (event, vocabulary, handler, recognized,  validations = undefined) ->
  39.         result = recognized.results[0][0].transcript.toLowerCase()
  40.        
  41.         unless validations
  42.             vocabulary.forEach (word) ->
  43.                 if result.indexOf(word) != -1 then handler()
  44.         else
  45.             if(eval(validations))
  46.                 vocabulary.forEach (word) ->
  47.                     if result.indexOf(word) != -1 then handler()
  48.  
  49.     # Common listener
  50.     listen = () ->
  51.         recognition.addEventListener "result", (recognized) ->
  52.             result = recognized.results[0][0].transcript.toLowerCase()
  53.             console.log(result)
  54.             for event of EGHV
  55.                 handle event, EGHV[event].grammar, EGHV[event].handler, recognized, EGHV[event].validation
  56.  
  57.     # Ride
  58.     this.recognize = () ->
  59.  
  60.         setRecognition()
  61.         setLanguage(this)
  62.         setGrammar()
  63.         makeContinuous(this)
  64.         listen(this)
  65.        
  66.         recognition.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement