Class: JsSpeechRecognition
- Inherits:
-
Object
- Object
- JsSpeechRecognition
- Defined in:
- lib/jsspeech2019.rb
Instance Method Summary collapse
-
#initialize(lang: 'en-GB') ⇒ JsSpeechRecognition
constructor
A new instance of JsSpeechRecognition.
- #to_css ⇒ Object
- #to_html ⇒ Object
- #to_js ⇒ Object
- #to_webpage ⇒ Object
Constructor Details
#initialize(lang: 'en-GB') ⇒ JsSpeechRecognition
Returns a new instance of JsSpeechRecognition.
13 14 15 |
# File 'lib/jsspeech2019.rb', line 13 def initialize(lang: 'en-GB') @lang = lang end |
Instance Method Details
#to_css ⇒ Object
69 70 71 |
# File 'lib/jsspeech2019.rb', line 69 def to_css() '' end |
#to_html ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/jsspeech2019.rb', line 17 def to_html() <<HTML <button id="start-button">Start Listening</button> <p><strong>You said:</strong> <em id="demo-echo">nothing yet</em>.</p> HTML end |
#to_js ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/jsspeech2019.rb', line 25 def to_js() <<JS function startListening() { var recognition = new (webkitSpeechRecognition || SpeechRecognition)(); recognition.lang = '#{@lang}'; recognition.interimResults = false; recognition.maxAlternatives = 1; recognition.start(); [ 'onaudiostart', 'onaudioend', 'onend', 'onerror', 'onnomatch', 'onresult', 'onsoundstart', 'onsoundend', 'onspeechend', 'onstart' ].forEach(function(eventName) { recognition[eventName] = function(e) { console.log(eventName, e); }; }); document.querySelector('#start-button').innerHTML = 'Listening...'; recognition.onend = function() { document.querySelector('#start-button').innerHTML = 'Start Listening'; }; recognition.onresult = function() { document.querySelector('#demo-echo').textContent = event.results[0][0].transcript; }; }; (function() { document.querySelector('#start-button').addEventListener('click', startListening); })(); JS end |
#to_webpage ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jsspeech2019.rb', line 73 def to_webpage() a = RexleBuilder.build do |xml| xml.html do xml.head do xml.title 'Speech Recognition' xml. name: "viewport", content: \ "width=device-width, initial-scale=1" xml.style "\nbody {font-family: Arial;}\n\n" + to_css() end xml.body end end doc = Rexle.new(a) e = Rexle.new("<html>%s</html>" % to_html()).root e.children.each {|child| doc.root.element('body').add child } doc.root.element('body').add \ Rexle::Element.new('script').add_text "\n" + to_js().gsub(/^ +\/\/[^\n]+\n/,'') "<!DOCTYPE html>\n" + doc.xml(pretty: true, declaration: false)\ .gsub(/<\/div>/,'\0' + "\n").gsub(/\n *<!--[^>]+>/,'') end |