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
|
# File 'lib/emiler/jarowinkler.rb', line 26
def getDistance(s1, s2)
a1 = s1.split(//)
a2 = s2.split(//)
max, min = s1.size > s2.size ? [a1, a2] : [a2, a1]
range = [(max.size / 2 - 1), 0].max
indexes = Array.new(min.size, -1)
flags = Array.new(max.size, false)
matches = 0
(0...min.size).each do |mi|
c1 = min[mi]
xi = [mi - range, 0].max
xn = [mi + range + 1, max.size].min
(xi...xn).each do |i|
next unless !flags[i] && c1 == max[i]
indexes[mi] = i
flags[i] = true
matches += 1
break
end
end
ms1 = Array.new(matches, nil)
ms2 = Array.new(matches, nil)
si = 0
(0...min.size).each do |i|
if indexes[i] != -1
ms1[si] = min[i]
si += 1
end
end
si = 0
(0...max.size).each do |i|
if flags[i]
ms2[si] = max[i]
si += 1
end
end
transpositions = 0
(0...ms1.size).each do |mi|
transpositions += 1 if ms1[mi] != ms2[mi]
end
prefix = 0
(0...min.size).each do |mi|
prefix += 1 if s1[mi] == s2[mi]
break unless s1[mi] == s2[mi]
end
if 0 == matches
0.0
else
m = matches.to_f
t = (transpositions / 2)
j = ((m / s1.size) + (m / s2.size) + ((m - t) / m)) / 3.0
return j < THRESHOLD ? j : j + [0.1, 1.0 / max.size].min * prefix * (1 - j)
end
end
|