我正在开发一个可以检测某些关键词的应用程序。我一直在使用谷歌的网络语音。
我能够记录用户的声音并将其打印出来,但我希望能够检测到某些单词。
我似乎找不到这样做的方法。我发现了这个问题Detecting known words using the Web Speech API,但它链接到的答案中的站点已经消失。
任何人都可以对此有所了解吗?任何帮助都会很棒。干杯! 这是一个小提琴
var recognition = new webkitSpeechRecognition(); // google voice recognition start
recognition.continuous = true; //so that recognition will continue even if the user pauses while speaking
document.getElementById('startSpeech').addEventListener('click', function() {
startSpeech();
});
document.getElementById('stopRecognizing').addEventListener('click', function() {
stopRecognizing();
});
var startSpeech = function(){
var lang = ['en-GB', 'United Kingdom'];
final_transcript = '';
recognition.lang = recognition.lang
recognition.start() //activates the speech recognizer
}
var stopRecognizing = function(){
recognition.stop();//stops the mic from listening
return;
}
recognition.onresult = function(event) {
console.log('event', event);
// This handler concatenates all
// the results received so far into two strings final_transcript & interim_transcript
var interim_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
//final_transcript = capitalize(final_transcript);
final_span.innerHTML = linebreak(final_transcript);
interim_span.innerHTML = linebreak(interim_transcript);
// console.log('final_transcript' , typeof final_transcript);
// console.log('interim_transcript' , interim_transcript);
if (final_transcript.length > 0) {
matchingKeyWords(final_transcript);
}
};
function linebreak(s) {
var two_line = /\n\n/g;
var one_line = /\n/g;
return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
function capitalize(s) {
var first_char = /\S/;
return s.replace(first_char, function(m) { return m.toUpperCase(); });
}
var matchingKeyWords = function(conversation){
var keyWords = ['power', 'tie', 'imaginary' ,'bomb', 'drink', 'underwear', 'chance', 'type', 'historical',
'society' ,'collect' ,'sugar']
for (var i = 0; i < keyWords.length; ++i) {
console.log(conversation[0]);
if (conversation === keyWords[1]) {
console.log('hit');
}
// console.log('keyWords[i]', keyWords[i]);
}
}