javascript - HTML 语音识别中的 if then 语句
问题描述
所以我正在尝试制作一个 html 语音助手,并且我正在努力处理 if/then 语句。这是我到目前为止所拥有的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- Input area -->
<p id="output">Output:</p>
<label for="Argon">Argon</label>
<input type="text" name="" id="speechToText" placeholder="Say a Command" onclick="record()">
<button type="button" onclick="record()">Start Listening</button>
<button type="button" onclick ="record()">Stop Listening</button>
<!-- Below is the script for voice recognition and conversion to text-->
<script>
function record() {
var recognition = new webkitSpeechRecognition();
recognition.lang = "en-GB";
recognition.onresult = function(event) {
// console.log(event);
document.getElementById('speechToText').value = event.results[0][0].transcript;
}
recognition.start();
}
if transcript = "hello"; {
document.getElementById("output").innerHTML = "YESSS";
}
</script>
<!-- end of script -->
</body>
</html>
我的问题是我找不到将文本从语音识别转换为 if/then 语句的方法。例如,如果我在语音识别中说“你好”,javascript 会看到“你好”,并且在代码中它会返回“你好用户”或其他东西。我怎么能这样。如果有人能给我一个答案,这将非常有帮助,因为那时我可以继续添加命令。我已经坚持了很长一段时间了,所以如果有人可以帮忙,那就太好了。同样,这是我正在处理的部分:
<script>
function record() {
var recognition = new webkitSpeechRecognition();
recognition.lang = "en-GB";
recognition.onresult = function(event) {
// console.log(event);
document.getElementById('speechToText').value = event.results[0][0].transcript;
}
recognition.start();
}
if transcript = "hello"; {
document.getElementById("output").innerHTML = "YESSS";
}
</script>
这是我在这里的第一篇文章,所以我是这个社区的新手,所以我希望我正确清晰地发布了这个问题。谢谢 :)
解决方案
您的代码中有几个问题:
- 您的 if 语句有语法错误。
if (condition == true)
是 javascript 中的正确语法。请参阅W3Schools 教程 - 你
transcript
的没有定义。根据您的描述,我认为您希望将其嵌套到onresult
事件中,一旦识别出单词或短语就会触发。
这是一个工作示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- Input area -->
<p id="output">Output:</p>
<label for="Argon">Argon</label>
<input type="text" name="" id="speechToText" placeholder="Say a Command" onclick="record()">
<button type="button" onclick="record()">Start Listening</button>
<button type="button" onclick ="record()">Stop Listening</button>
<!-- Below is the script for voice recognition and conversion to text-->
<script>
function record() {
var recognition = new webkitSpeechRecognition();
recognition.lang = "en-GB";
recognition.start();
recognition.onresult = function(event) {
let transcript = event.results[0][0].transcript;
if (transcript === "hello") {
document.getElementById("output").innerHTML = "YESSS";
}
document.getElementById('speechToText').value = event.results[0][0].transcript;
}
}
</script>
<!-- end of script -->
</body>
</html>
推荐阅读
- r - 在文本分析中使用德语字符(ü、ö、ä 等) (R)
- java - 什么时候在构造函数中实例化实例变量/对象与在构造函数之前实例化实例变量/对象更好
- python - Python 2 中的 256 AES 加密
- powershell - Powershell $MyInvocation.MyCommand.Path 在 exe 中转换脚本时返回 null
- django - Django - 如何设置 2 个具有相同字段的模型?
- python - 在 python 中,startswith() 解析大文本文件的问题
- python - 从子硬盘驱动器读取图像以进行张量流
- java - Java中二维列表中的短路径
- unity3d - 使用来自主场景的信息测试场景
- curl - axios:在axios中传递curl的-d