Вот версия CoffeeScript для тех, кто предпочитает это:
Array.prototype.equals = (array) ->
return false if not array # if the other array is a falsy value, return
return false if @length isnt array.length # compare lengths - can save a lot of time
for item, index in @
if item instanceof Array and array[index] instanceof Array # Check if we have nested arrays
if not item.equals(array[index]) # recurse into the nested arrays
return false
else if this[index] != array[index]
return false # Warning - two different object instances will never be equal: {x:20} != {x:20}
true
Все кредиты передаются @ tomas-zato.
Ванильный способ сделать это - использовать созданный для него Web API: SpeechRecognition , который в настоящее время поддерживается только в Chrome, и я не знаю, почему, но не в iframes. в настоящее время сделать живой пример, к сожалению, невозможно ...
В любом случае, вот базовый пример, который вы можете использовать на своей странице.
const magic_word = ###Some magic word###;
// initialize our SpeechRecognition object
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.continuous = true;
// when we get some results
recognition.onresult = e => {
// extract all the transcripts
const transcripts = [].concat.apply([], [...e.results]
.map(res => [...res]
.map(alt => alt.transcript)
)
);
// do something with the transcripts,
// here we are searching for our magic word
if(transcripts.some(t => t.indexOf(magic_word) > -1)){
//do something awesome, like starting your own command listeners
console.log('hello user');
recognition.stop();
}
else{
// didn't understood...
console.log("didn't got what you said", transcripts)
}
}
// start on click of a button
btn.onclick = e => {
recognition.stop();
recognition.start();
};
Чтобы понять, как это работает под капотом, вы можете проверить проект Mozilla с открытым исходным кодом DeepSpeech , основанный на исследовательских работ Baidu Deep Speech .
Итак, чтобы было ясно, это не javascript, а реализация Chrome все еще передается на их сервер. Если вы хотите что-то построить самостоятельно, будьте готовы провести долгие ночи; -)