File size: 1,535 Bytes
db61e17 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
//ver 0.1
async function loadCmudict(obj=cmudict,path='./cmudict-0.7b',splitter=" ") { //split double-space
return new Promise(async (resolve, reject) => {
try {
const response = await fetch(path);
const responseText = await response.text();
const lines = responseText.split('\n');
lines.forEach(line => {
let data = line.trim().split(splitter);
obj[data[0]] = data[1];
});
resolve(true);
} catch (error) {
console.error('Error:', error);
}
});
}
//let cmudictReady =loadCmudict();
function get_arpa(cmudict,word){
return cmudict[word.toUpperCase()]
}
function textToArpa(cmudict,text){
var keep_words = [",",".","!","?"]
let inputText = text.toUpperCase()
keep_words.forEach(function(key){
inputText = inputText.replaceAll(key," "+key+" ");
});
//console.log(`replaced ${inputText}`)
let result = []
let non_converted = []
var words = inputText.split(" ")
words.forEach(word => {
if (keep_words.includes(word)){//,.!? just keep
result.push(word)
}else if (word ==""){
}else{
const arpa = get_arpa(cmudict,word)
if (typeof arpa == "undefined"){
result.push("@"+word)
non_converted.push(word)
}else{
result.push(arpa)
}
}
});
return {"result":result,"non_converted":non_converted}
}
export { textToArpa, loadCmudict}; |