首页 文章 精选 留言 我的

精选列表

搜索[腾讯HR助手],共10001篇文章
优秀的个人博客,低调大师

用 80 行 Javascript 代码构建自己的语音助手

云栖号资讯:【点击查看更多行业资讯】在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! 在本教程中,我们将使用 80 行 JavaScript 代码在浏览器中构建一个虚拟助理(如 Siri 或 Google 助理)。你可以在这里测试这款应用程序,它将会听取用户的语音命令,然后用合成语音进行回复。 你所需要的是: Google Chrome (版本 25 以上)一款文本编辑器 由于 Web Speech API 仍处于试验阶段,该应用程序只能在受支持的浏览器上运行:Chrome(版本 25 以上)和 Edge(版本 79 以上)。 我们需要构建哪些组件? 要构建这个 Web 应用程序,我们需要实现四个组件:一个简单的用户界面,用来显示用户所说的内容和助理的回复。将语音转换为文本。处理文本并执行操作。将文本转换为语音。 用户界面 第一步就是创建一个简单的用户界面,它包含一个按钮用来触发助理,一个用于显示用户命令和助理响应的 div、一个用于显示处理信息的 p 组件。 const startBtn = document.createElement("button"); startBtn.innerHTML = "Start listening"; const result = document.createElement("div"); const processing = document.createElement("p"); document.write("<body><h1>My Siri</h1><p>Give it a try with 'hello', 'how are you', 'what's your name', 'what time is it', 'stop', ... </p></body>"); document.body.append(startBtn); document.body.append(result); document.body.append(processing); 语音转文本 我们需要构建一个组件来捕获语音命令并将其转换为文本,以进行进一步处理。在本教程中,我们使用 Web Speech API 的 SpeechRecognition。由于这个 API 只能在受支持的浏览器中使用,我们将显示警告信息并阻止用户在不受支持的浏览器中看到 Start 按钮。 const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; if (typeof SpeechRecognition === "undefined") { startBtn.remove(); result.innerHTML = "<b>Browser does not support Speech API. Please download latest chrome.<b>"; } 我们需要创建一个 SpeechRecognition 的实例,可以设置一组各种属性来定制语音识别。在这个应用程序中,我们将 continuous 和 interimResults 设置为 true,以便实时显示语音文本。 const recognition = new SpeechRecognition(); recognition.continuous = true; recognition.interimResults = true; 我们添加一个句柄来处理来自语音 API 的 onresult 事件。在这个处理程序中,我们以文本形式显示用户的语音命令,并调用函数 process 来执行操作。这个 process 函数将在下一步实现。 function process(speech_text) { return "...."; } recognition.onresult = event => { const last = event.results.length - 1; const res = event.results[last]; const text = res[0].transcript; if (res.isFinal) { processing.innerHTML = "processing ...."; const response = process(text); const p = document.createElement("p"); p.innerHTML = `You said: ${text} </br>Siri said: ${response}`; processing.innerHTML = ""; result.appendChild(p); // add text to speech later } else { processing.innerHTML = `listening: ${text}`; } } 我们还需要将 用户界面的 button 与 recognition 对象链接起来,以启动 / 停止语音识别。 let listening = false; toggleBtn = () => { if (listening) { recognition.stop(); startBtn.textContent = "Start listening"; } else { recognition.start(); startBtn.textContent = "Stop listening"; } listening = !listening; }; startBtn.addEventListener("click", toggleBtn); 处理文本并执行操作 在这一步中,我们将构建一个简单的会话逻辑并处理一些基本操作。助理可以回复“hello”、“what's your name?”、“how are you?”、提供当前时间的信息、“stop”听取或打开一个新的标签页来搜索它不能回答的问题。你可以通过使用一些 AI 库进一步扩展这个 process 函数,使助理更加智能。 function process(rawText) { // remove space and lowercase text let text = rawText.replace(/\s/g, ""); text = text.toLowerCase(); let response = null; switch(text) { case "hello": response = "hi, how are you doing?"; break; case "what'syourname": response = "My name's Siri."; break; case "howareyou": response = "I'm good."; break; case "whattimeisit": response = new Date().toLocaleTimeString(); break; case "stop": response = "Bye!!"; toggleBtn(); // stop listening } if (!response) { window.open(`http://google.com/search?q=${rawText.replace("search", "")}`, "_blank"); return "I found some information for " + rawText; } return response; } 文本转语音 在最后一步中,我们使用 Web Speech API 的 speechSynthesis 控制器为我们的助理提供语音。这个 API 简单明了。 speechSynthesis.speak(new SpeechSynthesisUtterance(response)); 就是这样!我们只用了 80 行代码就有了一个很酷的助理。程序的演示可以在这里找到。 // UI comp const startBtn = document.createElement("button"); startBtn.innerHTML = "Start listening"; const result = document.createElement("div"); const processing = document.createElement("p"); document.write("<body><h1>My Siri</h1><p>Give it a try with 'hello', 'how are you', 'what's your name', 'what time is it', 'stop', ... </p></body>"); document.body.append(startBtn); document.body.append(result); document.body.append(processing); // speech to text const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; let toggleBtn = null; if (typeof SpeechRecognition === "undefined") { startBtn.remove(); result.innerHTML = "<b>Browser does not support Speech API. Please download latest chrome.<b>"; } else { const recognition = new SpeechRecognition(); recognition.continuous = true; recognition.interimResults = true; recognition.onresult = event => { const last = event.results.length - 1; const res = event.results[last]; const text = res[0].transcript; if (res.isFinal) { processing.innerHTML = "processing ...."; const response = process(text); const p = document.createElement("p"); p.innerHTML = `You said: ${text} </br>Siri said: ${response}`; processing.innerHTML = ""; result.appendChild(p); // text to speech speechSynthesis.speak(new SpeechSynthesisUtterance(response)); } else { processing.innerHTML = `listening: ${text}`; } } let listening = false; toggleBtn = () => { if (listening) { recognition.stop(); startBtn.textContent = "Start listening"; } else { recognition.start(); startBtn.textContent = "Stop listening"; } listening = !listening; }; startBtn.addEventListener("click", toggleBtn); } // processor function process(rawText) { let text = rawText.replace(/\s/g, ""); text = text.toLowerCase(); let response = null; switch(text) { case "hello": response = "hi, how are you doing?"; break; case "what'syourname": response = "My name's Siri."; break; case "howareyou": response = "I'm good."; break; case "whattimeisit": response = new Date().toLocaleTimeString(); break; case "stop": response = "Bye!!"; toggleBtn(); } if (!response) { window.open(`http://google.com/search?q=${rawText.replace("search", "")}`, "_blank"); return `I found some information for ${rawText}`; } return response; } × Drag and Drop The image will be downloaded 作者介绍: Tuan Nhu Dinh,Facebook 软件工程师。 【云栖号在线课堂】每天都有产品技术专家分享!课程地址:https://yqh.aliyun.com/zhibo 立即加入社群,与专家面对面,及时了解课程最新动态!【云栖号在线课堂 社群】https://c.tb.cn/F3.Z8gvnK 原文发布时间:2020-07-17本文作者:Tuan Nhu Dinh本文来自:“InfoQ”,了解相关信息可以关注“InfoQ”

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册