-
Notifications
You must be signed in to change notification settings - Fork 7
/
pronounceable.js
160 lines (131 loc) · 3.78 KB
/
pronounceable.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Luke Mitchell, 2016
// https://github.com/lukem512/pronounceable
var fs = require("fs");
var path = require("path");
var threshold = 0.001;
// Load probabilities from JSON files.
var tuples = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "data/tuples.json"), "utf8")
);
var triples = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "data/triples.json"), "utf8")
);
// Remove any non-alphabet characters
// and convert to lower case.
function clean(word) {
return word.replace(/[^a-zA-Z]/g, "").toLowerCase();
}
// Make a percentage.
function percent(score, count) {
return score / count * 100;
}
// Check for undefined probabilities.
function undef(w, i, depth, probs) {
if (depth <= 1) return typeof probs[w[i]] === "undefined";
if (typeof probs[w[i]] === "undefined") return true;
return undef(w, i + 1, depth - 1, probs[w[i]]);
}
// Extract probabilities of word t uple.
function trainTuples(words) {
var probs = {};
var count = 0;
words.forEach(function(w) {
w = clean(w);
for (var i = 0; i < w.length - 1; i++) {
if (!probs[w[i]]) probs[w[i]] = {};
if (!probs[w[i]][w[i + 1]]) probs[w[i]][w[i + 1]] = 1;
else probs[w[i]][w[i + 1]]++;
count++;
}
});
Object.keys(probs).forEach(function(first) {
Object.keys(probs[first]).forEach(function(second) {
probs[first][second] = percent(probs[first][second], count);
});
});
return probs;
}
// Extract probabilities of word triples.
function trainTriples(words) {
var probs = {};
var count = 0;
words.forEach(function(w) {
w = clean(w);
for (var i = 0; i < w.length - 2; i++) {
if (!probs[w[i]]) probs[w[i]] = {};
if (!probs[w[i]][w[i + 1]]) probs[w[i]][w[i + 1]] = {};
if (!probs[w[i]][w[i + 1]][w[i + 2]]) probs[w[i]][w[i + 1]][w[i + 2]] = 1;
else probs[w[i]][w[i + 1]][w[i + 2]]++;
count++;
}
});
Object.keys(probs).forEach(function(first) {
Object.keys(probs[first]).forEach(function(second) {
Object.keys(probs[first][second]).forEach(function(third) {
probs[first][second][third] = percent(
probs[first][second][third],
count
);
});
});
});
return probs;
}
// Extract probabilities of word tuples and triples
// from a large list of words.
module.exports.train = function(filename, callback) {
fs.readFile(filename, "utf8", function read(err, data) {
if (err) throw err;
var words = data.trim().split(/\s+/);
var tuples = trainTuples(words);
var triples = trainTriples(words);
callback(tuples, triples);
});
};
// Check whether a word is pronounceable using
// the word tuple probabilities.
module.exports.test = function(word) {
var w = clean(word);
switch (w.length) {
case 1:
break;
case 2:
for (var i = 0; i < w.length - 1; i++) {
if (undef(w, i, 2, tuples)) return false;
if (tuples[w[i]][w[i + 1]] < threshold) return false;
}
default:
for (var i = 0; i < w.length - 2; i++) {
if (undef(w, i, 3, triples)) return false;
if (triples[w[i]][w[i + 1]][w[i + 2]] < threshold) return false;
}
}
return true;
};
// Compute a normalised score for
// the pronounceability of the word.
module.exports.score = function(word) {
var w = clean(word);
var score = 0;
switch (w.length) {
case 1:
return 1;
case 2:
for (var i = 0; i < w.length - 1; i++) {
if (undef(w, i, 2, tuples)) {
score = score + 0;
} else {
score = score + tuples[w[i]][w[i + 1]];
}
}
default:
for (var i = 0; i < w.length - 2; i++) {
if (undef(w, i, 3, triples)) {
score = score + 0;
} else {
score = score + triples[w[i]][w[i + 1]][w[i + 2]];
}
}
}
return score / w.length;
};