diff options
Diffstat (limited to 'prototype')
| -rw-r--r-- | prototype/grammar.rb | 4 | ||||
| -rw-r--r-- | prototype/hypergraph.rb | 20 | ||||
| -rwxr-xr-x | prototype/ow_proto.rb | 6 | ||||
| -rw-r--r-- | prototype/parse.rb | 31 |
4 files changed, 34 insertions, 27 deletions
diff --git a/prototype/grammar.rb b/prototype/grammar.rb index abccb15..4aebd95 100644 --- a/prototype/grammar.rb +++ b/prototype/grammar.rb @@ -120,9 +120,9 @@ class Grammar @rules.map { |r| r.lhs.symbol }.select { |s| s != 'S' }.uniq.each { |symbol| @rules << Rule.new(NT.new('S'), [NT.new(symbol, 0)], [NT.new(symbol, 0)], [0]) @start_nt << @rules.last - @rules << Rule.new(NT.new('S'), [NT.new('S', 0), NT.new('X', 1)], [NT.new('S', 0), NT.new('X', 1)], [0, 1]) - @start_nt << @rules.last } + @rules << Rule.new(NT.new('S'), [NT.new('S', 0), NT.new('X', 1)], [NT.new('S', 0), NT.new('X', 1)], [0, 1]) + @start_nt << @rules.last end def add_pass_through_rules a diff --git a/prototype/hypergraph.rb b/prototype/hypergraph.rb index fd72393..08d1a29 100644 --- a/prototype/hypergraph.rb +++ b/prototype/hypergraph.rb @@ -189,18 +189,30 @@ def HG::all_paths hypergraph, root paths = new_paths } - return paths + seen = Set.new + paths.select { |p| + reachable = Set.new + mark_reachable p, p.last, reachable + key = reachable.map(&:object_id).sort + !seen.include?(key) && seen.add(key) + } +end + +def HG::mark_reachable path, edge, used + used << edge + edge.tails.each { |t| + child = path.find { |e| e.head == t } + mark_reachable path, child, used if child && !used.include?(child) + } end def HG::derive path, cur, carry edge = path.select { |e| e.head.symbol==cur.symbol \ && e.head.left==cur.left \ && e.head.right==cur.right }.first - j = 0 edge.rule.target.each { |i| if i.class == Grammar::NT - derive path, edge.tails[edge.rule.map[j]], carry - j += 1 + derive path, edge.tails[edge.rule.map.index(i.index)], carry else carry << i end diff --git a/prototype/ow_proto.rb b/prototype/ow_proto.rb index 912090b..41fe683 100755 --- a/prototype/ow_proto.rb +++ b/prototype/ow_proto.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby -require 'trollop' -require 'xmlsimple' +require 'optimist' +begin; require 'xmlsimple'; rescue LoadError; end require_relative 'parse' def read_grammar fn, add_glue, add_pass_through, input=nil @@ -19,7 +19,7 @@ def read_grammar fn, add_glue, add_pass_through, input=nil end def main - cfg = Trollop::options do + cfg = Optimist::options do opt :input, "", :type => :string, :default => '-', :short => '-i' opt :grammar, "", :type => :string, :required => true, :short => '-g' opt :weights, "", :type => :string, :required => true, :short => '-w' diff --git a/prototype/parse.rb b/prototype/parse.rb index adf2b91..1741030 100644 --- a/prototype/parse.rb +++ b/prototype/parse.rb @@ -90,14 +90,10 @@ class Item < Grammar::Rule rule_or_item.rhs.each_with_index { |x,i| # duplicate rhs partially @rhs << x if x.class == Grammar::NT - begin - if i >= dot - @tail_spans[i] = Span.new(-1, -1) - else - @tail_spans[i] = rule_or_item.tail_spans[i].dup - end - rescue + if i >= dot || !rule_or_item.is_a?(Item) @tail_spans[i] = Span.new(-1, -1) + else + @tail_spans[i] = rule_or_item.tail_spans[i].dup end end } @@ -156,7 +152,7 @@ def Parse::parse input, n, active_chart, passive_chart, grammar while !active_chart.at(i,j).empty? active_item = active_chart.at(i,j).pop advanced = false - visit(1, i, j, 1) { |k,l| + visit(1, i, j) { |k,l| if passive_chart.has active_item.rhs[active_item.dot].symbol, k, l if k == active_item.right new_item = Item.new active_item, active_item.left, l, active_item.dot+1 @@ -186,16 +182,15 @@ def Parse::parse input, n, active_chart, passive_chart, grammar # 'self-filling' step new_symbols.each { |s| - remaining_items.each { |item| - next if item.dot!=0 - next if item.rhs[item.dot].class!=Grammar::NT - if item.rhs[item.dot].symbol == s - new_item = Item.new item, i, j, item.dot+1 - new_item.tail_spans[new_item.dot-1] = Span.new(i,j) - if new_item.dot==new_item.rhs.size - new_symbols << new_item.lhs.symbol if !new_symbols.include? new_item.lhs.symbol - passive_chart.add new_item, i, j - end + grammar.start_nt.each { |r| + next if r.rhs.size > j-i + next if r.rhs.first.class!=Grammar::NT + next if r.rhs.first.symbol != s + new_item = Item.new r, i, j, 1 + new_item.tail_spans[0] = Span.new(i,j) + if new_item.dot==new_item.rhs.size + new_symbols << new_item.lhs.symbol if !new_symbols.include? new_item.lhs.symbol + passive_chart.add new_item, i, j end } } |
