compromise-speech 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/builds/compromise-speech.cjs +350 -0
- package/builds/compromise-speech.min.js +1 -0
- package/builds/compromise-speech.mjs +1 -0
- package/builds/compromise-stream.cjs +373 -0
- package/builds/compromise-stream.min.js +1 -0
- package/builds/compromise-stream.mjs +1 -0
- package/package.json +38 -0
- package/rollup.config.js +22 -0
- package/scratch.js +17 -0
- package/src/api.js +33 -0
- package/src/compute/index.js +7 -0
- package/src/compute/soundsLike/index.js +11 -0
- package/src/compute/soundsLike/metaphone.js +32 -0
- package/src/compute/soundsLike/transformations.js +83 -0
- package/src/compute/syllables/index.js +13 -0
- package/src/compute/syllables/postProcess.js +107 -0
- package/src/compute/syllables/syllables.js +90 -0
- package/src/plugin.js +7 -0
- package/tests/_lib.js +13 -0
- package/tests/soundsLike.test.js +21 -0
- package/tests/syllables.test.js +73 -0
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://cloud.githubusercontent.com/assets/399657/23590290/ede73772-01aa-11e7-8915-181ef21027bc.png" />
|
|
3
|
+
|
|
4
|
+
<div>pronounciation metadata plugin for <a href="https://github.com/spencermountain/compromise/">compromise</a></div>
|
|
5
|
+
|
|
6
|
+
<!-- npm version -->
|
|
7
|
+
<a href="https://npmjs.org/package/compromise-plugin-speech">
|
|
8
|
+
<img src="https://img.shields.io/npm/v/compromise-plugin-speech.svg?style=flat-square" />
|
|
9
|
+
</a>
|
|
10
|
+
|
|
11
|
+
<!-- file size -->
|
|
12
|
+
<a href="https://unpkg.com/compromise-plugin-speech/builds/compromise-plugin-speech.min.js">
|
|
13
|
+
<img src="https://badge-size.herokuapp.com/spencermountain/compromise/master/plugins/plugin-speech/builds/compromise-plugin-speech.min.js" />
|
|
14
|
+
</a>
|
|
15
|
+
|
|
16
|
+
<div align="center">
|
|
17
|
+
<code>npm install compromise-plugin-speech</code>
|
|
18
|
+
</div>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
<!-- spacer -->
|
|
22
|
+
<img height="30px" src="https://user-images.githubusercontent.com/399657/68221862-17ceb980-ffb8-11e9-87d4-7b30b6488f16.png"/>
|
|
23
|
+
|
|
24
|
+
### Syllables
|
|
25
|
+
Tokenize a word approximately by it's pronounced syllables. We use a custom rule-based alortithm for this.
|
|
26
|
+
```js
|
|
27
|
+
import plg from 'compromise-plugin-speech'
|
|
28
|
+
nlp.extend(plg)
|
|
29
|
+
|
|
30
|
+
let doc = nlp('seventh millenium. white collar')
|
|
31
|
+
doc.syllables()
|
|
32
|
+
[ [ 'se', 'venth', 'mil', 'le', 'nium' ], [ 'white', 'col', 'lar' ] ]
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Alternatively, if you'd like to combine syllable information with other properties, you can use `.compute('syllables')`
|
|
36
|
+
```js
|
|
37
|
+
let doc = nlp('edmonton oilers')
|
|
38
|
+
doc.compute('syllables')
|
|
39
|
+
doc.json()
|
|
40
|
+
/*[{
|
|
41
|
+
"terms": [
|
|
42
|
+
{
|
|
43
|
+
"normal": "edmonton",
|
|
44
|
+
"syllables": ["ed", "mon", "ton"]
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"normal": "oilers",
|
|
48
|
+
"syllables": ["oi", "lers"]
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
}]
|
|
52
|
+
*/
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### SoundsLike
|
|
56
|
+
Generate an expected pronounciation for the word, as a normalized string.
|
|
57
|
+
We use the [metaphone implementation](https://en.wikipedia.org/wiki/Metaphone), which is a simple rule-based pronounciation system.
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
import plg from 'compromise-plugin-speech'
|
|
61
|
+
nlp.extend(plg)
|
|
62
|
+
|
|
63
|
+
let doc = nlp('seventh millenium. white collar')
|
|
64
|
+
doc.soundsLike()
|
|
65
|
+
// [ [ 'sefenth', 'milenium' ], [ 'wite', 'kolar' ] ]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Alternatively, if you'd like to combine syllable information with other properties, you can use `.compute('syllables')`
|
|
69
|
+
```js
|
|
70
|
+
let doc = nlp('edmonton oilers')
|
|
71
|
+
doc.compute('soundsLike')
|
|
72
|
+
doc.json()
|
|
73
|
+
/*[{
|
|
74
|
+
"terms": [
|
|
75
|
+
{
|
|
76
|
+
"normal": "edmonton",
|
|
77
|
+
"soundsLike": "etmonton"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"normal": "oilers",
|
|
81
|
+
"soundsLike": "oilers"
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
}]
|
|
85
|
+
*/
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
MIT
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.compromiseSpeech = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
//individual manipulations of the text
|
|
8
|
+
const transformations = {
|
|
9
|
+
dedup: (s) => {
|
|
10
|
+
return s.replace(/([^c])\1/g, '$1')
|
|
11
|
+
},
|
|
12
|
+
dropInitialLetters: (s) => {
|
|
13
|
+
if (s.match(/^(kn|gn|pn|ae|wr)/)) {
|
|
14
|
+
return s.substr(1, s.length - 1)
|
|
15
|
+
}
|
|
16
|
+
return s
|
|
17
|
+
},
|
|
18
|
+
dropBafterMAtEnd: (s) => {
|
|
19
|
+
return s.replace(/mb$/, 'm')
|
|
20
|
+
},
|
|
21
|
+
cchange: (s) => {
|
|
22
|
+
s = s.replace(/([^s]|^)(c)(h)/g, '$1x$3').trim();
|
|
23
|
+
s = s.replace(/cia/g, 'xia');
|
|
24
|
+
s = s.replace(/c(i|e|y)/g, 's$1');
|
|
25
|
+
return s.replace(/c/g, 'k')
|
|
26
|
+
},
|
|
27
|
+
dchange: (s) => {
|
|
28
|
+
s = s.replace(/d(ge|gy|gi)/g, 'j$1');
|
|
29
|
+
return s.replace(/d/g, 't')
|
|
30
|
+
},
|
|
31
|
+
dropG: (s) => {
|
|
32
|
+
s = s.replace(/gh(^$|[^aeiou])/g, 'h$1');
|
|
33
|
+
return s.replace(/g(n|ned)$/g, '$1')
|
|
34
|
+
},
|
|
35
|
+
changeG: (s) => {
|
|
36
|
+
s = s.replace(/gh/g, 'f');
|
|
37
|
+
s = s.replace(/([^g]|^)(g)(i|e|y)/g, '$1j$3');
|
|
38
|
+
s = s.replace(/gg/g, 'g');
|
|
39
|
+
return s.replace(/g/g, 'k')
|
|
40
|
+
},
|
|
41
|
+
dropH: (s) => {
|
|
42
|
+
return s.replace(/([aeiou])h([^aeiou]|$)/g, '$1$2')
|
|
43
|
+
},
|
|
44
|
+
changeCK: (s) => {
|
|
45
|
+
return s.replace(/ck/g, 'k')
|
|
46
|
+
},
|
|
47
|
+
changePH: (s) => {
|
|
48
|
+
return s.replace(/ph/g, 'f')
|
|
49
|
+
},
|
|
50
|
+
changeQ: (s) => {
|
|
51
|
+
return s.replace(/q/g, 'k')
|
|
52
|
+
},
|
|
53
|
+
changeS: (s) => {
|
|
54
|
+
return s.replace(/s(h|io|ia)/g, 'x$1')
|
|
55
|
+
},
|
|
56
|
+
changeT: (s) => {
|
|
57
|
+
s = s.replace(/t(ia[^n]|io)/g, 'x$1');
|
|
58
|
+
// return s.replace(/th/, '0')
|
|
59
|
+
return s
|
|
60
|
+
},
|
|
61
|
+
dropT: (s) => {
|
|
62
|
+
return s.replace(/tch/g, 'ch')
|
|
63
|
+
},
|
|
64
|
+
changeV: (s) => {
|
|
65
|
+
return s.replace(/v/g, 'f')
|
|
66
|
+
},
|
|
67
|
+
changeWH: (s) => {
|
|
68
|
+
return s.replace(/^wh/, 'w')
|
|
69
|
+
},
|
|
70
|
+
dropW: (s) => {
|
|
71
|
+
return s.replace(/w([^aeiou]|$)/g, '$1')
|
|
72
|
+
},
|
|
73
|
+
changeX: (s) => {
|
|
74
|
+
s = s.replace(/^x/, 's');
|
|
75
|
+
return s.replace(/x/g, 'ks')
|
|
76
|
+
},
|
|
77
|
+
dropY: (s) => {
|
|
78
|
+
return s.replace(/y([^aeiou]|$)/g, '$1')
|
|
79
|
+
},
|
|
80
|
+
changeZ: (s) => {
|
|
81
|
+
return s.replace(/z/, 's')
|
|
82
|
+
},
|
|
83
|
+
dropVowels: (s) => {
|
|
84
|
+
return s //.charAt(0) + s.substr(1, s.length).replace(/[aeiou]/g, '');
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
//a js version of the metaphone (#1) algorithm
|
|
89
|
+
|
|
90
|
+
const metaphone = function (s) {
|
|
91
|
+
s = transformations.dedup(s);
|
|
92
|
+
s = transformations.dropInitialLetters(s);
|
|
93
|
+
s = transformations.dropBafterMAtEnd(s);
|
|
94
|
+
s = transformations.changeCK(s);
|
|
95
|
+
s = transformations.cchange(s);
|
|
96
|
+
s = transformations.dchange(s);
|
|
97
|
+
s = transformations.dropG(s);
|
|
98
|
+
s = transformations.changeG(s);
|
|
99
|
+
s = transformations.dropH(s);
|
|
100
|
+
s = transformations.changePH(s);
|
|
101
|
+
s = transformations.changeQ(s);
|
|
102
|
+
s = transformations.changeS(s);
|
|
103
|
+
s = transformations.changeX(s);
|
|
104
|
+
s = transformations.changeT(s);
|
|
105
|
+
s = transformations.dropT(s);
|
|
106
|
+
s = transformations.changeV(s);
|
|
107
|
+
s = transformations.changeWH(s);
|
|
108
|
+
s = transformations.dropW(s);
|
|
109
|
+
s = transformations.dropY(s);
|
|
110
|
+
s = transformations.changeZ(s);
|
|
111
|
+
s = transformations.dropVowels(s);
|
|
112
|
+
return s.trim()
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const soundsLike = function (view) {
|
|
116
|
+
view.docs.forEach(terms => {
|
|
117
|
+
terms.forEach(term => {
|
|
118
|
+
term.soundsLike = metaphone(term.normal || term.text);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const starts_with_single_vowel_combos = /^(eu)/i;
|
|
124
|
+
const joining_consonant_vowel = /^[^aeiou][e]([^d]|$)/;
|
|
125
|
+
const cvcv_same_consonant = /^([^aeiouy])[aeiouy]\1[aeiouy]/;
|
|
126
|
+
const cvcv_same_vowel = /^[^aeiouy]([aeiouy])[^aeiouy]\1/;
|
|
127
|
+
const cvcv_known_consonants = /^([tg][aeiouy]){2}/;
|
|
128
|
+
const only_one_or_more_c = /^[^aeiouy]+$/;
|
|
129
|
+
|
|
130
|
+
const ends_with_vowel$1 = /[aeiouy]$/;
|
|
131
|
+
const starts_with_consonant_vowel$1 = /^[^aeiouy][h]?[aeiouy]/;
|
|
132
|
+
|
|
133
|
+
const ones = [
|
|
134
|
+
/^[^aeiou]?ion/,
|
|
135
|
+
/^[^aeiou]?ised/,
|
|
136
|
+
/^[^aeiou]?iled/,
|
|
137
|
+
|
|
138
|
+
// -ing, -ent
|
|
139
|
+
/[aeiou][n][gt]$/,
|
|
140
|
+
|
|
141
|
+
// -ate, -age
|
|
142
|
+
/\wa[gt]e$/,
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
//suffix fixes
|
|
146
|
+
const postprocess = function (arr) {
|
|
147
|
+
//trim whitespace
|
|
148
|
+
arr = arr.map(function (w) {
|
|
149
|
+
return w.trim()
|
|
150
|
+
});
|
|
151
|
+
arr = arr.filter(function (w) {
|
|
152
|
+
return w !== ''
|
|
153
|
+
});
|
|
154
|
+
// if (arr.length > 2) {
|
|
155
|
+
// return arr;
|
|
156
|
+
// }
|
|
157
|
+
let l = arr.length;
|
|
158
|
+
if (l > 1) {
|
|
159
|
+
let suffix = arr[l - 2] + arr[l - 1];
|
|
160
|
+
for (let i = 0; i < ones.length; i++) {
|
|
161
|
+
if (suffix.match(ones[i])) {
|
|
162
|
+
arr[l - 2] = arr[l - 2] + arr[l - 1];
|
|
163
|
+
arr.pop();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// since the open syllable detection is overzealous,
|
|
169
|
+
// sometimes need to rejoin incorrect splits
|
|
170
|
+
if (arr.length > 1) {
|
|
171
|
+
let first_is_open =
|
|
172
|
+
(arr[0].length === 1 || arr[0].match(starts_with_consonant_vowel$1)) &&
|
|
173
|
+
arr[0].match(ends_with_vowel$1);
|
|
174
|
+
let second_is_joining = arr[1].match(joining_consonant_vowel);
|
|
175
|
+
|
|
176
|
+
if (first_is_open && second_is_joining) {
|
|
177
|
+
let possible_combination = arr[0] + arr[1];
|
|
178
|
+
let probably_separate_syllables =
|
|
179
|
+
possible_combination.match(cvcv_same_consonant) ||
|
|
180
|
+
possible_combination.match(cvcv_same_vowel) ||
|
|
181
|
+
possible_combination.match(cvcv_known_consonants);
|
|
182
|
+
|
|
183
|
+
if (!probably_separate_syllables) {
|
|
184
|
+
arr[0] = arr[0] + arr[1];
|
|
185
|
+
arr.splice(1, 1);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (arr.length > 1) {
|
|
191
|
+
let second_to_last_is_open =
|
|
192
|
+
arr[arr.length - 2].match(starts_with_consonant_vowel$1) &&
|
|
193
|
+
arr[arr.length - 2].match(ends_with_vowel$1);
|
|
194
|
+
let last_is_joining =
|
|
195
|
+
arr[arr.length - 1].match(joining_consonant_vowel) &&
|
|
196
|
+
ones.every(re => !arr[arr.length - 1].match(re));
|
|
197
|
+
|
|
198
|
+
if (second_to_last_is_open && last_is_joining) {
|
|
199
|
+
let possible_combination = arr[arr.length - 2] + arr[arr.length - 1];
|
|
200
|
+
let probably_separate_syllables =
|
|
201
|
+
possible_combination.match(cvcv_same_consonant) ||
|
|
202
|
+
possible_combination.match(cvcv_same_vowel) ||
|
|
203
|
+
possible_combination.match(cvcv_known_consonants);
|
|
204
|
+
|
|
205
|
+
if (!probably_separate_syllables) {
|
|
206
|
+
arr[arr.length - 2] = arr[arr.length - 2] + arr[arr.length - 1];
|
|
207
|
+
arr.splice(arr.length - 1, 1);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (arr.length > 1) {
|
|
213
|
+
let single = arr[0] + arr[1];
|
|
214
|
+
if (single.match(starts_with_single_vowel_combos)) {
|
|
215
|
+
arr[0] = single;
|
|
216
|
+
arr.splice(1, 1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (arr.length > 1) {
|
|
221
|
+
if (arr[arr.length - 1].match(only_one_or_more_c)) {
|
|
222
|
+
arr[arr.length - 2] = arr[arr.length - 2] + arr[arr.length - 1];
|
|
223
|
+
arr.splice(arr.length - 1, 1);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return arr
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
//chop a string into pronounced syllables
|
|
231
|
+
|
|
232
|
+
const all_spaces = / +/g;
|
|
233
|
+
const ends_with_vowel = /[aeiouy]$/;
|
|
234
|
+
const starts_with_consonant_vowel = /^[^aeiouy][h]?[aeiouy]/;
|
|
235
|
+
const starts_with_e_then_specials = /^e[sm]/;
|
|
236
|
+
const starts_with_e = /^e/;
|
|
237
|
+
const ends_with_noisy_vowel_combos = /(eo|eu|ia|oa|ua|ui)$/i;
|
|
238
|
+
const aiouy = /[aiouy]/;
|
|
239
|
+
const ends_with_ee = /ee$/;
|
|
240
|
+
// const whitespace_dash = /\s\-/
|
|
241
|
+
|
|
242
|
+
//method is nested because it's called recursively
|
|
243
|
+
const doWord = function (w) {
|
|
244
|
+
let all = [];
|
|
245
|
+
let chars = w.split('');
|
|
246
|
+
let before = '';
|
|
247
|
+
let after = '';
|
|
248
|
+
let current = '';
|
|
249
|
+
for (let i = 0; i < chars.length; i++) {
|
|
250
|
+
before = chars.slice(0, i).join('');
|
|
251
|
+
current = chars[i];
|
|
252
|
+
after = chars.slice(i + 1, chars.length).join('');
|
|
253
|
+
let candidate = before + chars[i];
|
|
254
|
+
|
|
255
|
+
//it's a consonant that comes after a vowel
|
|
256
|
+
if (before.match(ends_with_vowel) && !current.match(ends_with_vowel)) {
|
|
257
|
+
if (after.match(starts_with_e_then_specials)) {
|
|
258
|
+
candidate += 'e';
|
|
259
|
+
after = after.replace(starts_with_e, '');
|
|
260
|
+
}
|
|
261
|
+
all.push(candidate);
|
|
262
|
+
return all.concat(doWord(after))
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
//unblended vowels ('noisy' vowel combinations)
|
|
266
|
+
if (candidate.match(ends_with_noisy_vowel_combos)) {
|
|
267
|
+
//'io' is noisy, not in 'ion'
|
|
268
|
+
all.push(before);
|
|
269
|
+
all.push(current);
|
|
270
|
+
return all.concat(doWord(after)) //recursion
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// if candidate is followed by a CV, assume consecutive open syllables
|
|
274
|
+
if (candidate.match(ends_with_vowel) && after.match(starts_with_consonant_vowel)) {
|
|
275
|
+
all.push(candidate);
|
|
276
|
+
return all.concat(doWord(after))
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
//if still running, end last syllable
|
|
280
|
+
if (w.match(aiouy) || w.match(ends_with_ee)) {
|
|
281
|
+
//allow silent trailing e
|
|
282
|
+
all.push(w);
|
|
283
|
+
} else if (w) {
|
|
284
|
+
let last = all.length - 1;
|
|
285
|
+
if (last < 0) {
|
|
286
|
+
last = 0;
|
|
287
|
+
}
|
|
288
|
+
all[last] = (all[last] || '') + w; //append it to the last one
|
|
289
|
+
}
|
|
290
|
+
return all
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
let syllables$1 = function (str) {
|
|
294
|
+
let all = [];
|
|
295
|
+
if (!str) {
|
|
296
|
+
return all
|
|
297
|
+
}
|
|
298
|
+
str = str.replace(/[.,?]/g, '');
|
|
299
|
+
str.split(all_spaces).map(s => {
|
|
300
|
+
all = all.concat(doWord(s));
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// str.split(whitespace_dash).forEach(doWord)
|
|
304
|
+
all = postprocess(all);
|
|
305
|
+
|
|
306
|
+
//for words like 'tree' and 'free'
|
|
307
|
+
if (all.length === 0) {
|
|
308
|
+
all = [str];
|
|
309
|
+
}
|
|
310
|
+
//filter blanks
|
|
311
|
+
all = all.filter(s => s);
|
|
312
|
+
|
|
313
|
+
return all
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const syllables = function (view) {
|
|
317
|
+
view.docs.forEach(terms => {
|
|
318
|
+
terms.forEach(term => {
|
|
319
|
+
term.syllables = syllables$1(term.normal || term.text);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
var compute = {
|
|
325
|
+
soundsLike,
|
|
326
|
+
syllables
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const api = function (View) {
|
|
330
|
+
/** */
|
|
331
|
+
View.prototype.syllables = function () {
|
|
332
|
+
this.compute('syllables');
|
|
333
|
+
let all = [];
|
|
334
|
+
this.docs.forEach(terms => {
|
|
335
|
+
terms.forEach(term => {
|
|
336
|
+
all = all.concat(term.syllables);
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
return all
|
|
340
|
+
};
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
var plugin = {
|
|
344
|
+
api,
|
|
345
|
+
compute
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
return plugin;
|
|
349
|
+
|
|
350
|
+
}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e,t;e=this,t=function(){const e=e=>e.replace(/([^c])\1/g,"$1"),t=e=>e.match(/^(kn|gn|pn|ae|wr)/)?e.substr(1,e.length-1):e,a=e=>e.replace(/mb$/,"m"),l=e=>(e=(e=(e=e.replace(/([^s]|^)(c)(h)/g,"$1x$3").trim()).replace(/cia/g,"xia")).replace(/c(i|e|y)/g,"s$1")).replace(/c/g,"k"),c=e=>(e=e.replace(/d(ge|gy|gi)/g,"j$1")).replace(/d/g,"t"),i=e=>(e=e.replace(/gh(^$|[^aeiou])/g,"h$1")).replace(/g(n|ned)$/g,"$1"),n=e=>(e=(e=(e=e.replace(/gh/g,"f")).replace(/([^g]|^)(g)(i|e|y)/g,"$1j$3")).replace(/gg/g,"g")).replace(/g/g,"k"),o=e=>e.replace(/([aeiou])h([^aeiou]|$)/g,"$1$2"),h=e=>e.replace(/ck/g,"k"),r=e=>e.replace(/ph/g,"f"),g=e=>e.replace(/q/g,"k"),u=e=>e.replace(/s(h|io|ia)/g,"x$1"),p=e=>e=e.replace(/t(ia[^n]|io)/g,"x$1"),s=e=>e.replace(/tch/g,"ch"),f=e=>e.replace(/v/g,"f"),m=e=>e.replace(/^wh/,"w"),y=e=>e.replace(/w([^aeiou]|$)/g,"$1"),$=e=>(e=e.replace(/^x/,"s")).replace(/x/g,"ks"),d=e=>e.replace(/y([^aeiou]|$)/g,"$1"),b=e=>e.replace(/z/,"s"),x=e=>e,k=/^(eu)/i,E=/^[^aeiou][e]([^d]|$)/,j=/^([^aeiouy])[aeiouy]\1[aeiouy]/,w=/^[^aeiouy]([aeiouy])[^aeiouy]\1/,v=/^([tg][aeiouy]){2}/,L=/^[^aeiouy]+$/,T=/[aeiouy]$/,q=/^[^aeiouy][h]?[aeiouy]/,z=[/^[^aeiou]?ion/,/^[^aeiou]?ised/,/^[^aeiou]?iled/,/[aeiou][n][gt]$/,/\wa[gt]e$/],S=/ +/g,A=/[aeiouy]$/,B=/^[^aeiouy][h]?[aeiouy]/,C=/^e[sm]/,D=/^e/,F=/(eo|eu|ia|oa|ua|ui)$/i,G=/[aiouy]/,H=/ee$/,I=function(e){let t=[],a=e.split(""),l="",c="",i="";for(let e=0;e<a.length;e++){l=a.slice(0,e).join(""),i=a[e],c=a.slice(e+1,a.length).join("");let n=l+a[e];if(l.match(A)&&!i.match(A))return c.match(C)&&(n+="e",c=c.replace(D,"")),t.push(n),t.concat(I(c));if(n.match(F))return t.push(l),t.push(i),t.concat(I(c));if(n.match(A)&&c.match(B))return t.push(n),t.concat(I(c))}if(e.match(G)||e.match(H))t.push(e);else if(e){let a=t.length-1;a<0&&(a=0),t[a]=(t[a]||"")+e}return t};let J=function(e){let t=[];return e?((e=e.replace(/[.,?]/g,"")).split(S).map(e=>{t=t.concat(I(e))}),t=function(e){let t=(e=(e=e.map((function(e){return e.trim()}))).filter((function(e){return""!==e}))).length;if(t>1){let a=e[t-2]+e[t-1];for(let l=0;l<z.length;l++)a.match(z[l])&&(e[t-2]=e[t-2]+e[t-1],e.pop())}if(e.length>1){let t=(1===e[0].length||e[0].match(q))&&e[0].match(T),a=e[1].match(E);if(t&&a){let t=e[0]+e[1];t.match(j)||t.match(w)||t.match(v)||(e[0]=e[0]+e[1],e.splice(1,1))}}if(e.length>1){let t=e[e.length-2].match(q)&&e[e.length-2].match(T),a=e[e.length-1].match(E)&&z.every(t=>!e[e.length-1].match(t));if(t&&a){let t=e[e.length-2]+e[e.length-1];t.match(j)||t.match(w)||t.match(v)||(e[e.length-2]=e[e.length-2]+e[e.length-1],e.splice(e.length-1,1))}}if(e.length>1){let t=e[0]+e[1];t.match(k)&&(e[0]=t,e.splice(1,1))}return e.length>1&&e[e.length-1].match(L)&&(e[e.length-2]=e[e.length-2]+e[e.length-1],e.splice(e.length-1,1)),e}(t),0===t.length&&(t=[e]),t=t.filter(e=>e),t):t};return{api:function(e){e.prototype.syllables=function(){this.compute("syllables");let e=[];return this.docs.forEach(t=>{t.forEach(t=>{e=e.concat(t.syllables)})}),e}},compute:{soundsLike:function(k){k.docs.forEach(k=>{k.forEach(k=>{var E;k.soundsLike=(E=k.normal||k.text,E=e(E),E=t(E),E=a(E),E=h(E),E=l(E),E=c(E),E=i(E),E=n(E),E=o(E),E=r(E),E=g(E),E=u(E),E=$(E),E=p(E),E=s(E),E=f(E),E=m(E),E=y(E),E=d(E),E=b(E),(E=x(E)).trim())})})},syllables:function(e){e.docs.forEach(e=>{e.forEach(e=>{e.syllables=J(e.normal||e.text)})})}}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).compromiseSpeech=t();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>e.replace(/([^c])\1/g,"$1"),t=e=>e.match(/^(kn|gn|pn|ae|wr)/)?e.substr(1,e.length-1):e,a=e=>e.replace(/mb$/,"m"),l=e=>(e=(e=(e=e.replace(/([^s]|^)(c)(h)/g,"$1x$3").trim()).replace(/cia/g,"xia")).replace(/c(i|e|y)/g,"s$1")).replace(/c/g,"k"),c=e=>(e=e.replace(/d(ge|gy|gi)/g,"j$1")).replace(/d/g,"t"),i=e=>(e=e.replace(/gh(^$|[^aeiou])/g,"h$1")).replace(/g(n|ned)$/g,"$1"),h=e=>(e=(e=(e=e.replace(/gh/g,"f")).replace(/([^g]|^)(g)(i|e|y)/g,"$1j$3")).replace(/gg/g,"g")).replace(/g/g,"k"),n=e=>e.replace(/([aeiou])h([^aeiou]|$)/g,"$1$2"),r=e=>e.replace(/ck/g,"k"),g=e=>e.replace(/ph/g,"f"),o=e=>e.replace(/q/g,"k"),u=e=>e.replace(/s(h|io|ia)/g,"x$1"),p=e=>e=e.replace(/t(ia[^n]|io)/g,"x$1"),s=e=>e.replace(/tch/g,"ch"),m=e=>e.replace(/v/g,"f"),f=e=>e.replace(/^wh/,"w"),$=e=>e.replace(/w([^aeiou]|$)/g,"$1"),y=e=>(e=e.replace(/^x/,"s")).replace(/x/g,"ks"),d=e=>e.replace(/y([^aeiou]|$)/g,"$1"),k=e=>e.replace(/z/,"s"),x=e=>e,b=/^(eu)/i,E=/^[^aeiou][e]([^d]|$)/,w=/^([^aeiouy])[aeiouy]\1[aeiouy]/,j=/^[^aeiouy]([aeiouy])[^aeiouy]\1/,v=/^([tg][aeiouy]){2}/,L=/^[^aeiouy]+$/,q=/[aeiouy]$/,z=/^[^aeiouy][h]?[aeiouy]/,A=[/^[^aeiou]?ion/,/^[^aeiou]?ised/,/^[^aeiou]?iled/,/[aeiou][n][gt]$/,/\wa[gt]e$/],B=/ +/g,C=/[aeiouy]$/,D=/^[^aeiouy][h]?[aeiouy]/,F=/^e[sm]/,G=/^e/,H=/(eo|eu|ia|oa|ua|ui)$/i,I=/[aiouy]/,J=/ee$/,K=function(e){let t=[],a=e.split(""),l="",c="",i="";for(let e=0;e<a.length;e++){l=a.slice(0,e).join(""),i=a[e],c=a.slice(e+1,a.length).join("");let h=l+a[e];if(l.match(C)&&!i.match(C))return c.match(F)&&(h+="e",c=c.replace(G,"")),t.push(h),t.concat(K(c));if(h.match(H))return t.push(l),t.push(i),t.concat(K(c));if(h.match(C)&&c.match(D))return t.push(h),t.concat(K(c))}if(e.match(I)||e.match(J))t.push(e);else if(e){let a=t.length-1;a<0&&(a=0),t[a]=(t[a]||"")+e}return t};let M=function(e){let t=[];return e?((e=e.replace(/[.,?]/g,"")).split(B).map(e=>{t=t.concat(K(e))}),t=function(e){let t=(e=(e=e.map((function(e){return e.trim()}))).filter((function(e){return""!==e}))).length;if(t>1){let a=e[t-2]+e[t-1];for(let l=0;l<A.length;l++)a.match(A[l])&&(e[t-2]=e[t-2]+e[t-1],e.pop())}if(e.length>1){let t=(1===e[0].length||e[0].match(z))&&e[0].match(q),a=e[1].match(E);if(t&&a){let t=e[0]+e[1];t.match(w)||t.match(j)||t.match(v)||(e[0]=e[0]+e[1],e.splice(1,1))}}if(e.length>1){let t=e[e.length-2].match(z)&&e[e.length-2].match(q),a=e[e.length-1].match(E)&&A.every(t=>!e[e.length-1].match(t));if(t&&a){let t=e[e.length-2]+e[e.length-1];t.match(w)||t.match(j)||t.match(v)||(e[e.length-2]=e[e.length-2]+e[e.length-1],e.splice(e.length-1,1))}}if(e.length>1){let t=e[0]+e[1];t.match(b)&&(e[0]=t,e.splice(1,1))}return e.length>1&&e[e.length-1].match(L)&&(e[e.length-2]=e[e.length-2]+e[e.length-1],e.splice(e.length-1,1)),e}(t),0===t.length&&(t=[e]),t=t.filter(e=>e),t):t};var N={api:function(e){e.prototype.syllables=function(){this.compute("syllables");let e=[];return this.docs.forEach(t=>{t.forEach(t=>{e=e.concat(t.syllables)})}),e}},compute:{soundsLike:function(b){b.docs.forEach(b=>{b.forEach(b=>{var E;b.soundsLike=(E=b.normal||b.text,E=e(E),E=t(E),E=a(E),E=r(E),E=l(E),E=c(E),E=i(E),E=h(E),E=n(E),E=g(E),E=o(E),E=u(E),E=y(E),E=p(E),E=s(E),E=m(E),E=f(E),E=$(E),E=d(E),E=k(E),(E=x(E)).trim())})})},syllables:function(e){e.docs.forEach(e=>{e.forEach(e=>{e.syllables=M(e.normal||e.text)})})}}};export{N as default};
|