Method: Coopy::CompareTable#align_core_2

Defined in:
lib/coopy/compare_table.rb

#align_core_2(align, a, b) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/coopy/compare_table.rb', line 35

def align_core_2(align, a, b) 
  if (align.meta.nil?) 
    align.meta = Coopy::Alignment.new
  end
  align_columns(align.meta,a,b)
  column_order = align.meta.to_order
  common_units = []
  column_order.get_list.each do |unit| 
    if (unit.l>=0 && unit.r>=0 && unit.p!=-1) 
      common_units << unit
    end
  end

  align.range(a.height,b.height)
  align.tables(a,b)
  align.set_rowlike(true)
    
  w  = a.width
  ha = a.height
  hb = b.height

  av = a.get_cell_view

  # If we have more columns than we have time to process their
  # combinations, we need to haul out some heuristics.

  n = 5
  columns = []
  if (common_units.length>n) 
    columns_eval = []
    (0..common_units.length-1).each do |i| 
      ct = 0
      mem = {}
      mem2 = {}
      ca = common_units[i].l
      cb = common_units[i].r
      (0..ha-1).each do |j| 
        key = av.to_s(a.get_cell(ca,j))
        if (!mem.has_key?(key)) 
          mem[key] = 1
          ct+=1
        end
      end
      (0..hb-1).each do |j| 
        key = av.to_s(b.get_cell(cb,j))
        if (!mem2.has_key?(key)) 
          mem2[key] = 1
          ct+=1
        end
      end
      columns_eval << [i,ct]
    end
    columns_eval.sort { |a,b| a[1] <=> b[1] }
    columns = columns_eval.map{ |v| v[0] }
    columns = columns.slice(0,n)
  else
    (0..common_units.length-1).each do |i| 
      columns << i
    end
  end

  top = (2 ** columns.length).round

  pending = {}
  (0...ha).each do |j| 
    pending[j] = j
  end
  pending_ct = ha
  
  (0...top).each do |k| 
    next if (k==0)
    break if (pending_ct == 0)
    active_columns = []
    kk = k
    at = 0
    while (kk>0) 
      if (kk%2==1) 
        active_columns << columns[at]
      end
      kk >>= 1
      at+=1
    end

    index = IndexPair.new
    (0...active_columns.length).each do |k|
      unit = common_units[active_columns[k]]
      index.add_columns(unit.l,unit.r)
      align.add_index_columns(unit)
    end
    index.index_tables(a,b)

    h = a.height
    h = b.height if (b.height>h)
    h = 1 if (h<1)
    wide_top_freq = index.get_top_freq
    ratio = wide_top_freq
    ratio /= (h+20) # "20" allows for low-data 
    next if (ratio>=0.1) # lousy no-good index, move on

    if @indexes
      @indexes << index
    end

    fixed  = []
    pending.keys.each do |j| 
      cross = index.query_local(j)
      spot_a = cross.spot_a
      spot_b = cross.spot_b
      next if (spot_a!=1 || spot_b!=1)
      fixed << j
      align.link(j,cross.item_b.lst[0])
    end
    (0...fixed.length).each do |j|
      pending.delete(fixed[j])
      pending_ct-=1
    end
  end
  # we expect headers on row 0 - link them even if quite different.
  align.link(0,0)
end