The shortest editing distance calculation method implemented by Ruby, And the ruby Calculation Method

Source: Internet
Author: User
Tags chop

The shortest editing distance calculation method implemented by Ruby, And the ruby Calculation Method

The dynamic planning algorithm is used to calculate the shortest editing distance.

Copy codeThe Code is as follows:
# Encoding: UTF-8
# Author: xu jin
# Date: Nov 12,201 2
# EditDistance
# To find the minimum cost by using EditDistance algorithm
# Example output:
# "Please input a string :"
# Exponential
# "Please input 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}
P "Please input the other string :"
Y = gets. chop. chars. map {| 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, insert, 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 in (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 here
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.