首页 > 解决方案 > 如何为单词“句号”转义谷歌浏览器 webkitSpeechRecognition 以防止“。” 解释?

问题描述

我正在使用 HTML 和 JavaScript 开发一个简单的语音识别页面。Google Chrome 的语音识别实现的 API 文档中显然未定义某些保留字和短语。

以下是一些常见的例子:

  1. “新段落” - 效果很好且不含糊
  2. "句号" - 无条件返回字符 "." 即使你拼出麦克风“perio d”中的字母,它仍然会输出“.”。
  3. "comma" - 无条件返回字符 "," 即使你在麦克风中拼出字母 "comm a" 它仍然输出 ","。

可能还有更多,我很想得到一份完整的清单。

有谁知道如何或说什么来逃避“。” 拼出“句号”这个词?

这是我的 chrome-speech-to-text.php 的测试代码

    <?php 
        simple_speech();
    ?>
    <?php 
    
    function simple_speech(){
    ?>
    <!DOCTYPE html>
    <meta charset="utf-8">
    <title>Simple Speech to Text</title>
    
    <?php
    $browser="";
             if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("MSIE"))){?>
    <p id="info_upgrade" style="text-align:center;">Simple Speech to Text is not supported by this browser.
        Upgrade to <a href="//www.google.com/chrome">Chrome</a>
        version 25 or later.</p>
    <?php }
             else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("Presto")))
             {
                     //$browser="opera";
             }
             else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("CHROME")))
             {?>
    
    <?php 
             }
             else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("SAFARI")))
             { ?>
    <p id="info_upgrade" style="text-align:center;">Simple Speech to Text is not supported by this browser.
        Upgrade to <a href="//www.google.com/chrome">Chrome</a>
        version 25 or later.</p>
    <?php }
             else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("FIREFOX")))
             { ?>
    <p id="info_upgrade" style="text-align:center;">Simple Speech to Text is not supported by this browser.
        Upgrade to <a href="//www.google.com/chrome">Chrome</a>
        version 25 or later.</p>
    <?php }
             else 
             {
             //$browser="other";
             }
    echo $browser;
    ?>
    
    <div style="margin-bottom:50px">
        <div style="width:100%">
            <textarea name="q" id="transcript" rows="15" style="width:100% !important;"></textarea>
        </div>
        <div style="margin-top: 20px;margin-left: 10px;float:left;">
            <a id="copy_dictation" onclick="speakcopy();" style="padding: 6px 11px;background-color:#2111c4;cursor:pointer;color:#fff;margin-top:10px;border-radius:5px;"> Copy</a>
        </div>
        <div style="margin-top: 20px;margin-left: 10px;float:left;">
            <a id="clear_dictation" onclick="speakclear()" style="padding: 6px 11px;background-color:#e5225c;cursor:pointer;color:#fff;margin-top:10px;border-radius:5px;"> Clear</a>
        </div>
    </div>
    <!-- HTML5 Speech Recognition API -->
    <script>
        startDictation()
    
        function startDictation() {
    
            var recognition = new webkitSpeechRecognition();
            console.log(recognition);
            recognition.continuous = true;
            recognition.interimResults = true;
            recognition.lang = "en";
    
            recognition.onend = function(e) {
                console.log('ended');
                var textarea = document.getElementById('transcript');
                if (textarea.value != '') {
                    textarea.value = textarea.value.trim();
                    textarea.value += ' ';
    
                }
                recognition.start();
            }
    
            recognition.onresult = function(e) {
                var textarea = document.getElementById('transcript');
                for (var i = e.resultIndex; i < e.results.length; ++i) {
                    if (e.results[i].isFinal) {
                        textarea.value += e.results[i][0].transcript;
                    }
                }
            }
    
            // start listening
            recognition.start();
        }
    
        function speakcopy() {
            var recognition = new webkitSpeechRecognition();
            recognition.stop();
            copyToClipboard(document.getElementById("transcript"));
        }
    
        function speakclear() {
            var recognition = new webkitSpeechRecognition();
            recognition.stop();
            document.getElementById("transcript").value = "";
        }
    
    
    
    
        function copyToClipboard(elem) {
    
            // create hidden text element, if it doesn't already exist
            var targetId = "_hiddenCopyText_";
            var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
            var origSelectionStart, origSelectionEnd;
            if (isInput) {
                // can just use the original source element for the selection and copy
                target = elem;
                origSelectionStart = elem.selectionStart;
                origSelectionEnd = elem.selectionEnd;
            } else {
                // must use a temporary form element for the selection and copy
                target = document.getElementById(targetId);
                if (!target) {
                    var target = document.createElement("textarea");
                    target.style.position = "absolute";
                    target.style.left = "-9999px";
                    target.style.top = "0";
                    target.id = targetId;
                    document.body.appendChild(target);
                }
                target.textContent = elem.textContent;
            }
            // select the content
            var currentFocus = document.activeElement;
            target.focus();
            target.setSelectionRange(0, target.value.length);
    
            // copy the selection
            var succeed;
            try {
                succeed = document.execCommand("copy");
            } catch (e) {
                succeed = false;
            }
            // restore original focus
            if (currentFocus && typeof currentFocus.focus === "function") {
                currentFocus.focus();
            }
    
            if (isInput) {
                // restore prior selection
                elem.setSelectionRange(origSelectionStart, origSelectionEnd);
            } else {
                // clear temporary content
                target.textContent = "";
            }
            return succeed;
        }
    
    </script>
    <?php 
    }
    ?>

标签: javascriptapigoogle-chromespeech-recognitionreserved-words

解决方案


很抱歉回答我自己的问题,但我有一个严格的截止日期。这是一个对着麦克风说话的例句。“这是一个伟大的时间点”解决方案分为两部分:

  1. 对句点字符说“点”。
  2. 遍历备选的最终结果以找到单词“period”并使用该备选方案。(返回“这是一个伟大的时期。”)

这是带有替代最终结果迭代的清理代码(注意:文件必须具有“.PHP”扩展名!它不适用于 HTML 扩展名“)我使用了文件名 chrome-speech-to-text.php

<html>
  <head>
    <meta charset="utf-8">
    <title>Simple Speech to Text</title>
  </head>

  <body>

    <div style="margin-bottom:50px">
      <div style="width:100%">
        <textarea name="q" id="transcript" rows="15" style="width:100% !important;"></textarea>
      </div>
      <div style="margin-top: 20px;margin-left: 10px;float:left;">
        <a id="copy_dictation" onclick="speakcopy();" style="padding: 6px 11px;background-color:#2111c4;cursor:pointer;color:#fff;margin-top:10px;border-radius:5px;"> Copy</a>
      </div>
      <div style="margin-top: 20px;margin-left: 10px;float:left;">
        <a id="clear_dictation" onclick="speakclear()" style="padding: 6px 11px;background-color:#e5225c;cursor:pointer;color:#fff;margin-top:10px;border-radius:5px;"> Clear</a>
      </div>
    </div>
    <script>
      startDictation()

      function startDictation() {

        var recognition = new webkitSpeechRecognition();
        console.log(recognition);
        recognition.continuous = true;
        recognition.interimResults = true;
        recognition.lang = "en";
        recognition.maxAlternatives = 10;

        recognition.onend = function(e) {
          //console.log('ended');
          var textarea = document.getElementById('transcript');
          if (textarea.value != '') {
            textarea.value = textarea.value.trim();
            textarea.value += ' ';

          }
          recognition.start();
        }

        recognition.onresult = function(e) {
          //console.log(e); // DO NOT UNCOMMENT ELSE ENDLESS LOOP
          //e.results[0][0].forEach(iterate);
          var textarea = document.getElementById('transcript');
          var finalText = '';
          for (var i = e.resultIndex; i < e.results.length; ++i) {
            if (e.results[i].isFinal) {
              var alternatePeriod = searchForAlternative(e.results[i], 'period');
              if (alternatePeriod != '') {
                textarea.value += alternatePeriod;
              } else {
                textarea.value += e.results[i][0].transcript;
              }
            }
          }
        }

        // start listening
        recognition.start();
      }

      function searchForAlternative(resultItem, searchString) {
        for (var i = 0; i < resultItem.length; ++i) {
          //console.log(resultItem[i].transcript);
          if (resultItem[i].transcript.includes(searchString)) {
            //console.log(resultItem[i].transcript);
            return (resultItem[i].transcript);;
          }
        }
        return ('');
      }

      function speakcopy() {
        var recognition = new webkitSpeechRecognition();
        recognition.stop();
        copyToClipboard(document.getElementById("transcript"));
      }

      function speakclear() {
        var recognition = new webkitSpeechRecognition();
        recognition.stop();
        document.getElementById("transcript").value = "";
      }

      function copyToClipboard(elem) {

        // create hidden text element, if it doesn't already exist
        var targetId = "_hiddenCopyText_";
        var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
        var origSelectionStart, origSelectionEnd;
        if (isInput) {
          // can just use the original source element for the selection and copy
          target = elem;
          origSelectionStart = elem.selectionStart;
          origSelectionEnd = elem.selectionEnd;
        } else {
          // must use a temporary form element for the selection and copy
          target = document.getElementById(targetId);
          if (!target) {
            var target = document.createElement("textarea");
            target.style.position = "absolute";
            target.style.left = "-9999px";
            target.style.top = "0";
            target.id = targetId;
            document.body.appendChild(target);
          }
          target.textContent = elem.textContent;
        }
        // select the content
        var currentFocus = document.activeElement;
        target.focus();
        target.setSelectionRange(0, target.value.length);

        // copy the selection
        var succeed;
        try {
          succeed = document.execCommand("copy");
        } catch (e) {
          succeed = false;
        }
        // restore original focus
        if (currentFocus && typeof currentFocus.focus === "function") {
          currentFocus.focus();
        }

        if (isInput) {
          // restore prior selection
          elem.setSelectionRange(origSelectionStart, origSelectionEnd);
        } else {
          // clear temporary content
          target.textContent = "";
        }
        return succeed;
      }

    </script>
  </body>

</html>

推荐阅读