A method for calculating the shortest editing distance in Ruby _ruby special topics

Source: Internet
Author: User
Tags chop diff

The calculation of the shortest editing distance is realized by using dynamic programming algorithm.

Copy Code code 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}"


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.