forked from words/syllable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
362 lines (334 loc) · 6.97 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import pluralize from 'pluralize'
// @ts-ignore remove when typed.
import normalize from 'normalize-strings'
import {problematic} from './problematic.js'
var own = {}.hasOwnProperty
// Two expressions of occurrences which normally would be counted as two
// syllables, but should be counted as one.
var EXPRESSION_MONOSYLLABIC_ONE = new RegExp(
[
'awe($|d|so)',
'cia(?:l|$)',
'tia',
'cius',
'cious',
'[^aeiou]giu',
'[aeiouy][^aeiouy]ion',
'iou',
'sia$',
'eous$',
'[oa]gue$',
'.[^aeiuoycgltdb]{2,}ed$',
'.ely$',
'^jua',
'uai',
'eau',
'^busi$',
'(?:[aeiouy](?:' +
[
'[bcfgklmnprsvwxyz]',
'ch',
'dg',
'g[hn]',
'lch',
'l[lv]',
'mm',
'nch',
'n[cgn]',
'r[bcnsv]',
'squ',
's[chkls]',
'th'
].join('|') +
')ed$)',
'(?:[aeiouy](?:' +
[
'[bdfklmnprstvy]',
'ch',
'g[hn]',
'lch',
'l[lv]',
'mm',
'nch',
'nn',
'r[nsv]',
'squ',
's[cklst]',
'th'
].join('|') +
')es$)'
].join('|'),
'g'
)
var EXPRESSION_MONOSYLLABIC_TWO = new RegExp(
'[aeiouy](?:' +
[
'[bcdfgklmnprstvyz]',
'ch',
'dg',
'g[hn]',
'l[lv]',
'mm',
'n[cgns]',
'r[cnsv]',
'squ',
's[cklst]',
'th'
].join('|') +
')e$',
'g'
)
// Four expression of occurrences which normally would be counted as one
// syllable, but should be counted as two.
var EXPRESSION_DOUBLE_SYLLABIC_ONE = new RegExp(
'(?:' +
[
'([^aeiouy])\\1l',
'[^aeiouy]ie(?:r|s?t)',
'[aeiouym]bl',
'eo',
'ism',
'asm',
'thm',
'dnt',
'snt',
'uity',
'dea',
'gean',
'oa',
'ua',
'react?',
'orbed', // Cancel `'.[^aeiuoycgltdb]{2,}ed$',`
'shred', // Cancel `'.[^aeiuoycgltdb]{2,}ed$',`
'eings?',
'[aeiouy]sh?e[rs]'
].join('|') +
')$',
'g'
)
var EXPRESSION_DOUBLE_SYLLABIC_TWO = new RegExp(
[
'creat(?!u)',
'[^gq]ua[^auieo]',
'[aeiou]{3}',
'^(?:ia|mc|coa[dglx].)',
'^re(app|es|im|us)',
'(th|d)eist'
].join('|'),
'g'
)
var EXPRESSION_DOUBLE_SYLLABIC_THREE = new RegExp(
[
'[^aeiou]y[ae]',
'[^l]lien',
'riet',
'dien',
'iu',
'io',
'ii',
'uen',
'[aeilotu]real',
'real[aeilotu]',
'iell',
'eo[^aeiou]',
'[aeiou]y[aeiou]'
].join('|'),
'g'
)
var EXPRESSION_DOUBLE_SYLLABIC_FOUR = /[^s]ia/
// Expression to match single syllable pre- and suffixes.
var EXPRESSION_SINGLE = new RegExp(
[
'^(?:' +
[
'un',
'fore',
'ware',
'none?',
'out',
'post',
'sub',
'pre',
'pro',
'dis',
'side',
'some'
].join('|') +
')',
'(?:' +
[
'ly',
'less',
'some',
'ful',
'ers?',
'ness',
'cians?',
'ments?',
'ettes?',
'villes?',
'ships?',
'sides?',
'ports?',
'shires?',
'[gnst]ion(?:ed|s)?'
].join('|') +
')$'
].join('|'),
'g'
)
// Expression to match double syllable pre- and suffixes.
var EXPRESSION_DOUBLE = new RegExp(
[
'^' +
'(?:' +
[
'above',
'anti',
'ante',
'counter',
'hyper',
'afore',
'agri',
'infra',
'intra',
'inter',
'over',
'semi',
'ultra',
'under',
'extra',
'dia',
'micro',
'mega',
'kilo',
'pico',
'nano',
'macro',
'somer'
].join('|') +
')',
'(?:fully|berry|woman|women|edly|union|((?:[bcdfghjklmnpqrstvwxz])|[aeiou])ye?ing)$'
].join('|'),
'g'
)
// Expression to match triple syllable suffixes.
var EXPRESSION_TRIPLE = /(creations?|ology|ologist|onomy|onomist)$/g
// Wrapper to support multiple word-parts (GH-11).
/**
* Syllable count
*
* @param {string} value
* @returns {number}
*/
export function syllable(value) {
var values = normalize(String(value))
.toLowerCase()
// Remove apostrophes.
.replace(/['’]/g, '')
// Split on word boundaries.
.split(/\b/g)
var index = -1
var sum = 0
while (++index < values.length) {
// Remove non-alphabetic characters from a given value.
sum += one(values[index].replace(/[^a-z]/g, ''))
}
return sum
}
/**
* Get syllables in a given value.
*
* @param {string} value
* @returns {number}
*/
function one(value) {
var count = 0
/** @type {number} */
var index
/** @type {string} */
var singular
/** @type {Array.<string>} */
var parts
/** @type {ReturnType.<returnFactory>} */
var addOne
/** @type {ReturnType.<returnFactory>} */
var subtractOne
if (value.length === 0) {
return count
}
// Return early when possible.
if (value.length < 3) {
return 1
}
// If `value` is a hard to count, it might be in `problematic`.
if (own.call(problematic, value)) {
return problematic[value]
}
// Additionally, the singular word might be in `problematic`.
singular = pluralize(value, 1)
if (own.call(problematic, singular)) {
return problematic[singular]
}
addOne = returnFactory(1)
subtractOne = returnFactory(-1)
// Count some prefixes and suffixes, and remove their matched ranges.
value = value
.replace(EXPRESSION_TRIPLE, countFactory(3))
.replace(EXPRESSION_DOUBLE, countFactory(2))
.replace(EXPRESSION_SINGLE, countFactory(1))
// Count multiple consonants.
parts = value.split(/[^aeiouy]+/)
index = -1
while (++index < parts.length) {
if (parts[index] !== '') {
count++
}
}
// Subtract one for occurrences which should be counted as one (but are
// counted as two).
value
.replace(EXPRESSION_MONOSYLLABIC_ONE, subtractOne)
.replace(EXPRESSION_MONOSYLLABIC_TWO, subtractOne)
// Add one for occurrences which should be counted as two (but are counted as
// one).
value
.replace(EXPRESSION_DOUBLE_SYLLABIC_ONE, addOne)
.replace(EXPRESSION_DOUBLE_SYLLABIC_TWO, addOne)
.replace(EXPRESSION_DOUBLE_SYLLABIC_THREE, addOne)
.replace(EXPRESSION_DOUBLE_SYLLABIC_FOUR, addOne)
// Make sure at least on is returned.
return count || 1
/**
* Define scoped counters, to be used in `String#replace()` calls.
* The scoped counter removes the matched value from the input.
*
* @param {number} addition
*/
function countFactory(addition) {
return counter
/**
* @returns {string}
*/
function counter() {
count += addition
return ''
}
}
/**
* This scoped counter does not remove the matched value from the input.
*
* @param {number} addition
*/
function returnFactory(addition) {
return returner
/**
* @param {string} $0
* @returns {string}
*/
function returner($0) {
count += addition
return $0
}
}
}