首页 > 解决方案 > 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>

这是我在这里的第一篇文章,所以我是这个社区的新手,所以我希望我正确清晰地发布了这个问题。谢谢 :)

标签: javascripthtml

解决方案


您的代码中有几个问题:

  • 您的 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>

如果你说“你好”,你应该看到: 结果


推荐阅读