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
|
#!/usr/bin/perl
use strict;
use Getopt::Std;
use locale;
my %opt;
getopts('l:he:s:ap:om:f', \%opt);
if ( $opt{h} || ! ($opt{e}||$opt{s}) || !$opt{l} ) {
print<<EOT;
Generator of variant of the Lovin's stemmer which
uses a longest match algorithm.
Author Teodor Sigaev <teodor\@stack.net>
Usage:
$0 -l LOCALENAME [ -e FILENAME ] [ -s FILENAME ] [ -p PREFIX ] [ -o FILENAME ] [ -a ] [ -m NUMBER ]
-e FILENAME - file with endings of word
-s FILENAME - file with list of stop-word
-o FILENAME - out file, default STDOUT
-a - stop-word are strimmed
-p PREFIX - prefix of function and etc, default strimmed locale
-m NUMBER - minimal length of rest after semming, default 3
-l LOCALENAME - name of locale
-f - do not call tolower for each char
At least one of -e or -s must be defined
EOT
exit;
}
if ( ! defined $opt{p} ) {
$opt{p} = $opt{l};
$opt{p}=~s/[^a-zA-Z0-9_]+//g;
}
$opt{m}=3 if ! defined $opt{m};
my ($enddata,$stopdata) = ('','');
my $maxchild = 0;
if ( $opt{e} ) {
my @tree;
buildtree(\@tree, $opt{e}, 1);
printstruct( \@tree, 0, \$enddata);
undef @tree;
}
if ( $opt{s} ) {
my @tree;
buildtree(\@tree, $opt{s}, 0);
printstruct( \@tree, 0, \$stopdata);
undef @tree;
}
die "No data\n" if ( ! (length $enddata || length $stopdata) );
$enddata = "\t{0,0,0,0}" if ( ! length $enddata );
$stopdata = "\t{0,0,0,0}" if ( ! length $stopdata );
my $fh=\*STDOUT;
if ( $opt{o} ) {
open(OUT,">$opt{o}") || die "Can;t open file '$opt{o}' for writing\n";
$fh = \*OUT;
}
my $linktype = 'uint32';
if ( $maxchild <= 0xff ) {
$linktype='uint8';
} elsif ( $maxchild <= 0xffff ) {
$linktype='uint16';
}
my $wherecheck = ( $opt{a} ) ?
"NULL,\n\t$opt{p}_is_stopword"
:
"$opt{p}_is_stopword,\n\tNULL";
my ($tolower, $resttolower) = ('','');
if ( ! $opt{f} ) {
$tolower = '*cur = tolower( *cur );';
$resttolower=<<EOT;
while( cur - buf >= 0 ) {
*cur = tolower(*cur);
cur--;
}
EOT
}
print {$fh} <<EOT;
/*
* Autogenerated file
*
* Variant of the Lovin's stemmer which uses a longest match algorithm.
* Endings are stored in a suffix tree.
*/
#ifdef DICT_BODY
typedef struct {
uint8 val;
uint8 flag;
uint8 right;
$linktype child;
} $opt{p}_NODE;
/* is exists left tree ? */
#define L 0x01
/* finish word flag */
#define F 0x02
#define ISLEFT(x) ((($opt{p}_NODE*)x)->flag & L)
#define ISFINISH(x) ((($opt{p}_NODE*)x)->flag & F)
#define MINLENREST $opt{m}
static $opt{p}_NODE $opt{p}_endstree[]={
$enddata
};
static $opt{p}_NODE $opt{p}_stoptree[]={
$stopdata
};
static char*
$opt{p}_stem( void* obj, char *in, int *len ) {
$opt{p}_NODE *ptr = $opt{p}_endstree;
int result = 0;
uint8 *buf = (uint8 *)in;
uint8 *cur = buf + (*len) - 1;
while( cur - buf >= MINLENREST ) {
$tolower
if ( ptr->val == *cur ) {
if ( ISFINISH(ptr) ) result = buf + (*len) - cur;
cur--;
if ( ! ptr->child ) break;
ptr += ptr->child;
} else if ( ptr->val > *cur ) {
if ( ISLEFT(ptr) )
ptr++;
else
break;
} else {
if ( ptr->right )
ptr += ptr->right;
else
break;
}
}
$resttolower
*len -= result;
return in;
}
static int
$opt{p}_is_stopword( void *obj, char *in, int len ) {
$opt{p}_NODE *ptr = $opt{p}_stoptree;
int result = 0;
uint8 *buf = (uint8 *)in;
uint8 *cur = buf;
while( cur - buf < len ) {
$tolower
if ( ptr->val == *cur ) {
cur++;
if ( ISFINISH(ptr) ) result = cur - buf;
if ( ! ptr->child ) break;
ptr += ptr->child;
} else if ( ptr->val > *cur ) {
if ( ISLEFT(ptr) )
ptr++;
else
break;
} else {
if ( ptr->right )
ptr += ptr->right;
else
break;
}
}
return (result==len) ? 1 : 0;
}
#undef L
#undef F
#undef ISLEFT
#undef ISFINISH
#undef MINLENREST
#endif /* DICT_BODY */
#ifdef DICT_TABLE
TABLE_DICT_START
\"$opt{l}\",
NULL,
NULL,
$opt{p}_stem,
$wherecheck
TABLE_DICT_END
#endif
EOT
close($fh) if ( $fh != \*STDOUT );
sub buildtree {
my ($reftree,$file, $needreverse) = @_;
open(DATA,$file) || die "Can't open file '$file'\n";
while(<DATA>) {
chomp;
next if ! length $_;
$_ = lc($_) if ! $opt{f};
addtostruct( $reftree, ( $needreverse ) ? scalar(reverse($_)) : $_ );
}
close DATA;
}
sub mkbintree {
my ( $start, $stop, $rprop, $rres) = @_;
my $middle = $start + int( ($stop-$start)/2 );
push( @$rres, $rprop->[$middle] );
my $idx = $#$rres;
$rres->[$idx]{right}=0;
$rres->[$idx]{left}=0;
return 1 if ( $start == $stop );
my $leftsize = 0;
if ( $middle!=$start ) {
$rres->[$idx]{left}=1;
$leftsize = mkbintree( $start, $middle-1, $rprop, $rres );
$rres->[$idx]{right}=$leftsize+1;
} else {
$rres->[$idx]{right} = 1;
}
return 1 + $leftsize + mkbintree( $middle+1, $stop, $rprop, $rres );
}
sub addtostruct {
my $node = shift;
my ($char, $subval) = split('', shift, 2);
$char = ord( $char );
if ( ! defined $node->[$char] ) {
$node->[$char] = {};
$node->[$char]{finish} = length $subval;
$node->[$char]{child} = [];
} elsif ( ! length $subval ) {
$node->[$char]{finish} = 0;
}
addtostruct( $node->[$char]{child}, $subval ) if ( length $subval );
}
sub printstruct {
my ($node, $pre, $refout) = @_;
my $add = 0;
my @prop;
my $outchild;
my $current = 0;
my $poschild=0;
my @tmp;
foreach my $i ( 0..255 ) {
next if ( !defined $node->[ $i ] );
push @prop , { val=>$i,
nchild=>printstruct( $node->[ $i ]{child}, 1, \$outchild ),
poschild=>$poschild };
$poschild += $prop[$#prop]{nchild};
}
return 0 if $#prop < 0;
if ($pre) {
$$refout .= ",\n\n";
}
mkbintree(0,$#prop,\@prop,\@tmp);
@prop = @tmp;
$current=$#prop+1;
foreach my $i ( 0..$#prop ) {
my $flag = ($prop[$i]{left}) ? 'L' : undef;
if ( $node->[ $prop[$i]{val} ]{finish}==0 ) {
$flag .= '|' if defined $flag;
$flag .= 'F';
} elsif ( ! defined $flag ) {
$flag='0';
}
$$refout .= "\t{'".chr( $prop[$i]{val} )."',".
$flag.','.
$prop[$i]{right}.','.
(($prop[$i]{nchild}==0)?0:($prop[$i]{poschild}+$current)).'}'.
(($i==$#prop)? '' : ",\n");
$maxchild = $prop[$i]{poschild}+$current if
( $prop[$i]{nchild} && $prop[$i]{poschild}+$current > $maxchild );
$current--;
$add += $prop[$i]{nchild};
}
$$refout .= $outchild;
return $#prop+1 + $add;
}
|