【发布时间】:2014-11-10 03:35:55
【问题描述】:
我想在较大的字符串中找出一个具有一定 Levenshtein 距离的字符串。我已经编写了用于查找两个字符串之间距离的代码,但是当我想找到一些具有固定 Levenshtein 距离的子字符串时想要有效地实现。
module Levenshtein
def self.distance(a, b)
a, b = a.downcase, b.downcase
costs = Array(0..b.length) # i == 0
(1..a.length).each do |i|
costs[0], nw = i, i - 1 # j == 0; nw is lev(i-1, j)
(1..b.length).each do |j|
costs[j], nw = [costs[j] + 1, costs[j-1] + 1, a[i-1] == b[j-1] ? nw : nw + 1].min, costs[j]
end
end
costs[b.length]
end
def self.test
%w{kitten sitting saturday sunday rosettacode raisethysword}.each_slice(2) do |a, b|
puts "distance(#{a}, #{b}) = #{distance(a, b)}"
end
end
end
【问题讨论】:
标签: ruby-on-rails fuzzy-search fuzzy-logic