Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # EGHV stands for Events, Grammars, Handlers & Validations
- Recognee = (EGHV, language = 'ru-RU', continuous = true) ->
- # Public properties
- this.language = language;
- this.continuous = continuous;
- # Private one
- recognition = null
- # Settings
- setRecognition = () ->
- SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
- SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent
- recognition = new SpeechRecognition
- setLanguage = (_this) ->
- recognition.lang = _this.language
- setGrammar = () ->
- SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
- words = []
- for event of EGHV
- EGHV[event].grammar.forEach (word) -> words.push word
- speechList = new SpeechGrammarList
- grammar = '#JSGF V1.0; grammar playercontrols; public <player> = ' + words.join(' | ') + ' ;';
- speechList.addFromString grammar
- recognition.grammars = speechList
- makeContinuous = (_this) ->
- if _this.continuous
- recognition.addEventListener "end", () -> recognition.start()
- # Internal functions
- # Common event handling
- handle = (event, vocabulary, handler, recognized, validations = undefined) ->
- result = recognized.results[0][0].transcript.toLowerCase()
- unless validations
- vocabulary.forEach (word) ->
- if result.indexOf(word) != -1 then handler()
- else
- if(eval(validations))
- vocabulary.forEach (word) ->
- if result.indexOf(word) != -1 then handler()
- # Common listener
- listen = () ->
- recognition.addEventListener "result", (recognized) ->
- result = recognized.results[0][0].transcript.toLowerCase()
- console.log(result)
- for event of EGHV
- handle event, EGHV[event].grammar, EGHV[event].handler, recognized, EGHV[event].validation
- # Ride
- this.recognize = () ->
- setRecognition()
- setLanguage(this)
- setGrammar()
- makeContinuous(this)
- listen(this)
- recognition.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement