-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.carp
266 lines (242 loc) · 8.79 KB
/
main.carp
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
(relative-include "src/string_extras.h")
(defmodule Stemmer
(hidden utf8-length)
(private utf8-length)
(register utf8-length (Fn [&String] Int) "String_utf8len_")
(hidden to-lower)
(private to-lower)
(register to-lower (Fn [&String] String) "String_to_lower_")
(hidden trim-from)
(private trim-from)
(defn trim-from [x j]
(String.slice x 0 (+ (String.length x) j)))
(hidden replace-from)
(private replace-from)
(defn replace-from [x j r]
(String.append &(trim-from x j) r))
(defn consonant? [x i]
(case (String.char-at x (the Int i))
\a false
\e false
\i false
\o false
\u false
\y (or (= i 0) (not (consonant? x (Int.dec i))))
true))
(hidden m)
(private m)
(defn m [x j]
(let-do [n 0 prev \X]
(for [i 0 (+ (String.length x) j)]
(cond (consonant? x i)
(do
(when (= prev \V) (set! n (Int.inc n)))
(set! prev \C))
(set! prev \V)
)) n))
(hidden m0)
(private m0)
(defn m0 [x n] (> (m x n) 0))
(hidden m1)
(private m1)
(defn m1 [x n] (> (m x n) 1))
(hidden contains-vowel-from?)
(private contains-vowel-from?)
(defn contains-vowel-from? [x j]
(let-do [res false]
(for [i 0 (+ (String.length x) j)]
(when (not (consonant? x i))
(do (set! res true) (break))))
res))
(hidden ends-with-cc?)
(private ends-with-cc?)
(defn ends-with-cc? [x]
(let [j (Int.dec (String.length x))]
(and*
(>= j 1)
(= (String.char-at x j) (String.char-at x (Int.dec j)))
(consonant? x j))))
(hidden cvc?)
(private cvc?)
(defn cvc? [x]
(let [j (Int.dec (String.length x))]
(and*
(>= j 2)
(consonant? x j)
(not (consonant? x (- j 1)))
(consonant? x (- j 2))
(not (= \w (String.char-at x j)))
(not (= \x (String.char-at x j)))
(not (= \y (String.char-at x j))))))
(hidden step-1a)
(private step-1a)
(defn step-1a [x]
(cond
(not (String.ends-with? &x "s")) x
(String.ends-with? &x "sses") (trim-from &x -2)
(String.ends-with? &x "ies") (trim-from &x -2)
(not (String.ends-with? &x "ss")) (trim-from &x -1)
x))
(hidden step-1bi)
(private step-1bi)
(defn step-1bi [x]
(cond
(or*
(String.ends-with? &x "at")
(String.ends-with? &x "bl")
(String.ends-with? &x "iz"))
(append &x "e")
(and*
(ends-with-cc? &x)
(not (String.ends-with? &x "l"))
(not (String.ends-with? &x "s"))
(not (String.ends-with? &x "z")))
(trim-from &x -1)
(and (= (m &x 0) 1) (cvc? &x))
(append &x "e")
x))
(hidden step-1b)
(private step-1b)
(defn step-1b [x]
(cond
(String.ends-with? &x "eed")
(if (m0 &x -3) (trim-from &x -1) x)
(and (String.ends-with? &x "ed") (contains-vowel-from? &x -2))
(step-1bi (trim-from &x -2))
(and (String.ends-with? &x "ing") (contains-vowel-from? &x -3))
(step-1bi (trim-from &x -3))
x))
(hidden step-1c)
(private step-1c)
(defn step-1c [x]
(cond (and (String.ends-with? &x "y") (contains-vowel-from? &x -1))
(replace-from &x -1 "i")
x))
(hidden step-2)
(private step-2)
(defn step-2 [x]
(case (String.char-at &x (- (String.length &x) 2))
\a (cond
(String.ends-with? &x "ational") (if (m0 &x -7) (replace-from &x -7 "ate") x)
(String.ends-with? &x "tional") (if (m0 &x -6) (replace-from &x -6 "tion") x) x)
\c (cond
(String.ends-with? &x "enci") (if (m0 &x -4) (replace-from &x -4 "ence") x)
(String.ends-with? &x "anci") (if (m0 &x -4) (replace-from &x -4 "ance") x) x)
\e (cond
(String.ends-with? &x "izer") (if (m0 &x -4) (replace-from &x -4 "ize") x) x)
\l (cond
(String.ends-with? &x "bli") (if (m0 &x -3) (replace-from &x -3 "ble") x)
(String.ends-with? &x "alli") (if (m0 &x -4) (replace-from &x -4 "al") x)
(String.ends-with? &x "entli") (if (m0 &x -5) (replace-from &x -5 "ent") x)
(String.ends-with? &x "eli") (if (m0 &x -3) (replace-from &x -3 "e") x)
(String.ends-with? &x "ousli") (if (m0 &x -5) (replace-from &x -5 "ous") x) x)
\o (cond
(String.ends-with? &x "ization") (if (m0 &x -7) (replace-from &x -7 "ize") x)
(String.ends-with? &x "ation") (if (m0 &x -5) (replace-from &x -5 "ate") x)
(String.ends-with? &x "ator") (if (m0 &x -4) (replace-from &x -4 "ate") x) x)
\s (cond
(String.ends-with? &x "alism") (if (m0 &x -5) (replace-from &x -5 "ize") x)
(String.ends-with? &x "iveness") (if (m0 &x -7) (replace-from &x -7 "ive") x)
(String.ends-with? &x "fulness") (if (m0 &x -7) (replace-from &x -7 "ful") x)
(String.ends-with? &x "ousness") (if (m0 &x -7) (replace-from &x -7 "ous") x) x)
\t (cond
(String.ends-with? &x "aliti") (if (m0 &x -5) (replace-from &x -5 "al") x)
(String.ends-with? &x "iviti") (if (m0 &x -5) (replace-from &x -5 "ive") x)
(String.ends-with? &x "biliti") (if (m0 &x -6) (replace-from &x -6 "ble") x) x)
\g (cond
(String.ends-with? &x "logi") (if (m0 &x -4) (replace-from &x -4 "log") x) x)
x))
(hidden step-3)
(private step-3)
(defn step-3 [x]
(case (String.char-at &x (- (String.length &x) 1))
\e (cond
(String.ends-with? &x "icate") (if (m0 &x -5) (replace-from &x -5 "ic") x)
(String.ends-with? &x "ative") (if (m0 &x -5) (replace-from &x -5 "") x)
(String.ends-with? &x "alize") (if (m0 &x -5) (replace-from &x -5 "al") x) x)
\i (cond
(String.ends-with? &x "iciti") (if (m0 &x -5) (replace-from &x -5 "ic") x) x)
\l (cond
(String.ends-with? &x "ical") (if (m0 &x -4) (replace-from &x -4 "ic") x)
(String.ends-with? &x "ful") (if (m0 &x -3) (replace-from &x -3 "") x) x)
\s (cond
(String.ends-with? &x "ness") (if (m0 &x -4) (replace-from &x -4 "") x) x)
x))
(hidden step-4)
(private step-4)
(defn step-4 [x]
(case (String.char-at &x (- (String.length &x) 2))
\a (cond
(String.ends-with? &x "al") (if (m1 &x -2) (trim-from &x -2) x) x)
\c (cond
(String.ends-with? &x "ance") (if (m1 &x -4) (trim-from &x -4) x)
(String.ends-with? &x "ence") (if (m1 &x -4) (trim-from &x -4) x) x)
\e (cond
(String.ends-with? &x "er") (if (m1 &x -2) (trim-from &x -2) x) x)
\i (cond
(String.ends-with? &x "ic") (if (m1 &x -2) (trim-from &x -2) x) x)
\l (cond
(String.ends-with? &x "able") (if (m1 &x -4) (trim-from &x -4) x)
(String.ends-with? &x "ible") (if (m1 &x -4) (trim-from &x -4) x) x)
\n (cond
(String.ends-with? &x "ant") (if (m1 &x -3) (trim-from &x -3) x)
(String.ends-with? &x "ement") (if (m1 &x -5) (trim-from &x -5) x)
(String.ends-with? &x "ment") (if (m1 &x -4) (trim-from &x -4) x)
(String.ends-with? &x "ent") (if (m1 &x -3) (trim-from &x -3) x) x)
\o (cond
(String.ends-with? &x "ion") (if
(and (m1 &x -3)
(or
(= \s (String.char-at &x (- (String.length &x) 4)))
(= \t (String.char-at &x (- (String.length &x) 4)))))
(trim-from &x -3) x)
(String.ends-with? &x "ou") (if (m1 &x -2) (trim-from &x -2) x) x)
\s (cond
(String.ends-with? &x "ism") (if (m1 &x -3) (trim-from &x -3) x) x)
\t (cond
(String.ends-with? &x "ate") (if (m1 &x -3) (trim-from &x -3) x)
(String.ends-with? &x "iti") (if (m1 &x -3) (trim-from &x -3) x) x)
\u (cond
(String.ends-with? &x "ous") (if (m1 &x -3) (trim-from &x -3) x) x)
\v (cond
(String.ends-with? &x "ive") (if (m1 &x -3) (trim-from &x -3) x) x)
\z (cond
(String.ends-with? &x "ize") (if (m1 &x -3) (trim-from &x -3) x) x)
x))
(hidden step-5a)
(private step-5a)
(defn step-5a [x]
(cond (String.ends-with? &x "e")
(let [mn (m &x 0)]
(cond
(> mn 1) (trim-from &x -1)
(and (= mn 1) (not (cvc? &(trim-from &x -1)))) (trim-from &x -1)
x))
x))
(hidden step-5b)
(private step-5b)
(defn step-5b [x]
(cond
(and*
(String.ends-with? &x "l")
(> (m &x 0) 1)
(ends-with-cc? &x))
(trim-from &x -1)
x))
(defn stem [x]
(cond (or (< (String.length x) 3) (not (= (String.length x) (utf8-length x))))
@x
(=> x
(to-lower)
(step-1a)
(step-1b)
(step-1c)
(step-2)
(step-3)
(step-4)
(step-5a)
(step-5b)
)))
(defn stem-cstr [x]
(String.cstr &(stem &(String.from-cstr x))))
)