-
Notifications
You must be signed in to change notification settings - Fork 0
/
distances.py
262 lines (211 loc) · 7.29 KB
/
distances.py
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
import string
from collections import deque
import numpy as np
import utils
import jaro
class Distance:
def __init__(self, glossary: dict = None, max_proposition=5, select_fn=1):
self.glossary = glossary
self.max_proposition = max_proposition
self.select_fn = select_fn
def __call__(self, w1, w2):
return self.distance(w1, w2)
def distance(self, w1, w2):
raise NotImplementedError()
def _select_0(self, w):
props = deque([], maxlen=self.max_proposition)
min_dist = np.inf
for word in self.glossary.keys():
dist = self.distance(w, word)
if dist <= min_dist:
props.appendleft(word)
min_dist = dist
return list(props)
def _select_1(self, w):
props = deque([], maxlen=self.max_proposition)
dists = deque([], maxlen=self.max_proposition)
for word in self.glossary.keys():
dist = self.distance(w, word)
min_dist = np.inf if len(dists) == 0 else dists[0]
if dist <= min_dist:
props.appendleft(word)
dists.appendleft(dist)
elif dist <= np.mean(dists):
if len(props) == props.maxlen:
props.pop()
dists.pop()
props.insert(1, word)
dists.insert(1, dist)
return list(props)
def _select_2(self, w):
props = {}
chosen_props = []
for word in self.glossary.keys():
dist = self.distance(w, word)
props[word] = dist
for i in range(self.max_proposition):
word = list(props.keys())[np.argmin(list(props.values()))]
chosen_props.append(word)
props.pop(word)
return chosen_props
def select(self, w):
if self.select_fn == 0:
return self._select_0(w)
elif self.select_fn == 1:
return self._select_1(w)
elif self.select_fn == 2:
return self._select_2(w)
def propositions(self, w: str or list):
if isinstance(w, str):
if w in self.glossary:
return [w]
else:
return self.select(w)
elif isinstance(w, list):
return [(w0, self.propositions(w0)) for w0 in w]
class NullDistance(Distance):
def distance(self, w1, w2):
return 0
class HammingDistance(Distance):
def distance(self, w1, w2):
min_length = min(len(w1), len(w2))
diff = 0
for i in range(min_length):
if w1[i] != w2[i]:
diff += 1
diff += abs(len(w1) - len(w2))
return diff
class JaccardDistance(Distance):
def __init__(self, nb_char=2, *args, **kwargs):
super().__init__(*args, **kwargs)
self.nb_char = nb_char
def _separate(self, word) -> set:
w = set()
for i in range(len(word)-self.nb_char+1):
w.add(word[i:i+self.nb_char])
return w
def distance(self, w1, w2):
w1 = self._separate(w1)
w2 = self._separate(w2)
union = w1.union(w2)
intersection = w1.intersection(w2)
jaccard_index = len(intersection)/len(union)
return 1 - jaccard_index
class LevenshteinDistance(Distance):
def _sub(self, w1: list, w2: list, i, j):
w2 = w2.copy()
if w1[i] == w2[j]:
return w1, w2, 0
else:
w2[j] = w1[i]
return w1, w2, 1
def _ins(self, w1: list, w2: list, i, j):
w2 = w2.copy()
w2.insert(j+1, w1[i])
return w1, w2, 1
def _del(self, w1: list, w2: list, i, j):
w2 = w2.copy()
w2.pop(j)
return w1, w2, 1
def solve(self, w1: list, w2: list, i: int, j: int):
if i == -1:
if j == -1:
return 0
else:
t1, t2, c = self._del(w1, w2, i, j)
return self.solve(t1, t2, i, j-1) + c
else:
if j == -1:
t1, t2, c = self._ins(w1, w2, i, j)
return self.solve(t1, t2, i-1, j) + c
else:
t1, t2, c = self._sub(w1, w2, i, j)
i1 = self.solve(t1, t2, i-1, j-1) + c
t1, t2, c = self._ins(w1, w2, i, j)
i2 = self.solve(t1, t2, i-1, j) + c
t1, t2, c = self._del(w1, w2, i, j)
i3 = self.solve(t1, t2, i, j-1) + c
return min(i1, i2, i3)
def distance(self, w1, w2):
w1 = list(w1)
w2 = list(w2)
return self.solve(w1, w2, len(w1) - 1, len(w2) - 1)
class SoundexDistance(Distance):
def __init__(self, soundex_file=None, distance_fn=None, *args, **kwargs):
super(SoundexDistance, self).__init__(*args, **kwargs)
self.distance_fn = distance_fn
self.soundex_dict = {}
if soundex_file is None:
self.char_to_int = {
'B': '1',
'F': '1',
'P': '1',
'V': '1',
'C': '2',
'G': '2',
'J': '2',
'K': '2',
'Q': '2',
'S': '2',
'X': '2',
'Z': '2',
'D': '3',
'T': '3',
'L': '4',
'M': '5',
'N': '5',
'R': '6'
}
self.bad_letters = 'AEIOUHWY'
for word in self.glossary.keys():
soundex_code = self.soundex(word)
if soundex_code in self.soundex_dict:
self.soundex_dict[soundex_code].append(word)
else:
self.soundex_dict[soundex_code] = [word]
utils.save_obj('soundex.code', self.soundex_dict)
else:
self.soundex_dict = utils.load_obj(soundex_file)
def soundex(self, word: str):
w = word.upper()
w = [i for i in w if i in string.ascii_uppercase]
first_letter = w[:1]
w = w[1:]
w = [i for i in w if i not in self.bad_letters]
tmp = []
prev_i = -1
for c in w:
i = self.char_to_int[c]
if prev_i != i:
tmp.append(i)
prev_i = i
w = first_letter + tmp
w = w[:4]
w = ''.join(w)
w = w.ljust(4, '0')
return w
def distance(self, w1, w2):
code_1 = self.soundex(w1)
code_2 = self.soundex(w2)
if self.distance_fn is not None:
return self.distance_fn(code_1, code_2)
else:
if code_1 != code_2:
return 1
else:
return 0
def propositions(self, w: str or list):
if self.distance_fn is None:
if isinstance(w, str):
code = self.soundex(w)
return self.soundex_dict[code]
else:
return [(w0, self.soundex_dict[self.soundex(w0)][:self.max_proposition]) for w0 in w]
else:
return super(SoundexDistance, self).propositions(w)
class JaroDistance(Distance):
def __init__(self, winkler=True, *args, **kwargs):
super().__init__(*args, **kwargs)
self.winkler = winkler
def distance(self, w1, w2):
return 1 - (jaro.jaro_winkler_metric(w1, w2) if self.winkler else jaro.jaro_metric(w1, w2))