This article mainly introduced the Ruby implementation of the shortest editing distance calculation method, this article directly gives the implementation code, the need for friends can refer to the
The calculation of the shortest editing distance is realized by using dynamic programming algorithm.
The code is as follows:
#encoding: Utf-8
#author: Xu Jin
#date: Nov 12, 2012
#EditDistance
#to find the minimum cost by using editdistance algorithm
#example output:
# "Please input a string:"
# exponential
# "Please input to the other string:"
# polynomial
# "The expected cost is 6"
# The result is:
# ["E", "X", "P", "O", "n", "E", "N", "-", "T", "I", "a", "L"]
# ["-", "-", "P", "O", "L", "Y", "n", "O", "M", "I", "a", "L"]
P "Please input a string:"
x = gets.chop.chars.map{|c| C}
P "Please input the other string:"
y = gets.chop.chars.map{|c| C}
X.unshift ("")
Y.unshift ("")
E = Array.new (x.size) {array.new (y.size)}
Flag = Array.new (x.size) {array.new (y.size)}
DEL, INS, CHA, fit = (1..4). To_a #deleat, inserts, change, and fit
def edit_distance (x, Y, E, flag)
(0..x.length-1). each{|i| e[i][0] = i}
(0..y.length-1). each{|j| e[0][j] = j}
diff = array.new (x.size) {array.new (y.size)}
For I in (1..x.length-1) do
For J. (1..y.length-1) do
DIFF[I][J] = (x[i] = = Y[j])? 0:1
E[I][J] = [E[i-1][j] + 1, e[i][j-1] + 1, e[i-1][j-1] + diff[i][j]].min
If e[i][j] = = E[i-1][j] + 1
FLAG[I][J] = DEL
elsif E[i][j] = = E[i-1][j-1] + 1
FLAG[I][J] = CHA
elsif E[i][j] = = E[i][j-1] + 1
FLAG[I][J] = INS
else flag[i][j] = fit
End
End
End
End
out_x, out_y = [], []
def solution_structure (x, y, Flag, I, J, out_x, Out_y)
Case Flag[i][j]
When fit
Out_x.unshift (X[i])
Out_y.unshift (Y[j])
Solution_structure (x, Y, Flag, i-1, J-1, out_x, out_y)
When DEL
Out_x.unshift (X[i])
Out_y.unshift ('-')
Solution_structure (x, Y, Flag, I-1, J, out_x, Out_y)
When INS
Out_x.unshift ('-')
Out_y.unshift (Y[j])
Solution_structure (x, y, flag, I, J-1, out_x, out_y)
When CHA
Out_x.unshift (X[i])
Out_y.unshift (Y[j])
Solution_structure (x, Y, Flag, i-1, J-1, out_x, out_y)
End
#if flag[i][j] = = Nil, go
return if i = = 0 && J = = 0
If j = = 0
Out_y.unshift ('-')
Out_x.unshift (X[i])
Solution_structure (x, Y, Flag, I-1, J, out_x, Out_y)
elsif i = = 0
Out_x.unshift ('-')
Out_y.unshift (Y[j])
Solution_structure (x, y, flag, I, J-1, out_x, out_y)
End
End
Edit_distance (x, Y, E, flag)
P "The expected edit distance is #{e[x.length-1][y.length-1]}"
Solution_structure (x, Y, Flag, x.length-1, Y.length-1, out_x, out_y)
Puts "The result is:n #{out_x}n #{out_y}"