-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_distance.rb
executable file
·46 lines (41 loc) · 1.19 KB
/
edit_distance.rb
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
#!/usr/bin/ruby
module EditDistance
def self.calculate(x, y)
matrix = Hash.new
(y.length + 1).times do |n| # incializa primeira linha
matrix[[0,n]] = n
end
(x.length + 1).times do |n| # inicializa primeira coluna
matrix[[n,0]] = n
end
1.upto(x.length) do |m|
1.upto(y.length) do |n|
phi = x[m-1] == y[n-1] ? 0 : 1
val = [(matrix[[m-1,n-1]] + phi), (matrix[[m-1, n]] + 1),
(matrix[[m, n-1]] + 1)].min
matrix[[m,n]] = val
end
end
matrix
end
def self.tryHarder(x, y)
matrix = Hash.new
(x.length + 1).times do |n| # inicializa primeira coluna
matrix[[n,0]] = n
end
1.upto(y.length) do |n|
matrix[[0,1]] = n # ajusta elementos da primeira linha
1.upto(x.length) do |m|
phi = x[m-1] == y[n-1] ? 0 : 1
# puts x[m-1].to_s + " " + y[n-1].to_s
val = [(matrix[[m-1,0]] + phi), (matrix[[m, 0]] + 1),
(matrix[[m-1, 1]] + 1)].min
matrix[[m,1]] = val
matrix[[m-1,0]] = matrix[[m-1,1]] # atualiza matriz
end
matrix[[x.length,0]] = matrix[[x.length,1]]
end
matrix
end
end
puts EditDistance.tryHarder("CADA", "ABADAC").inspect