summaryrefslogtreecommitdiff
path: root/intersect.rb
blob: 23d8fdbd9d2d99b9ba4d5938255c63d0ff375b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby

require_relative './grammar.rb'


class Chart
  attr_accessor :m

  def initialize input
    @m = []
    (input.size+1).times {
      _ = []
      (input.size+1).times { _ << [] }
      @m << _
    }
  end

  def at i,j
    return @m[i][j]
  end
end

class Item < Rule
  attr_accessor :lhs, :rhs, :span, :dot

  def initialize rule, dot=-1
    @lhs = rule.lhs.dup
    @rhs = rule.rhs.dup
    @span = Span.new rule.span.left, rule.span.right
    @dot = dot
    if rule.class==Item
      @dot = rule.dot
    end
  end

  def to_s
    "#{lhs} -> #{rhs.map{|i|i.to_s}.insert(@dot,'*').join ' '} dot:#{@dot} a:#{arity} (#{@span.left}, #{@span.right})"
  end
end

# set-up
g = Grammar.new 'grammar'
input = "ich sah ein kleines haus".split.map { |i| Terminal.new i }
passive_chart = Chart.new input
active_chart = Chart.new input

# pre-fill passive chart w/ 0-arity rules
g.rules.select { |r| r.rhs.first.class==Terminal }.each { |r|
  input.each_index.select{ |j| input[j].w==r.rhs.first.w }.each { |j|
    k = 1
    if r.rhs.size > 1
      z = r.rhs.index { |i| i.class==NonTerminal }
      if z
        z -= 1
      else
        z = r.rhs.size-1
      end
      slice = input[j..j+z].map { |i| i.w }
      if slice == r.rhs[0..z].map { |i| i.w }
        k = z+1
      else
        next
      end
    end
    if k == r.rhs.size
      passive_chart.at(j,j+k) << Item.new(r)
      passive_chart.at(j,j+k).last.span.left = j
      passive_chart.at(j,j+k).last.span.right = j+k
      passive_chart.at(j,j+k).last.dot = k
    else
      (j+k).upto(input.size) { |l|
      active_chart.at(j,l) << Item.new(r)
      active_chart.at(j,l).last.span.left = j
      active_chart.at(j,l).last.span.right = j+k
      active_chart.at(j,l).last.dot = k
      }
    end
  }
}

# seed active chart
s = g.rules.reject { |r| r.rhs.first.class!=NonTerminal }
(input.size).times { |k|
  0.upto(input.size-(k+2)) { |i|
    s.each { |r|
      active_chart.at(i,i+k+2) << Item.new(r)
      active_chart.at(i,i+k+2).last.span.left = i
      active_chart.at(i,i+k+2).last.span.right = i
      active_chart.at(i,i+k+2).last.dot = 0
    }
  }
}

# scan
def scan i, j, active_chart, passive_chart, input
  active_chart.at(i,j).each { |item|
    while item.rhs[item.dot].class == Terminal
      if item.rhs[item.dot].w == input[item.span.left+item.dot].w
        item.dot += 1
        if item.dot == item.rhs.size
          passive_chart.at(i,j) << Item.new(item)
          passive_chart.at(i,j).last.span.right = item.span.left+item.dot
        end
      end
    end
  }
end

def scan2 item, passive_chart, input, i, j
  while item.rhs[item.dot].class == Terminal 
    if item.rhs[item.dot].w == input[item.span.left+item.dot].w
      item.dot += 1
      item.span.right += 1
      if item.dot == item.rhs.size
        passive_chart.at(i,j) << Item.new(item)
        passive_chart.at(i,j).last.span.right = item.span.left+item.dot
      end
    end
  end
end

# parse
def parse i, j, sz, active_chart, passive_chart, g, input
  puts "| #{i},#{j}"
  #scan i, j, active_chart, passive_chart, input
  1.upto(sz) { |span|
    break if span==(j-i)
    i.upto(j-span) { |k|
    puts "  #{k},#{k+span} (##{span})"
    # complete 
    active_chart.at(i,j).each { |active_item|
      passive_chart.at(k, k+span).each { |passive_item|
        if active_item.rhs[active_item.dot].class==NonTerminal && passive_item.lhs.sym == active_item.rhs[active_item.dot].sym
          next if not active_item.span.right==passive_item.span.left
          active_item.span.right = passive_item.span.right
          active_item.dot += 1
          scan2 active_item, passive_chart, input, i, j
          if active_item.dot == active_item.rhs.size
            passive_chart.at(i,j) << Item.new(active_item)
          end
        end
      }
    }
  }
  #scan i, j, active_chart, passive_chart, input

  }
end


def visit n
  n.times { |i|
    0.upto(n-(i+2)) { |j|
      yield j, j+i+2 if block_given?
    }
  }
end


visit(input.size) { |i,j|
  puts "#{i},#{j}" 
}

# once for each cell
(input.size).times { |k|
  0.upto(input.size-(k+2)) { |i|
    parse i, i+k+2, input.size, active_chart, passive_chart, g, input
  }
}


puts "---"
passive_chart.at(0,5).each { |item|
  puts item.to_s
}