【问题标题】:How to search google with speech recognition from your website?如何从您的网站使用语音识别搜索谷歌?
【发布时间】:2019-11-25 06:56:31
【问题描述】:

我在网络浏览器中有一个“Jarvis”或个人助理。它运行在一些语音识别代码上。我会放在底部。我想告诉它谷歌一些东西,然后告诉它我想让它谷歌什么,然后让它自动为我谷歌它。

你能帮忙吗? 只是为了让您知道语音识别工作正常,但说明中没有显示激活它的按钮。

<script>

        var x = document.getElementById("reconition");
        var message = document.querySelector('#message');
        var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
        var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
        var grammar = '#JSGF V1.0;'
        var recognition = new SpeechRecognition();
        var speechRecognitionList = new SpeechGrammarList();
                speechRecognitionList.addFromString(grammar, 1);
                recognition.grammars = speechRecognitionList;
                recognition.lang = 'en-US';
                recognition.interimResults = false;
                recognition.onresult = function(event) {
                        var last = event.results.length - 1;
                        var command = event.results[last][0].transcript;
                        message.textContent = 'Voice Input: ' + command + '.';



                        if(command.toLowerCase() === 'google something'){
                                var msg = new SpeechSynthesisUtterance('what would you like for me to google');
                window.speechSynthesis.speak(msg);
                        }
  }




                    };
            recognition.onspeechend = function() {
                recognition.stop();
            };
            recognition.onerror = function(event) {
                message.textContent = 'Error occurred in recognition: ' + event.error;
            }
            document.querySelector('#btnGiveCommand').addEventListener('click', function(){
                recognition.start();
            });

【问题讨论】:

  • 您的问题不清楚。如果您的代码有效,那么您已经解决了自己的问题。
  • 不,我想用谷歌搜索我说的话,这只测试我说的话

标签: javascript html speech-recognition google-speech-api


【解决方案1】:

好吧,由于您没有指定之后要对结果进行任何处理,因此您可以打开一个弹出窗口/使用 iframe 并使用搜索查询打开一个 google url。例如:

https://www.google.com/search?q=this+is+a+test

将打开 google 并查询“这是一个测试”

q= 参数实际上负责查询。

现在,如果您想以某种方式使用页面上的结果,一个选项可能是 googles 客户搜索 api:https://developers.google.com/custom-search/v1/overview

【讨论】:

  • 这可行,但我已经做过一些类似的事情,我希望它搜索我所说的内容,而不必输入它,如果可能的话,idk,但我想告诉它要搜索什么,而我的语音检测只听某些单词。
  • 如果我错了,请纠正我,但网络语音 api 也不能在没有语法的情况下接收整个文本吗?你肯定需要一个选项来将你的口语变成文本。否则你想存档的东西是不可能的。如果网络语音 api 做不到,还有 Google 的 Cloud Speech API,但是如果你使用超过 60 分钟就会花钱,所以我猜可能不是你的选择?
  • 是的,我不想花钱,我只是一个 13 岁的孩子,想做一个很酷的私人助理,我已经做了很多,但我真的认为这个功能可能是最好的之一
【解决方案2】:

如果有人想要这个问题的答案,我前段时间确实想出来了,但忘记发布了。

else if (command.includes("Google")){
    var search = command.substring(command.indexOf("+google")+7);
    window.open('https://www.google.com/search?q=' + search, "mywindow", "width=420, height=420, top=0,");
}

这将打开一个新窗口,其中包含您在 google 之后所说的任何内容的 google 搜索。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多