merge distance between two string or array
need to support matrix
class MergeDistance
attr_reader :gn
attr_writer :gn
def initialize
@gn = 0
end
def merge_table(one, other)
m = one.length
n = otherlength
if m != n then
puts "error!!!!"
return true
end
ret = Array.new
index = 0
one.each_index do |x|
a = Array.new
arr = one[x]
arr.each_index do |y|
if (one[x][y] == 1) and (1==other[x][y]) then
a[y] = 1
else
a[y] = 0
end
end
ret[index] = a
index += 1
end
return ret
end
def union_table(one, other)
m = one.length
n = otherlength
if m != n then
puts "error!!!!"
return true
end
ret = Array.new
index = 0
one.each_index do |x|
a = Array.new
arr = one[x]
arr.each_index do |y|
if (one[x][y] ==1) or (1 == other[x][y]) then
a[y] = 1
else
a[y] = 0
end
end
ret[index] = a
index += 1
end
return ret
end
def max_tree(graph)
end
def max_substr(one, other)
x = one.length
y = other.length
temp = Array.new
tl = 0
maxMatch = 0
one.each_index do |m|
i = m
loop do
break if i>= x
other.each_index do |j|
if (i+j) > (x-1) then
break
end
if one[i+j] == other[j] then
temp[tl] = other[j]
tl += 1
else
if tl >= maxMatch then
maxMatch = tl
end
tl = 0
end
end
i+=1
end #loop
end
return maxMatch
end
def get_ss(x, y)
ret = Array.new
len = 0
y.each do |m|
find = false
x.each do |n|
if m == n then
find = true
break
end
end
if find then
ret[len] = m
len+=1
end
end
return ret
end
def md(x,y)
z = get_ss(x,y)
return (x.length - max_substr(x,z))
end
def id(x,y)
z = get_ss(x,y)
return (y.length - max_substr(x,z))
end
def mc(x, y)
m = x.length
n = y.length
if (m>n) then
a = id(y,x)
b = a.to_f/m
c = md(y,x)+b
ret1= (1 - c.to_f/(n+1))
return ret1
elsif (m a = id(x,y)
b = a.to_f/n
c = md(x,y) +b
ret2 = (1- c.to_f/(m+1))
return ret2
else
a = id(x,y)
b = a.to_f/n
c = md(x,y)+b
ret1= (1 - c.to_f/(m+1))
a = id(y,x)
b = a.to_f/m
c = md(y,x) +b
ret2 = (1- c.to_f/(n+1))
return (ret1>ret2)?ret1:ret2
end
end
end
class Module
attr_reader :modTable
def initialize
@modTable = Array.new
end
def add_array(arr)
@modTable.push(arr)
end
end
阅读(1663) | 评论(0) | 转发(0) |