2017-05-04 29 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() 
    }); 
}