-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·125 lines (101 loc) · 2.7 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
'use strict';
let helper = require('./helper');
const { prefixes, suffixes, infixes } = require('./affixes');
function removePrefix(word) {
if (helper.countSyllables(word) < 3) {
return word;
}
for (let i = 0; i < prefixes.length; i++) {
if (
helper.hasPrefix(word, prefixes[i]) &&
helper.countSyllables(word) - helper.countSyllables(prefixes[i]) > 1
) {
return word.replace(prefixes[i], '').replace(/^-/, '');
}
}
return word;
}
function prefixSoundChange(word) {
if (word[i] == 'r') {
word[i] = 'd';
return word;
}
}
function removeSuffix(word) {
let removed;
for (let i = 0; i < suffixes.length; i++) {
if (
helper.hasSuffix(word, suffixes[i]) &&
helper.countSyllables(word) - helper.countSyllables(suffixes[i]) > 1
) {
word = word.slice(0, word.length - suffixes[i].length);
removed = suffixes[i];
break;
}
}
if (removed && removed.length == 3 && word[word.length - 1] == 'u') {
word = helper.replaceAt(word, word.length - 1, 'o');
}
if (removed && removed.length == 2 && word[word.length - 2] == 'u') {
word = helper.replaceAt(word, word.length - 2, 'o');
}
if (removed && removed.length == 2 && word[word.length - 1] == 'u') {
word = helper.replaceAt(word, word.length - 1, 'o');
}
return word;
}
function removeInfix(word) {
for (let i = 0; i < infixes.length > 0; i++) {
let infix = infixes[i];
let infixIndex = word.indexOf(infix);
if (infixIndex === 0) {
return word.slice(infixIndex + infix.length);
} else if (infixIndex === 1) {
return word[0] + word.slice(infix.length + 1);
}
}
return word;
}
function removePartialReduplication(word) {
//if two syllablic root
if (word[0] == word[1]) {
return word.slice(1);
} else if (word[0] == word[2] && word[1] == word[3]) {
return word.slice(2);
}
//if three syllabic root
return word;
}
function removeFullReduplication(word) {
let middleIndex = Math.floor(word.length / 2);
if (word.slice(0, middleIndex) == word.slice(middleIndex)) {
return word.slice(middleIndex);
}
return word;
}
function stem(word) {
let pipeline = [
removePrefix,
removeSuffix,
removeInfix,
removePartialReduplication,
removeFullReduplication
];
let history = [word];
for (let i = 0; i < pipeline.length; i++) {
word = pipeline[i](word);
history.push(word);
}
return {
result: word,
history: history
};
}
module.exports = {
stem,
removePrefix,
removeSuffix,
removeInfix,
removePartialReduplication,
removeFullReduplication
};