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
|
# File 'lib/string_metric/levenshtein/iterative_with_two_matrix_rows.rb', line 6
def self.distance(from, to, options = {})
max_distance = options[:max_distance]
insertion_cost = options.fetch(:insertion_cost, 1)
deletion_cost = options.fetch(:deletion_cost, 1)
substitution_cost = options.fetch(:substitution_cost, 1)
m = from.length
n = to.length
if max_distance && (n - m).abs >= max_distance
return max_distance
end
return 0 if from == to
return n if m.zero?
return m if n.zero?
v0 = (0..m).to_a
x = 0
n.times do |i|
current = x = i + 1
sub_cell = v0[0]
m.times do |j|
cost = (from[j] == to[i]) ? 0 : substitution_cost
ins_cell = v0[j+1]
x = [current + deletion_cost, ins_cell + insertion_cost, sub_cell + cost ].min
v0[j] = current
current = x
sub_cell = ins_cell
end
v0[m] = x
break if max_distance && v0.min > max_distance
end
if max_distance && x > max_distance
max_distance
else
x
end
end
|