-
Notifications
You must be signed in to change notification settings - Fork 11
/
fuzzy.pyx
263 lines (206 loc) · 5.18 KB
/
fuzzy.pyx
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
# cython: c_string_type=unicode, c_string_encoding=ascii
import re
cdef extern from "string.h":
int strlen(char *s)
cdef extern from "ctype.h":
int toupper(int c)
cdef extern from "stdlib.h":
void * malloc(int i)
void free(void * buf)
_nysiis_suffix_map = {
'IX': 'IC',
'EX': 'EC',
'YE': 'Y',
'EE': 'Y',
'IE': 'Y',
'DT': 'D',
'RT': 'D',
'RD': 'D',
'NT': 'D',
'ND': 'D'
}
_nysiis_transforms = {
'AY': 'Y',
'DG': 'G',
'E': 'A',
'EY': 'Y',
'GHT': 'GT',
'K': 'C',
'KN': 'N',
'I': 'A',
'IY': 'Y',
'O': 'A',
'OY': 'Y',
'PH': 'F',
'SH': 'S',
'SCH': 'S',
'U': 'A',
'UY': 'Y',
'WR': 'R',
'YW': 'Y'
}
_nysiis_trans_not_first = {
'AH': 'A',
'AW': 'A',
'EH': 'A',
'EV': 'AF',
'EW': 'A',
'HA': 'A',
'HE': 'A',
'HI': 'A',
'HO': 'A',
'HU': 'A',
'IH': 'A',
'IW': 'A',
'M': 'N',
'OH': 'A',
'OW': 'A',
'Q': 'G',
'UH': 'A',
'UW': 'A',
'Z': 'S'
}
_nysiis_trans_middle = {
'Y': 'A'
}
_non_AZ = re.compile('[^A-Z]')
def nysiis(s):
cdef int i, start, stop
cdef char *suffix
cdef char *first
# Normally we would strip out Roman numerals and name suffixes,
# but we are not going to use this for person names.
# Strip out anything non-alpha
s = _non_AZ.sub('', s.upper())
start, stop = 0, len(s)
first = ''
if stop:
foo = s[0]
first = foo
# Find index without trailing SZs
i = stop
while i and s[i-1] in 'SZ':
i = i - 1
stop = i
# Initial MAC -> MC, PF -> F
if s[:3] == 'MAC':
s = 'MC' + s[3:]
stop = stop - 1
elif s[:2] == 'PF':
start = 1
# Translate 2-character suffix elements
suffix = ''
while (stop - start) > 2:
x = s[stop-2:stop]
if x in _nysiis_suffix_map:
y = _nysiis_suffix_map[x] + suffix
suffix, stop = y, stop - 2
else:
break
s = s[start:stop] + suffix
# Build a list of adjacent components while performing transformations
r = []
i = start = 0
stop = len(s)
while i < stop:
remain = stop-i # number of letters including this one
app = ''
for l in 3, 2, 1:
if remain >= l:
x = s[i:i+l]
if x in _nysiis_transforms:
app = _nysiis_transforms[x]
break
elif i > start:
if x in _nysiis_trans_not_first:
app = _nysiis_trans_not_first[x]
break
elif i < (stop-1) and x in _nysiis_trans_middle:
app = _nysiis_trans_middle[x]
break
if app:
r.extend(app)
i = i + l
else:
r.append(s[i])
i = i + 1
#print i, s, app, l, r
# Remove trailing vowels
stop = len(r)
while stop and r[stop-1] in 'AEIOU':
stop = stop - 1
# If first char of original string is a A vowel, use it
if first in 'AEIOU':
if r:
r[0] = first
else:
r = [first]
# Filter out repeated characters
q, last = [], ''
for x in r[:stop]:
if x == last:
continue
q.append(x)
last = x
return ''.join(q)
cdef class Soundex:
cdef int size
cdef char *map
def __init__(self, size):
self.size = size
self.map = "01230120022455012623010202"
def __call__(self, s):
cdef char *cs
cdef int ls
cdef int i
cdef int written
cdef char *out
cdef char c
written = 0
out = <char *>malloc(self.size + 1)
cs = s
ls = strlen(cs)
for i from 0<= i < ls:
c = cs[i]
if c >= 97 and c <= 122:
c = c - 32
if c >= 65 and c <= 90:
if written == 0:
out[written] = c
written = written + 1
elif self.map[c - 65] != 48 and (written == 1 or
out[written - 1] != self.map[c - 65]):
out[written] = self.map[c - 65]
written = written + 1
if written == self.size:
break
for i from written <= i < self.size:
out[i] = 48
out[self.size] = 0
pout = out
free(out)
return pout
cdef extern from "double_metaphone.h":
void DoubleMetaphone(char *str, char **codes)
cdef class DMetaphone:
cdef int size
def __init__(self, size=0):
self.size = size or 99999
def __call__(self, s):
cdef char *cs
cdef char **out
cdef bytes o1
cdef bytes o2
out = <char **>malloc(sizeof(char *) * 2)
cs = s
DoubleMetaphone(cs, out)
o1 = out[0]
o2 = out[1]
if out[0] != NULL:
free(out[0])
if out[1] != NULL:
free(out[1])
free(out)
if o1 == o2:
o2 = None
return [o1 and o1[:self.size] or None, o2 and o2[:self.size] or None]