Created
September 18, 2015 16:53
-
-
Save Carreau/5a156bf6a46f1d2ca9b6 to your computer and use it in GitHub Desktop.
IPython voice recognition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(){ | |
"use strict"; | |
var Jupyter = require('base/js/namespace'); | |
var norm = function(initial){ | |
return initial | |
.replace('open parentheses','( ') | |
.replace('open parenthesis','(') | |
.replace('open bracket','(') | |
.replace('hash','#') | |
.replace('close parenthesis','(') | |
.replace('close parentheses', ')') | |
.replace('close bracket', ')') | |
.replace(':', ':\n ') | |
.replace('colon', ':\n ') | |
.replace('equal', '=') | |
.replace('equals', '=') | |
.replace('new line', '\n') | |
.replace('Plus', '+') | |
.replace('triple quote', '"""\n') | |
.replace('quote', '"') | |
.replace('Define function', 'def') | |
.replace('define function', 'def') | |
.replace('Jupiter', 'Jupyter') | |
} | |
var dictate = { | |
help: 'Dictate', | |
icon : 'fa-microphoone', | |
help_index : '', | |
handler : function (env) { | |
var recognition = new webkitSpeechRecognition(); | |
env.notebook.edit_mode(); | |
recognition.continuous = true; | |
recognition.interimResults = true; | |
var pause = false; | |
var final_length = 0; | |
recognition.onresult = function(event) { | |
if(!event.results){ | |
return; | |
} | |
var res = event.results[event.results.length-1] | |
var sure = ''; | |
var incertain = ''; | |
for(var i=final_length; i<event.results.length; i++){ | |
var r = event.results[i]; | |
if(r.isFinal){ | |
sure += r[0].transcript | |
final_length = Math.max(i+1, final_length); | |
} else { | |
incertain += r[0].transcript; | |
} | |
console.log(final_length); | |
} | |
if(incertain.match(/[s|S]top$/)){ | |
recognition.stop() | |
return | |
} | |
console.log(sure, '|', incertain); | |
env.notebook.get_selected_cell().code_mirror.replaceSelection(norm(sure)); | |
env.notebook.get_selected_cell().code_mirror.replaceSelection(norm(incertain), "around"); | |
} | |
recognition.start(); | |
} | |
} | |
var load_extension = function(){ | |
var _dictate = Jupyter.keyboard_manager.actions.register(dictate, 'dictate', 'accessibility') | |
console.warn(dictate) | |
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Shift-Q', _dictate) | |
} | |
return { load_ipython_extension: load_extension } | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.replace('close parenthesis','(') #<<< Looks wrong?