2017-05-04 47 views
2

我试图使西里尔文本到语音节点js模块。PowerShell西里尔输入编码通过节点js

我使用node-powershell来运行.NET TTS命令。它对拉丁符号正常工作,但不会对任何西里尔符号作出反应。

但是,如果我直接输入命令到Powershell控制台 - 它适用于西里尔和拉丁符号。 enter image description here

所以我做了一个讨论,问题点是node.js输出编码。

Node.js的脚本:

var sayWin = (text) => { 
    var Shell = require('node-powershell'); 
    var shell = new Shell({ 
    inputEncoding: 'binary' //tried different endcoding 
    }); 
    shell.addCommand('Add-Type -AssemblyName System.speech'); 
    shell.addCommand('$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer'); 
    shell.addCommand('$speak.Speak("' + text + '")'); 
    shell.on('output', data => { 
    console.log("data", data); 
    }); 
    return shell.invoke(); 
} 

sayWin('latin'); //talk 

sayWin('кирилица'); //silence 

sayWin('\ufeffкирилица'); //silence trying with BOM 

请注意您可能必须安装Windows TTS语音包,并选择它作为默认的系统声音播放西里尔文字(我做了以前)。

回答

0

可能的解决方案之一是将西里尔文文字音译为拉丁语模拟。它有效,但与预期的结果相差甚远(单词发音不尽如人意)。

var transliterate = function(word) { 
    var a = { "Ё": "YO", "Й": "I", "Ц": "TS", "У": "U", "К": "K", "Е": "E", "Н": "N", "Г": "G", "Ш": "SH", "Щ": "SCH", "З": "Z", "Х": "H", "Ъ": "'", "ё": "yo", "й": "i", "ц": "ts", "у": "u", "к": "k", "е": "e", "н": "n", "г": "g", "ш": "sh", "щ": "sch", "з": "z", "х": "h", "ъ": "'", "Ф": "F", "Ы": "I", "В": "V", "А": "a", "П": "P", "Р": "R", "О": "O", "Л": "L", "Д": "D", "Ж": "ZH", "Э": "E", "ф": "f", "ы": "i", "в": "v", "а": "a", "п": "p", "р": "r", "о": "o", "л": "l", "д": "d", "ж": "zh", "э": "e", "Я": "Ya", "Ч": "CH", "С": "S", "М": "M", "И": "yi", "Т": "T", "Ь": "'", "Б": "B", "Ю": "YU", "я": "ya", "ч": "ch", "с": "s", "м": "m", "и": "yi", "т": "t", "ь": "'", "б": "b", "ю": "yu" }; 
    return word.split('').map(function(char) { 
    return a[char] || char; 
    }).join(""); 
} 

var sayWin = (text) => { 

    text = /[а-яА-ЯЁё]/.test(text) ? transliterate(text) : text; 

    var shell = new Shell({ 
    inputEncoding: 'binary' 
    }); 
    shell.addCommand('Add-Type -AssemblyName System.speech'); 
    shell.addCommand('$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer'); 
    shell.addCommand('$speak.Speak("' + text + '")'); 
    shell.on('output', data => { 
    console.log("data", data); 
    }); 
    shell.on('err', err => { 
    console.log("err", err); 
    }); 
    shell.on('end', code => { 
    console.log("code", code); 
    }); 
    return shell.invoke().then(output => { 
    shell.dispose() 
    }); 
}