forked from LeonieWeissweiler/CISTEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cistem.ts
129 lines (110 loc) · 3.3 KB
/
Cistem.ts
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
const stripge = /^ge(.{4,})/g;
const replxx = /(.)\1/g;
const replxxback = /(.)\*/g;
const stripemr = /e[mr]$/g;
const stripnd = /nd$/g;
const stript = /t$/g;
const stripesn = /[esn]$/g;
export function stem(word: string, caseSensitive: boolean) {
if (word.length === 0) {
return word;
}
const upper = word.slice(0, 1) === word.slice(0, 1).toUpperCase();
word = word.toLowerCase();
word = word.replace(/ü/g, 'u');
word = word.replace(/ö/g, 'o');
word = word.replace(/ä/g, 'a');
word = word.replace(/ß/g, 'ss');
word = word.replace(stripge, '$1');
word = word.replace(/sch/g, '$');
word = word.replace(/ei/g, '%');
word = word.replace(/ie/g, '&');
word = word.replace(replxx, '$1*');
while (word.length > 3) {
if (word.length > 5) {
let newWord = word.replace(stripemr, '');
if (word !== newWord) {
word = newWord;
continue;
}
newWord = word.replace(stripnd, '');
if (word !== newWord) {
word = newWord;
continue;
}
}
if (!upper || caseSensitive) {
let newWord = word.replace(stript, '');
if (word !== newWord) {
word = newWord;
continue;
}
}
let newWord = word.replace(stripesn, '');
if (word !== newWord) {
word = newWord;
} else {
break;
}
}
word = word.replace(replxxback, '$1$1');
word = word.replace(/&/g, 'ie');
word = word.replace(/%/g, 'ei');
word = word.replace(/\$/g, 'sch');
return word;
}
export function segment(word: string, caseSensitive: boolean) {
let restLength = 0;
if (word.length === 0) {
return {word: '', rest: ''};
}
const upper = word.slice(0, 1) === word.slice(0, 1).toUpperCase();
word = word.toLowerCase();
const original = word;
word = word.replace(/sch/g, '$');
word = word.replace(/ei/g, '%');
word = word.replace(/ie/g, '&');
word = word.replace(replxx, '$1*');
while (word.length > 3) {
if (word.length > 5) {
let newWord = word.replace(stripemr, '');
if (word !== newWord) {
restLength += 2;
word = newWord;
continue;
}
newWord = word.replace(stripnd, '');
if (word !== newWord) {
restLength += 2;
word = newWord;
continue;
}
}
if (!upper || caseSensitive) {
let newWord = word.replace(stript, '');
if (word !== newWord) {
restLength += 1;
word = newWord;
continue;
}
}
let newWord = word.replace(stripesn, '');
if (word !== newWord) {
restLength += 1;
word = newWord;
} else {
break;
}
}
word = word.replace(replxxback, '$1$1');
word = word.replace(/&/g, 'ie');
word = word.replace(/%/g, 'ei');
word = word.replace(/\$/g, 'sch');
let rest = '';
if (restLength !== 0) {
rest = original.substr(original.length - restLength);
} else {
rest = '';
}
return {word: word, rest: rest}
}