如何添加Artyom.js不完全支持的语言

本文概述

  • 要求
  • 用新语言扩展Artyom
Artyom.js是一个有用的库, 可让你实施由Google Chrome的语音识别和语音合成支持的某种小助手。你可以使用JavaScript语音命令添加纯文本或使用正则表达式。当用户说出的文字与命令中的任何Providen选项匹配时, 将触发命令, 最终触发确定命令功能的函数。
最初直到今天, Artyom.js支持13种语言。该库对这些语言的” 支持” 基于Google的语音识别API在浏览器中针对该语言的语音(本机或来自Google)的可用性。唯一的问题是, Webkit语音识别本身目前在Chrome中支持40多种语言。有些语言根据地区有特定的代码(标识符遵循BCP-47格式)。有关可用的区域和语言代码的更多信息, 请阅读本文。
在本文中, 你将学习如何扩展Artyom对其他语言的支持。
要求你需要2件事来扩展Artyom:
为你的语言安装了语音
第一个是你的浏览器确实具有你所用语言的声音, 例如, 如果你的语言是土耳其语, 则你的浏览器至少需要安装(操作系统上的)本机声音, 并且语言代码需要与所列语言相匹配Webkit语音识别支持的语言。
你可以将以下代码片段与Artyom一起使用, 以字符串形式列出控制台中的所有声音:
let voiceResults = []; let artyom = new Artyom(); // or if you don't want to use artyom//let voices = speechSynthesis.getVoices(); let voices = artyom.getVoices(); voices.forEach((voice) => {voiceResults.push({voiceURI: voice.voiceURI, name: voice.name, lang: voice.lang}); }); console.log(JSON.stringify(voiceResults, null , 5)); // Outputs something like//[//{//"voiceURI": "Microsoft Zira Desktop - English (United States)", //"name": "Microsoft Zira Desktop - English (United States)", //"lang": "en-US"//}, //{//"voiceURI": "Google Deutsch", //"name": "Google Deutsch", //"lang": "de-DE"//}, //{//"voiceURI": "Google US English", //"name": "Google US English", //"lang": "en-US"//}, //etc...

在许多操作系统中, 你都可以安装其他声音, 因此请不要忘记在Google中进行了解。
语音识别API支持你的语言
第二个是Google的语音识别API支持你的语言, 因此你可能希望通过此处的API检查支持的语言列表来进行验证。
如果语音中列出的项目之一具有你的语言的语言代码(Artyom不支持的语言)并且Google语音识别支持你的语言, 那么你可以继续用你的语言扩展Artyom!
用新语言扩展Artyom要将新语言添加到Artyom, 你只需知道要使用的语言代码。例如, 如果我们要添加土耳其语, 我们的代码将为tr-TR。需要在你的Artyom实例的ArtyomVoicesIdentifier对象中将此语言设置为键, 该对象作为值接收一个数组, 该数组包含计算机中安装的语音的一种或多种语言代码:
const myAssistant = new Artyom(); // The code language that will be used to initialize artyom// with Speech Recognitionlet newLanguageIdentifier = "tr-TR"; // An array with the possible code languages of the installed voice// on your device (or voiceURI or name)let possibleVoiceLangIdentifiers = ["tr-TR", "tr_TR"]; // This will add support to Turkish for Voice Commands// and (only if a voice available for turkish in the browser) for speech synthesismyAssistant.ArtyomVoicesIdentifiers[newLanguageIdentifier] = possibleVoiceLangIdentifiers; // Add a command in TurkishmyAssistant.addCommands({indexes: ["günayd?n"], action: () => {myAssistant.say("Günayd?n, bugün nas?ls?n?"); }})// Start Artyom in TurkishmyAssistant.initialize({lang: newLanguageIdentifier, // rest of your initialization code !});

【如何添加Artyom.js不完全支持的语言】使用前面的代码片段, 在操作系统中安装的语音以及Google语音识别API中对语言的支持, 你现在可以轻松地为artyom添加新语言了!

    推荐阅读