-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphinxstemar.cpp
521 lines (450 loc) · 12.9 KB
/
sphinxstemar.cpp
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//
// $Id$
//
//
// Copyright (c) 2010, Ammar Ali
// Copyright (c) 2012, Sphinx Technologies Inc
//
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
//
// The extended ISRI Arabic stemmer
//
// Original algorithm as described by Kazem Taghva, Rania Elkoury, and
// Jeffery Coombs in "Arabic Stemming Without A Root Dictionary", 2005.
// DOI 10.1109/ITCC.2005.90
//
// Extensions (by Ammar Ali) include:
// - added stripping of kashida (tatweel) characters;
// - added support for matching recurring stem characters;
// - added "ef3ou3ala" verb form to the known patterns.
//
#include "sphinx.h"
/// characters used in affix and word form patterns
/// FIXME! not right on big-endian
#define AR_ALEF_HA 0xA3D8U // U+0623
#define AR_ALEF 0xA7D8U // U+0627
#define AR_BA 0xA8D8U // U+0628
#define AR_TA_M 0xA9D8U // U+0629
#define AR_TA 0xAAD8U // U+062A
#define AR_SEEN 0xB3D8U // U+0633
#define AR_TATWEEL 0x80D9U // U+0640
#define AR_FA 0x81D9U // U+0641
#define AR_KAF 0x83D9U // U+0643
#define AR_LAM 0x84D9U // U+0644
#define AR_MIM 0x85D9U // U+0645
#define AR_NOON 0x86D9U // U+0646
#define AR_HA 0x87D9U // U+0647
#define AR_WAW 0x88D9U // U+0648
#define AR_YA 0x8AD9U // U+064A
/// extension; used for recurring root character matching
#define MATCH_M 0xB0DBU // 0x06F0
#define MATCH_0 MATCH_M
#define MATCH_1 0xB1DBU // 0x06F1
#define MATCH_2 0xB2DBU // 0x06F2
#define MATCH_3 0xB3DBU // 0x06F3
#define MATCH_4 0xB4DBU // 0x06F4
//////////////////////////////////////////////////////////////////////////
// CHARACTER SET TESTS
//////////////////////////////////////////////////////////////////////////
typedef WORD ar_char;
// AR_HAMZA_SET (U+0621, U+0624, U+0626)
#define AR_HAMZA_SET ( c==0xA1D8U || c==0xA4D8U || c==0xA6D8U )
// ALEF_SET (U+0623, U+0625, U+0671)
#define AR_ALEF_SET ( c==0xA3D8U || c==0xA5D8U || c==0xB1D9U )
// AR_DIACRITIC (>= U+064B &&<=U+0652)
#define AR_DIACRITIC(c) \
( c==0x8BD9U || c==0x8CD9U || c==0x8DD9U \
|| c==0x8ED9U || c==0x8FD9U || c==0x90D9U \
|| c==0x91D9U || c==0x92D9U )
// AR_KASHIDA (U+0640)
#define AR_KASHIDA(c) ( c==0x80D9U )
// OPTIMIZE?
#define AR_WORD_LENGTH ((int)(strlen((char*)word) / sizeof(ar_char)))
// FIXME? can crash on misaligned reads
#define AR_CHAR_AT(i) (*((ar_char*)(&word[(i * sizeof(ar_char))])))
#define AR_CHAR_SET(i,c) \
{ \
ar_char *p = (ar_char*) &word[(i * sizeof(ar_char))]; \
*p = (ar_char) c; \
}
/// remove length chars starting from start, null-terminating to new length
static inline void ar_remove ( BYTE * word, int start, int length )
{
int remain = ((AR_WORD_LENGTH - length - start) * sizeof(ar_char));
ar_char *s = (ar_char*) &word[(start * sizeof(ar_char))];
if ( remain > 0 ) {
memmove((void*)s, (void*)(s + length), remain);
s = s + (remain / sizeof(ar_char));
}
*s = '\0';
}
/// normalize (replace) all occurrences of chars in set to to_char
#define AR_NORMALIZE(set, to_char) \
{ \
int wlen = AR_WORD_LENGTH; \
while ( wlen > 0 ) { \
int ni = wlen - 1; \
ar_char c = AR_CHAR_AT(ni); \
if ( set ) { \
AR_CHAR_SET ( ni, to_char ); \
} wlen--; \
} \
}
/// remove all occurances of chars that match the given macro (e.g. KASHIDA)
#define AR_STRIP(_what) \
{ \
int wlen = AR_WORD_LENGTH; \
while ( wlen > 0 ) { \
int si = wlen - 1; \
if ( _what ( AR_CHAR_AT(si) ) ) \
ar_remove ( word, si, 1 ); \
wlen--; \
} \
}
/// attempt to match and remove a prefix with the given character count
#define AR_PREFIX(count) \
{ \
int match = ar_match_affix ( word, prefix_##count, count, 0 ); \
if ( match>=0 ) \
ar_remove ( word, 0, count ); \
}
/// attempt to match and remove a suffix with the given character count
#define AR_SUFFIX(count) \
{ \
int match = ar_match_affix ( word, suffix_##count, count, 1 ); \
if ( match>=0 ) \
ar_remove ( word, AR_WORD_LENGTH - count, count ); \
}
//////////////////////////////////////////////////////////////////////////
// TYPES
//////////////////////////////////////////////////////////////////////////
struct ar_affix_t
{
ar_char chars[4];
};
struct ar_form_entry_t
{
int at; // index to match at
ar_char cp; // code point to match
};
struct ar_form_t
{
ar_form_entry_t entry[4];
};
//////////////////////////////////////////////////////////////////////////
// PREFIX LOOKUP TABLES
//////////////////////////////////////////////////////////////////////////
/// 3-letter prefixes
static struct ar_affix_t prefix_3[] =
{
{ { AR_WAW, AR_LAM, AR_LAM, 0 } },
{ { AR_WAW, AR_ALEF, AR_LAM, 0 } },
{ { AR_KAF, AR_ALEF, AR_LAM, 0 } },
{ { AR_BA, AR_ALEF, AR_LAM, 0 } },
// Extensions
{ { AR_ALEF, AR_SEEN, AR_TA, 0 }},
{ { AR_WAW, AR_BA, AR_MIM, 0 }},
{ { AR_WAW, AR_BA, AR_ALEF, 0 }},
{ { 0 } }
};
/// 2-letter prefixes
static struct ar_affix_t prefix_2[] =
{
{ { AR_ALEF, AR_LAM, 0 } },
{ { AR_LAM, AR_LAM, 0 } },
{ { 0 } }
};
/// 1-letter prefixes
static struct ar_affix_t prefix_1[] =
{
{ { AR_ALEF, 0 } },
{ { AR_BA, 0 } },
{ { AR_TA, 0 } },
{ { AR_SEEN, 0 } },
{ { AR_FA, 0 } },
{ { AR_LAM, 0 } },
{ { AR_NOON, 0 } },
{ { AR_WAW, 0 } },
{ { AR_YA, 0 } },
{ { 0 } }
};
//////////////////////////////////////////////////////////////////////////
// SUFFIX LOOKUP TABLES
//////////////////////////////////////////////////////////////////////////
/// 3-letter suffixes
static struct ar_affix_t suffix_3[] =
{
{ { AR_TA, AR_MIM, AR_LAM, 0 } },
{ { AR_HA, AR_MIM, AR_LAM, 0 } },
{ { AR_TA, AR_ALEF, AR_NOON, 0 } },
{ { AR_TA, AR_YA, AR_NOON, 0 } },
{ { AR_KAF, AR_MIM, AR_LAM, 0 } },
{ { 0 } }
};
/// 2-letter suffixes
static struct ar_affix_t suffix_2[] = {
{ { AR_WAW, AR_NOON, 0 } },
{ { AR_ALEF, AR_TA, 0 } },
{ { AR_ALEF, AR_NOON, 0 } },
{ { AR_YA, AR_NOON, 0 } },
{ { AR_TA, AR_NOON, 0 } },
{ { AR_KAF, AR_MIM, 0 } },
{ { AR_HA, AR_NOON, 0 } },
{ { AR_NOON, AR_ALEF, 0 } },
{ { AR_YA, AR_ALEF, 0 } },
{ { AR_HA, AR_ALEF, 0 } },
{ { AR_TA, AR_MIM, 0 } },
{ { AR_KAF, AR_NOON, 0 } },
{ { AR_NOON, AR_YA, 0 } },
{ { AR_WAW, AR_ALEF, 0 } },
{ { AR_MIM, AR_ALEF, 0 } },
{ { AR_HA, AR_MIM, 0 } },
// extensions
{ { AR_WAW, AR_HA, 0 }},
{ { 0 } }
};
/// 1-letter prefixes
static struct ar_affix_t suffix_1[] =
{
{ { AR_ALEF, 0 } },
{ { AR_TA_M, 0 } },
{ { AR_TA, 0 } },
{ { AR_KAF, 0 } },
{ { AR_NOON, 0 } },
{ { AR_HA, 0 } },
{ { AR_YA, 0 } },
{ { 0 } }
};
//////////////////////////////////////////////////////////////////////////
// FORMS
//////////////////////////////////////////////////////////////////////////
/// forms for 4 letter words that yield a 3 letter stem
static struct ar_form_t form_4_3[] =
{
{ { { 3, AR_TA_M }, { 0xFF, 0 } } },
{ { { 1, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 0xFF, 0 } } }, // originally at index 05
{ { { 2, AR_WAW }, { 0xFF, 0 } } },
{ { { 2, AR_ALEF }, { 0xFF, 0 } } },
{ { { 2, AR_YA }, { 0xFF, 0 } } },
{ { { 0xFF, 0 } } }
};
/// forms for 5 letter words that yield a 3 letter stem
static struct ar_form_t form_5_3[] =
{
{ { { 0, AR_TA }, { 2, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_ALEF }, { 2, AR_TA }, { 0xFF, 0 } } },
{ { { 0, AR_ALEF }, { 3, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_ALEF }, { 2, AR_ALEF }, { 0xFF, 0 } } },
{ { { 2, AR_ALEF }, { 4, AR_TA_M }, { 0xFF, 0 } } },
{ { { 3, AR_ALEF }, { 4, AR_NOON }, { 0xFF, 0 } } },
{ { { 2, AR_WAW }, { 4, AR_TA_M }, { 0xFF, 0 } } },
{ { { 0, AR_TA }, { 4, AR_TA_M }, { 0xFF, 0 } } },
{ { { 0, AR_TA }, { 3, AR_YA }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 4, AR_TA_M }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 2, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 3, AR_WAW }, { 0xFF, 0 } } },
{ { { 1, AR_ALEF }, { 3, AR_WAW }, { 0xFF, 0 } } },
{ { { 1, AR_WAW }, { 2, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 3, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 3, AR_YA }, { 0xFF, 0 } } },
{ { { 0, AR_ALEF }, { 4, AR_TA_M }, { 0xFF, 0 } } },
{ { { 2, AR_ALEF }, { 3, AR_NOON }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 1, AR_NOON }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 2, AR_TA }, { 0xFF, 0 } } },
{ { { 1, AR_ALEF }, { 4, AR_TA_M }, { 0xFF, 0 } } },
{ { { 0, AR_YA }, { 2, AR_TA }, { 0xFF, 0 } } },
{ { { 0, AR_TA }, { 2, AR_TA }, { 0xFF, 0 } } },
{ { { 2, AR_ALEF }, { 4, AR_YA }, { 0xFF, 0 } } },
{ { { 0, AR_ALEF }, { 1, AR_NOON }, { 0xFF, 0 } } },
// extensions
{ { { 1, AR_TA }, { 4, AR_WAW }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 1, AR_TA }, { 0xFF, 0 } } },
{ { { 0, AR_TA }, { 4, AR_TA }, { 0xFF, 0 } } },
{ { { 1, AR_ALEF }, { 3, AR_YA }, { 0xFF, 0 } } },
{ { { 0xFF, 0 } } }
};
/// forms for 5 letter words that yield a 4 letter stem
static struct ar_form_t form_5_4[] =
{
{ { { 0, AR_TA }, { 0xFF, 0 } } },
{ { { 0, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 0xFF, 0 } } },
{ { { 4, AR_TA_M }, { 0xFF, 0 } } },
{ { { 2, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0xFF, 0} }}
};
/// forms for 6 letter words that yield a 3 letter stem
static struct ar_form_t form_6_3[] =
{
{ { { 0, AR_ALEF}, { 1, AR_SEEN }, { 2, AR_TA }, { 0xFF, 0 } }},
{ { { 0, AR_MIM}, { 3, AR_ALEF }, { 5, AR_TA_M }, { 0xFF, 0 } }},
{ { { 0, AR_ALEF}, { 2, AR_TA }, { 4, AR_ALEF }, { 0xFF, 0 } }},
// extensions that match recurring 2nd root letter
{ { { 0, AR_ALEF }, { 3, AR_WAW }, { 4, MATCH_2 }, { 0xFF, 0 } }},
{ { { 0, AR_MIM }, { 1, AR_SEEN }, { 2, AR_TA }, { 0xFF, 0 } }},
// extensions
{ { { 0, AR_MIM }, { 2, AR_ALEF}, { 4, AR_YA }, { 0xFF, 0 } }},
{ { { 0xFF, 0 } }}
};
/// forms for 6 letter words that yield a 4 letter stem
static struct ar_form_t form_6_4[] =
{
{ { { 0, AR_ALEF }, { 4, AR_ALEF }, { 0xFF, 0 } } },
{ { { 0, AR_MIM }, { 1, AR_TA }, { 0xFF, 0 } } },
{ { { 0xFF, 0} }}
};
/// attempt to match the given word against one of the given affix rules
static int ar_match_affix ( BYTE * word, struct ar_affix_t * affixes, int length, int reverse )
{
int match = -1, ai = 0;
while ( affixes[ai].chars[0] && match<0 )
{
int ci = 0;
while ( affixes[ai].chars[ci] && match<0 )
{
int wi = ci;
if ( reverse )
wi = (AR_WORD_LENGTH - length) + ci;
if ( AR_CHAR_AT(wi)!=affixes[ai].chars[ci] )
break;
ci++;
if ( affixes[ai].chars[ci]==0 )
match = ai;
}
ai++;
}
return match;
}
/// attempt to match the given word against one of the given form
/// rules, and if found, extract the stem
static int ar_match_form ( BYTE * word, struct ar_form_t * forms )
{
int match = -1, fi = 0;
while ( forms[fi].entry[0].at!=0xFF && match < 0 )
{
int pi = 0;
while ( forms[fi].entry[pi].at!=0xFF && match<0 )
{
if ( forms[fi].entry[pi].cp>=MATCH_M && forms[fi].entry[pi].cp<=MATCH_4 )
{
int index = ( forms[fi].entry[pi].cp - MATCH_M ) >> 8;
if ( AR_CHAR_AT(index)!=AR_CHAR_AT(forms[fi].entry[pi].at) )
break;
} else
{
if ( forms[fi].entry[pi].cp!=AR_CHAR_AT ( forms[fi].entry[pi].at ) )
break;
}
pi++;
if ( forms[fi].entry[pi].at==0xFF )
match = fi;
}
fi++;
}
// if match found, extract the stem
if ( match>=0 )
{
int pi = 0;
while ( forms[match].entry[pi].at!=0xFF )
{
ar_remove ( word, (forms[match].entry[pi].at - pi), 1 );
pi++;
}
}
return match;
}
static void ar_word_4 ( BYTE * word )
{
if ( ar_match_form ( word, form_4_3 )>=0 )
return;
AR_SUFFIX(1);
if ( AR_WORD_LENGTH==4 )
AR_PREFIX(1);
}
static void ar_word_5 ( BYTE * word )
{
if ( ar_match_form ( word, form_5_3 )>=0 )
return;
AR_SUFFIX(1);
if ( AR_WORD_LENGTH==4 )
{
ar_word_4(word);
return;
}
AR_PREFIX(1);
if ( AR_WORD_LENGTH==4 )
ar_word_4(word);
else if ( AR_WORD_LENGTH==5 )
ar_match_form ( word, form_5_4 );
}
static void ar_word_6 ( BYTE * word )
{
if ( ar_match_form ( word, form_6_3 )>=0 )
return;
AR_SUFFIX(1);
if ( AR_WORD_LENGTH==5 )
{
ar_word_5(word);
return;
}
AR_PREFIX(1);
if ( AR_WORD_LENGTH==5 )
ar_word_5(word);
else if ( AR_WORD_LENGTH==6 )
ar_match_form ( word, form_6_4 );
}
void stem_ar_utf8 ( BYTE * word )
{
AR_STRIP ( AR_DIACRITIC );
AR_STRIP ( AR_KASHIDA ); // extension
AR_NORMALIZE ( AR_HAMZA_SET, AR_ALEF_HA );
#ifdef AR_STEM_AGGRESSIVE
// extension; does both if possible (i.e. 6 and 5, not 6 else 5)
if ( AR_WORD_LENGTH>=6 )
AR_PREFIX(3);
if ( AR_WORD_LENGTH>=5 )
AR_PREFIX(2);
if ( AR_WORD_LENGTH>=6 )
AR_SUFFIX(3);
if ( AR_WORD_LENGTH>=5 )
AR_SUFFIX(2);
#else
// original; does one only (i.e. 6 or 5, not 6 and 5)
if ( AR_WORD_LENGTH>=6 )
AR_PREFIX(3)
else if ( AR_WORD_LENGTH>=5 )
AR_PREFIX(2);
if ( AR_WORD_LENGTH>=6 )
AR_SUFFIX(3)
else if ( AR_WORD_LENGTH>=5 )
AR_SUFFIX(2);
#endif
AR_NORMALIZE ( AR_ALEF_SET, AR_ALEF );
switch ( AR_WORD_LENGTH )
{
case 4: ar_word_4 ( word ); return;
case 5: ar_word_5 ( word ); return;
case 6: ar_word_6 ( word ); return;
case 7:
AR_SUFFIX(1);
if ( AR_WORD_LENGTH==6 )
{
ar_word_6(word);
return;
}
AR_PREFIX(1);
if ( AR_WORD_LENGTH==6 )
ar_word_6(word);
return;
}
}
//
// $Id$
//