如何在语音识别成绩单上执行正则表达式?
How to perform regular expressions on a Speech Recognition transcript?
我的申请摘要:
- 输入:让用户对着电脑说话(询问电脑
"What is your name?"
)✅
- 对 'transcript' 结果执行正则表达式(来自
webkitSpeechRecognition
) ❌
- 输出:
console.log("My name is Harry Johnson")
。 ❌
我正在尝试对语音识别记录执行正则表达式,如下所示:
var speechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
var recognition = new webkitSpeechRecognition();
var transcript;
recognition.addEventListener('result', e => {
transcript = Array.from(e.results)
.map(result => result[0])
.map(result => result.transcript)
.join('');
console.log(transcript);
});
recognition.addEventListener('end',recognition.start);
window.addEventListener('DOMContentLoaded', function() {
document.getElementById("speak_button").addEventListener('click', function() {
recognition.start();
});
});
以上工作正常,我能够 console.log 来自用户的 transcript/speech。问题来自以下代码,我认为这与成绩单必须是字符串这一事实有关?我已经尝试使用 String(transcript) 和 transcript.toString() 将成绩单转换为字符串,但我仍然没有实现我的结果:这是让计算机 console.log “我的名字是哈里·约翰逊".
function speakBackToMe() {
/*regular expression that will match words in the transcript */
var person_name_regex = /what is your name|(?=.*\byour\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bcan\b)(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\blet\b)(?=.*\bknow\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\bgrab\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bwhat\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bshare\b)(?=.*\bme\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\bwhat\b)(?=.*\byour\b)(?=.*\bfirst\b)(?=.*\band\b)(?=.*\blast\b)(?=.*\bname\b)/ig;
/*if regular expression matches all words, then function will be performed*/
if (person_name_regex.test(transcript)) {
console.log("My name is Harry Johnson");
}
}
speakBackToMe();
正则表达式是正确的,主要问题是 transcript
变量。如果有人对为什么不执行 console.log("My name is Harry Johnson")
函数有任何建议,请与我分享,我们将不胜感激! :)
谢谢!
对我来说,我只是把 speakBackToMe(); console.log(成绩单)下方的行;在结果事件侦听器块中并且有效
recognition.addEventListener('result', e => {
transcript = Array.from(e.results)
.map(result => result[0])
.map(result => result.transcript)
.join('');
console.log(transcript);
speakBackToMe();
});
我的申请摘要:
- 输入:让用户对着电脑说话(询问电脑
"What is your name?"
)✅ - 对 'transcript' 结果执行正则表达式(来自
webkitSpeechRecognition
) ❌ - 输出:
console.log("My name is Harry Johnson")
。 ❌
我正在尝试对语音识别记录执行正则表达式,如下所示:
var speechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
var recognition = new webkitSpeechRecognition();
var transcript;
recognition.addEventListener('result', e => {
transcript = Array.from(e.results)
.map(result => result[0])
.map(result => result.transcript)
.join('');
console.log(transcript);
});
recognition.addEventListener('end',recognition.start);
window.addEventListener('DOMContentLoaded', function() {
document.getElementById("speak_button").addEventListener('click', function() {
recognition.start();
});
});
以上工作正常,我能够 console.log 来自用户的 transcript/speech。问题来自以下代码,我认为这与成绩单必须是字符串这一事实有关?我已经尝试使用 String(transcript) 和 transcript.toString() 将成绩单转换为字符串,但我仍然没有实现我的结果:这是让计算机 console.log “我的名字是哈里·约翰逊".
function speakBackToMe() {
/*regular expression that will match words in the transcript */
var person_name_regex = /what is your name|(?=.*\byour\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bcan\b)(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\blet\b)(?=.*\bknow\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\bgrab\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bwhat\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bshare\b)(?=.*\bme\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\bwhat\b)(?=.*\byour\b)(?=.*\bfirst\b)(?=.*\band\b)(?=.*\blast\b)(?=.*\bname\b)/ig;
/*if regular expression matches all words, then function will be performed*/
if (person_name_regex.test(transcript)) {
console.log("My name is Harry Johnson");
}
}
speakBackToMe();
正则表达式是正确的,主要问题是 transcript
变量。如果有人对为什么不执行 console.log("My name is Harry Johnson")
函数有任何建议,请与我分享,我们将不胜感激! :)
谢谢!
对我来说,我只是把 speakBackToMe(); console.log(成绩单)下方的行;在结果事件侦听器块中并且有效
recognition.addEventListener('result', e => {
transcript = Array.from(e.results)
.map(result => result[0])
.map(result => result.transcript)
.join('');
console.log(transcript);
speakBackToMe();
});