-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmu-text-to-phoneme.js
54 lines (46 loc) · 1.13 KB
/
cmu-text-to-phoneme.js
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
var through2 = require('through2');
var settings = {
dictCommentMarker: ';;;',
streamOptions: {
flags: 'r',
encoding: 'utf8'
}
};
var textToPhonemeStream = through2({
objectMode: true
},
function lineToPhoneme(chunk, enc, callback) {
if (chunk.indexOf(settings.dictCommentMarker) !== 0) {
var annotated = annotateLine(chunk);
if (annotated) {
this.push(annotated);
}
}
callback();
}
);
function annotateLine(line) {
var wordAndPhonemes = line.split(' ');
if (wordAndPhonemes.length < 2) {
return;
}
var phonemeStrings = wordAndPhonemes[1].split(' ');
return {
word: wordAndPhonemes[0],
phonemes: phonemeStrings.map(parsePhonemeToken)
};
}
function parsePhonemeToken(token, index) {
var phoneme = token;
var stress = -1;
var lastCharPos = token.length - 1;
if (lastCharPos > 0) {
var lastCharAsNumber = +(token.charAt(lastCharPos));
if (lastCharAsNumber > -1) {
stress = lastCharAsNumber;
phoneme = token.substring(0, lastCharPos);
}
}
return {phoneme: phoneme, stress: stress};
}
module.exports = textToPhonemeStream;