-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
174 lines (154 loc) · 4.11 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
// Standard suffix manipulations.
/** @type {Record<string, string>} */
const step2list = {
ational: 'ate',
tional: 'tion',
enci: 'ence',
anci: 'ance',
izer: 'ize',
bli: 'ble',
alli: 'al',
entli: 'ent',
eli: 'e',
ousli: 'ous',
ization: 'ize',
ation: 'ate',
ator: 'ate',
alism: 'al',
iveness: 'ive',
fulness: 'ful',
ousness: 'ous',
aliti: 'al',
iviti: 'ive',
biliti: 'ble',
logi: 'log'
}
/** @type {Record<string, string>} */
const step3list = {
icate: 'ic',
ative: '',
alize: 'al',
iciti: 'ic',
ical: 'ic',
ful: '',
ness: ''
}
// Consonant-vowel sequences.
const consonant = '[^aeiou]'
const vowel = '[aeiouy]'
const consonants = '(' + consonant + '[^aeiouy]*)'
const vowels = '(' + vowel + '[aeiou]*)'
const gt0 = new RegExp('^' + consonants + '?' + vowels + consonants)
const eq1 = new RegExp(
'^' + consonants + '?' + vowels + consonants + vowels + '?$'
)
const gt1 = new RegExp('^' + consonants + '?(' + vowels + consonants + '){2,}')
const vowelInStem = new RegExp('^' + consonants + '?' + vowel)
const consonantLike = new RegExp('^' + consonants + vowel + '[^aeiouwxy]$')
// Exception expressions.
const sfxLl = /ll$/
const sfxE = /^(.+?)e$/
const sfxY = /^(.+?)y$/
const sfxIon = /^(.+?(s|t))(ion)$/
const sfxEdOrIng = /^(.+?)(ed|ing)$/
const sfxAtOrBlOrIz = /(at|bl|iz)$/
const sfxEED = /^(.+?)eed$/
const sfxS = /^.+?[^s]s$/
const sfxSsesOrIes = /^.+?(ss|i)es$/
const sfxMultiConsonantLike = /([^aeiouylsz])\1$/
const step2 =
/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/
const step3 = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/
const step4 =
/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/
/**
* Get the stem from a given value.
*
* @param {string} value
* Value to stem.
* @returns {string}
* Stem for `value`
*/
// eslint-disable-next-line complexity
export function stemmer(value) {
let result = String(value).toLowerCase()
// Exit early.
if (result.length < 3) {
return result
}
/** @type {boolean} */
let firstCharacterWasLowerCaseY = false
// Detect initial `y`, make sure it never matches.
if (
result.codePointAt(0) === 121 // Lowercase Y
) {
firstCharacterWasLowerCaseY = true
result = 'Y' + result.slice(1)
}
// Step 1a.
if (sfxSsesOrIes.test(result)) {
// Remove last two characters.
result = result.slice(0, -2)
} else if (sfxS.test(result)) {
// Remove last character.
result = result.slice(0, -1)
}
/** @type {RegExpMatchArray|null} */
let match
// Step 1b.
if ((match = sfxEED.exec(result))) {
if (gt0.test(match[1])) {
// Remove last character.
result = result.slice(0, -1)
}
} else if ((match = sfxEdOrIng.exec(result)) && vowelInStem.test(match[1])) {
result = match[1]
if (sfxAtOrBlOrIz.test(result)) {
// Append `e`.
result += 'e'
} else if (sfxMultiConsonantLike.test(result)) {
// Remove last character.
result = result.slice(0, -1)
} else if (consonantLike.test(result)) {
// Append `e`.
result += 'e'
}
}
// Step 1c.
if ((match = sfxY.exec(result)) && vowelInStem.test(match[1])) {
// Remove suffixing `y` and append `i`.
result = match[1] + 'i'
}
// Step 2.
if ((match = step2.exec(result)) && gt0.test(match[1])) {
result = match[1] + step2list[match[2]]
}
// Step 3.
if ((match = step3.exec(result)) && gt0.test(match[1])) {
result = match[1] + step3list[match[2]]
}
// Step 4.
if ((match = step4.exec(result))) {
if (gt1.test(match[1])) {
result = match[1]
}
} else if ((match = sfxIon.exec(result)) && gt1.test(match[1])) {
result = match[1]
}
// Step 5.
if (
(match = sfxE.exec(result)) &&
(gt1.test(match[1]) ||
(eq1.test(match[1]) && !consonantLike.test(match[1])))
) {
result = match[1]
}
if (sfxLl.test(result) && gt1.test(result)) {
result = result.slice(0, -1)
}
// Turn initial `Y` back to `y`.
if (firstCharacterWasLowerCaseY) {
result = 'y' + result.slice(1)
}
return result
}