summaryrefslogtreecommitdiff
path: root/parse.rb
blob: e075b8e5620704ef338387256a763ee2e7158609 (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
177
178
179
180
#!/usr/bin/env ruby

require_relative './grammar.rb'


class Chart

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

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

  def add item, i, j
    at(i,j) << item
    @b["#{i},#{j},#{item.lhs.symbol}"] = true
  end

  def has symbol, i, j
    return @b["#{i},#{j},#{symbol}"]
  end
end

class Span
  attr_accessor :left, :right

  def initialize left=nil, right=nil
    @left = left
    @right = right
  end
end

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

  def initialize rule_or_item, left, right, dot
    @lhs = rule_or_item.lhs
    @rhs = rule_or_item.rhs.dup
    @lhs.span = Span.new left, right
    @dot = dot
  end

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

def init input, n, active_chart, passive_chart, grammar
  grammar.flat.each { |r|
    input.each_index { |i|
      if input[i, r.rhs.size] == r.rhs.map { |x| x.word }
        passive_chart.add Item.new(r, i, i+r.rhs.size, r.rhs.size), i, i+r.rhs.size
      end
    }
  }
end

def scan item, input, limit, passive_chart
  while item.rhs[item.dot].class == T
    return false if item.lhs.span.right==limit
    if item.rhs[item.dot].word == input[item.lhs.span.right]
      item.dot += 1
      item.lhs.span.right += 1
    else
      return false
    end
  end
  return true
end

def visit i, l, r, x=0
  i.upto(r-x) { |span|
    l.upto(r-span) { |k|
      yield k, k+span
    }
  }
end

def parse input, n, active_chart, passive_chart, grammar
  visit(1, 0, n) { |i,j|

    STDERR.write " span(#{i},#{j})\n"

    # try to apply rules starting with T
    grammar.startt.select { |r| r.rhs.first.word == input[i] }.each { |r|
      new_item = Item.new r, i, i, 0
      active_chart.at(i,j) << new_item if scan new_item, input, j, passive_chart
    }

    # seed active chart
    grammar.startn.each { |r|
      next if r.rhs.size > j-i
      active_chart.at(i,j) << Item.new(r, i, i, 0)
    }
 
    # parse
    new_symbols = []
    remaining_items = []
    while !active_chart.at(i,j).empty?
      active_item = active_chart.at(i,j).pop
      advanced = false
      visit(1, i, j, 1) { |k,l|
        if passive_chart.has active_item.rhs[active_item.dot].symbol, k, l
          if k == active_item.lhs.span.right
            new_item = Item.new active_item, active_item.lhs.span.left, l, active_item.dot+1
            if scan new_item, input, j, passive_chart
              if new_item.dot == new_item.rhs.size
                if new_item.lhs.span.left == i && new_item.lhs.span.right == j
                  new_symbols << new_item.lhs.symbol if !new_symbols.include? new_item.lhs.symbol
                  passive_chart.add new_item, i, j
                  advanced = true
                end
              else
                if new_item.lhs.span.right+(new_item.rhs.size-(new_item.dot)) <= j
                  active_chart.at(i,j) << new_item
                  advanced = true
                end
              end
            end
          end
        end
      }
      if !advanced
        remaining_items << active_item
      end
    end

    # 'self-filling' step
    new_symbols.each { |s|
      remaining_items.each { |active_item|
        next if active_item.dot!=0
        next if active_item.rhs[active_item.dot].class!=NT
        if active_item.rhs[active_item.dot].symbol == s
          new_item = Item.new active_item, i, j, active_item.dot+1
          new_symbols << new_item.lhs.symbol if !new_symbols.include? new_item.lhs.symbol
          passive_chart.add new_item, i, j if new_item.dot==new_item.rhs.size
        end
      }
    }

  }
end

def main
  STDERR.write "> reading input from TODO\n"
  input = 'ich sah ein kleines haus'.split
  #input = 'lebensmittel schuld an europäischer inflation'.split
  #input = 'offizielle prognosen sind von nur 3 prozent ausgegangen , meldete bloomberg .'.split
  n = input.size

  STDERR.write "> reading grammar\n"
  grammar = Grammar.new 'example/grammarx'
  STDERR.write ">> adding glue grammar\n"
  #grammar.add_glue_rules
  STDERR.write ">> adding pass-through grammar\n"
  #grammar.add_pass_through_rules input

  STDERR.write "> initializing charts\n"
  passive_chart = Chart.new n
  active_chart = Chart.new n
  init input, n, active_chart, passive_chart, grammar

  STDERR.write "> parsing\n"
  parse input, n, active_chart, passive_chart, grammar

  puts "\n---\npassive chart"
  visit(1, 0, 5) { |i,j| puts "#{i},#{j}"; passive_chart.at(i,j).each { |item| puts ' '+item.to_s }; puts }
end


main