-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggestions.py
40 lines (34 loc) · 1.35 KB
/
suggestions.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
def iterative_levenshtein(s, t):
"""
iterative_levenshtein(s, t) -> ldist
ldist is the Levenshtein distance between the strings
s and t.
For all i and j, dist[i,j] will contain the Levenshtein
distance between the first i characters of s and the
first j characters of t
"""
rows = len(s) + 1
cols = len(t) + 1
# creazione matrice
dist = [[0 for x in range(cols)] for x in range(rows)]
# matrice con prima colonna e riga numerate
for i in range(1, rows):
dist[i][0] = i
for i in range(1, cols):
dist[0][i] = i
for col in range(1, cols):
for row in range(1, rows):
cost = 0 if s[row - 1] == t[col - 1] else 1
dist[row][col] = min(dist[row - 1][col] + 1, # deletion
dist[row][col - 1] + 1, # insertion
dist[row - 1][col - 1] + cost) # substitution
return dist[row][col]
def evalSuggestions(testArr, ref, debug=True):
# valuta un array di candidati contro una parola di riferimento
def word_ref_distance(it):
return dict(w=it, dist=iterative_levenshtein(it, ref))
res = map(word_ref_distance, testArr)
res = sorted(res, key=lambda k: k['dist'], reverse=False)
if debug:
print('Suggestions:', res)
return res