Chinaunix首页 | 论坛 | 博客
  • 博客访问: 623566
  • 博文数量: 138
  • 博客积分: 3067
  • 博客等级: 中校
  • 技术积分: 1565
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-21 12:44
文章分类

全部博文(138)

文章存档

2016年(5)

2014年(4)

2012年(1)

2011年(2)

2010年(10)

2009年(19)

2008年(97)

我的朋友

分类:

2010-01-05 23:59:07

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) |
给主人留下些什么吧!~~