1 |
/* -*- Mode: C -*- |
2 |
* $Basename: WAIT.xs $ |
3 |
* $Revision: 1.3 $ |
4 |
* Author : Ulrich Pfeifer |
5 |
* Created On : Thu Aug 15 18:01:00 1996 |
6 |
* Last Modified By: Ulrich Pfeifer |
7 |
* Last Modified On: Wed Nov 5 17:01:30 1997 |
8 |
* Language : C |
9 |
* Update Count : 106 |
10 |
* Status : Unknown, Use with caution! |
11 |
* |
12 |
* (C) Copyright 1997, Ulrich Pfeifer, all rights reserved. |
13 |
* |
14 |
*/ |
15 |
|
16 |
#ifdef __cplusplus |
17 |
extern "C" { |
18 |
#endif |
19 |
#include "EXTERN.h" |
20 |
#include "perl.h" |
21 |
#include "XSUB.h" |
22 |
#ifdef __cplusplus |
23 |
} |
24 |
#endif |
25 |
|
26 |
#include "soundex.h" |
27 |
#include "stemmer.h" |
28 |
#include "metaphone.h" |
29 |
#include "levenstein.h" |
30 |
|
31 |
static unsigned char *lchars = |
32 |
"abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïñòóôõöøùúûüýß"; |
33 |
static unsigned char *uchars = |
34 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝß"; |
35 |
static char *scodes = |
36 |
"01230120022455012623010202000000 00000000500000 000002"; |
37 |
static char *pcodes = |
38 |
"01230720022455012683070808000000 00000000500000 000008"; |
39 |
static unsigned char tou[256]; |
40 |
static unsigned char tol[256]; |
41 |
static unsigned char tos[256]; |
42 |
static char scd[256]; |
43 |
static char pcd[256]; |
44 |
static unsigned char *nums = "0123456789"; |
45 |
|
46 |
void init_lcuc () |
47 |
{ |
48 |
short i; |
49 |
short l = strlen(lchars); |
50 |
|
51 |
for(i=0;i<256;i++) { |
52 |
tou[i] = (unsigned char)i; |
53 |
tol[i] = (unsigned char)i; |
54 |
tos[i] = (unsigned char)' '; |
55 |
scd[i] = ' '; |
56 |
pcd[i] = ' '; |
57 |
} |
58 |
for (i=0;i<l;i++) { |
59 |
tou[lchars[i]] = uchars[i]; |
60 |
tol[uchars[i]] = lchars[i]; |
61 |
tos[uchars[i]] = uchars[i]; |
62 |
tos[lchars[i]] = lchars[i]; |
63 |
scd[uchars[i]] = scodes[i]; |
64 |
scd[lchars[i]] = scodes[i]; |
65 |
pcd[uchars[i]] = pcodes[i]; |
66 |
pcd[lchars[i]] = pcodes[i]; |
67 |
} |
68 |
for (i=0;i<10;i++) { |
69 |
tos[nums[i]] = nums[i]; |
70 |
} |
71 |
} |
72 |
|
73 |
unsigned char ToUpper(c) |
74 |
unsigned char c; |
75 |
{ |
76 |
return (tou[c]); |
77 |
} |
78 |
|
79 |
unsigned char ToLower(c) |
80 |
unsigned char c; |
81 |
{ |
82 |
return (tol[c]); |
83 |
} |
84 |
|
85 |
#define ToUpper(c) (tou[c]) |
86 |
#define ToLower(c) (tol[c]) |
87 |
#define SoundexLen 4 /* length of a soundex code */ |
88 |
#define SoundexKey "Z000" /* default key for soundex code */ |
89 |
|
90 |
bool IsAlpha(c) |
91 |
unsigned char c; |
92 |
{ |
93 |
return ((bool) scd[c] != ' '); |
94 |
} |
95 |
|
96 |
bool IsVowel(c) |
97 |
unsigned char c; |
98 |
{ |
99 |
return ((bool) scd[c] == '0'); |
100 |
} |
101 |
|
102 |
static char SCode(c) |
103 |
unsigned char c; |
104 |
{ |
105 |
return (scd[c]); |
106 |
} |
107 |
|
108 |
#define IsAlpha(c) (scd[c] != ' ') |
109 |
#define IsVowel(c) (scd[c] == '0') |
110 |
#define SCode(c) (scd[c]) |
111 |
|
112 |
char PCode(c) |
113 |
unsigned char c; |
114 |
{ |
115 |
return (pcd[c]); |
116 |
} |
117 |
|
118 |
void SoundexCode (Name, Key) |
119 |
unsigned char *Name; |
120 |
unsigned char *Key; |
121 |
{ |
122 |
unsigned char LastLetter; |
123 |
int Index; |
124 |
|
125 |
/* set default key */ |
126 |
strcpy(Key, SoundexKey); |
127 |
|
128 |
/* keep first letter */ |
129 |
Key[0] = *Name; |
130 |
LastLetter = *Name; |
131 |
Name++; |
132 |
|
133 |
/* scan rest of string */ |
134 |
for (Index = 1; (Index < SoundexLen) && *Name; Name++) |
135 |
{ |
136 |
/* use only letters */ |
137 |
if (IsAlpha(*Name)) |
138 |
{ |
139 |
/* ignore duplicate successive chars */ |
140 |
if (LastLetter != *Name) |
141 |
{ |
142 |
/* new LastLetter */ |
143 |
LastLetter = *Name; |
144 |
|
145 |
/* ignore letters with code 0 */ |
146 |
if (!IsVowel(*Name) && (SCode(*Name) != 0)) |
147 |
{ |
148 |
Key[Index] = SCode(*Name); |
149 |
Index++; |
150 |
} |
151 |
} |
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
static unsigned char * isolc (s, l) |
157 |
unsigned char * s; |
158 |
int l; |
159 |
{ |
160 |
int i; |
161 |
for (i=0;i<l;i++) { |
162 |
s[i] = tol[s[i]]; |
163 |
} |
164 |
return(s); |
165 |
} |
166 |
|
167 |
static unsigned char * isouc (s, l) |
168 |
unsigned char * s; |
169 |
int l; |
170 |
{ |
171 |
int i; |
172 |
|
173 |
for (i=0;i<l;i++) { |
174 |
s[i] = tou[s[i]]; |
175 |
} |
176 |
return(s); |
177 |
} |
178 |
|
179 |
static unsigned char * isotr (s, l) |
180 |
unsigned char * s; |
181 |
int l; |
182 |
{ |
183 |
int i; |
184 |
|
185 |
for (i=0;i<l;i++) { |
186 |
s[i] = tos[s[i]]; |
187 |
} |
188 |
return(s); |
189 |
} |
190 |
|
191 |
MODULE = WAIT PACKAGE = WAIT::Filter |
192 |
|
193 |
PROTOTYPES: ENABLE |
194 |
|
195 |
BOOT: |
196 |
init_lcuc (); |
197 |
|
198 |
|
199 |
char * |
200 |
isolc(word) |
201 |
char * word |
202 |
CODE: |
203 |
{ |
204 |
char *copy; |
205 |
ST(0) = sv_mortalcopy(ST(0)); |
206 |
copy = (char *)SvPV(ST(0),na); |
207 |
(void) isolc(copy, (int)na); |
208 |
} |
209 |
|
210 |
char * |
211 |
isouc(word) |
212 |
char * word |
213 |
CODE: |
214 |
{ |
215 |
char *copy; |
216 |
ST(0) = sv_mortalcopy(ST(0)); |
217 |
copy = (char *)SvPV(ST(0),na); |
218 |
(void) isouc(copy, (int)na); |
219 |
} |
220 |
|
221 |
char * |
222 |
isotr(word) |
223 |
char * word |
224 |
CODE: |
225 |
{ |
226 |
char *copy; |
227 |
ST(0) = sv_mortalcopy(ST(0)); |
228 |
copy = (char *)SvPV(ST(0),na); |
229 |
(void) isotr(copy, (int)na); |
230 |
} |
231 |
|
232 |
char * |
233 |
disolc(word) |
234 |
char * word |
235 |
CODE: |
236 |
{ |
237 |
(void) isolc(word, (int)na); |
238 |
} |
239 |
|
240 |
char * |
241 |
disouc(word) |
242 |
char * word |
243 |
CODE: |
244 |
{ |
245 |
(void) isouc(word, (int)na); |
246 |
} |
247 |
|
248 |
char * |
249 |
disotr(word) |
250 |
char * word |
251 |
CODE: |
252 |
{ |
253 |
(void) isotr(word, (int)na); |
254 |
} |
255 |
|
256 |
char * |
257 |
Soundex(word) |
258 |
char * word |
259 |
CODE: |
260 |
{ |
261 |
char key[5]; |
262 |
Soundex (word, key); |
263 |
ST(0) = sv_newmortal(); |
264 |
sv_setpv((SV *) ST(0), key); |
265 |
} |
266 |
|
267 |
char * |
268 |
Phonix (word) |
269 |
char * word |
270 |
CODE: |
271 |
{ |
272 |
char key[9]; |
273 |
Phonix (word, key); |
274 |
ST(0) = sv_newmortal(); |
275 |
sv_setpv((SV *) ST(0), key); |
276 |
} |
277 |
|
278 |
char * |
279 |
Stem (word) |
280 |
char * word |
281 |
CODE: |
282 |
{ |
283 |
char copy[80]; |
284 |
strncpy(copy, word, 79); |
285 |
if (Stem(copy)) { |
286 |
ST(0) = sv_newmortal(); |
287 |
sv_setpv((SV *) ST(0), copy); |
288 |
} |
289 |
} |
290 |
|
291 |
char * |
292 |
Metaphone (word) |
293 |
char * word |
294 |
CODE: |
295 |
{ |
296 |
char metaph[80]; |
297 |
metaph[0] = '\0'; |
298 |
phonetic(word,metaph,79); |
299 |
ST(0) = sv_newmortal(); |
300 |
sv_setpv((SV *) ST(0), metaph); |
301 |
} |
302 |
|
303 |
void |
304 |
split_pos(ipair) |
305 |
SV * ipair; |
306 |
PPCODE: |
307 |
{ |
308 |
AV * aipair = (AV *) SvRV(ipair); |
309 |
char * word = (char *)SvPV(*av_fetch(aipair, 0, 0),na); |
310 |
int offset = (av_len(aipair)?SvIV(*av_fetch(aipair, 1, 0)):0); |
311 |
char * begin = word; |
312 |
SV * pair[2]; |
313 |
|
314 |
pair[0] = newSV((STRLEN)20); |
315 |
pair[1] = newSV((STRLEN)0); |
316 |
|
317 |
while (*word) { |
318 |
char * start; |
319 |
AV * apair; |
320 |
SV * ref; |
321 |
while (*word && isspace(*word)) word++; |
322 |
if (!*word) break; |
323 |
start = word; |
324 |
while (*word && !isspace(*word)) word++; |
325 |
EXTEND(sp, 1); |
326 |
sv_setpvn(pair[0], start, (STRLEN)(word-start)); |
327 |
sv_setiv(pair[1],offset + start - begin); |
328 |
apair = av_make(2, pair); |
329 |
ref = newRV((SV*) apair); |
330 |
SvREFCNT_dec(apair); |
331 |
PUSHs(sv_2mortal(ref)); |
332 |
} |
333 |
/* free pair */ |
334 |
SvREFCNT_dec(pair[0]); |
335 |
SvREFCNT_dec(pair[1]); |
336 |
} |
337 |
|
338 |
MODULE = WAIT PACKAGE = WAIT::Table |
339 |
int |
340 |
max(left,right=0) |
341 |
int left |
342 |
int right |
343 |
CODE: |
344 |
{ |
345 |
RETVAL = (left>right)?left:right; |
346 |
ST(0) = sv_newmortal(); |
347 |
sv_setiv(ST(0), (IV)RETVAL); |
348 |
} |
349 |
|
350 |
|
351 |
MODULE = WAIT PACKAGE = WAIT::Metric |
352 |
|
353 |
int |
354 |
WLD(word,towards,mode=' ',limit=0) |
355 |
char * word |
356 |
char * towards |
357 |
char mode |
358 |
int limit |