Skip to content

Instantly share code, notes, and snippets.

@Carreau

Carreau/main.js

Created Sep 18, 2015
Embed
What would you like to do?
IPython voice recognition
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 }
})
@johnowhitaker

This comment has been minimized.

Copy link

@johnowhitaker johnowhitaker commented Jan 1, 2020

.replace('close parenthesis','(') #<<< Looks wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.