From 1298b37784247d6de486814838b07ddcf69cd970 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 7 Jun 2014 12:31:02 +0200 Subject: fix reordering --- example/json/test.json | 12 ++++++------ hg.rb | 2 +- parse.rb | 32 ++------------------------------ test.rb | 19 ------------------- test_hg.rb | 19 +++++++++++++++++++ 5 files changed, 28 insertions(+), 56 deletions(-) delete mode 100755 test.rb create mode 100755 test_hg.rb diff --git a/example/json/test.json b/example/json/test.json index d865fa0..c58c249 100644 --- a/example/json/test.json +++ b/example/json/test.json @@ -22,12 +22,12 @@ {"head":2, "rule":"[JJ] ||| kleines ||| little ||| logp=0.0 use_little=1.0", "tails":[ -1 ], "f":{"logp":0.0, "use_little":1.0}, "weight":1.0 }, {"head":3, "rule":"[NN] ||| kleines haus ||| small house ||| logp=0.0 use_house=1.0", "tails":[ -1 ], "f":{"logp":0.0, "use_house":1.0}, "weight":1.0 }, {"head":3, "rule":"[NN] ||| kleines haus ||| little house ||| logp=0.0 use_house=1.0", "tails":[ -1 ], "f":{"logp":0.0, "use_house":1.0}, "weight":1.0 }, -{"head":3, "rule":"[NN] ||| [JJ,1] haus ||| [1] house ||| logp=0.0 use_house=1.0", "tails":[ 2 ], "f":{"logp":0.0, "use_house":1.0}, "weight":1.0 }, -{"head":3, "rule":"[NN] ||| [JJ,1] haus ||| [1] shell ||| logp=0.0 use_shell=1.0", "tails":[ 2 ], "f":{"logp":0.0, "use_shell":1.0}, "weight":2.71828182846 }, -{"head":4, "rule":"[NP] ||| ein [NN,1] ||| a [1] ||| logp=0.0 use_a=1.0", "tails":[ 3 ], "f":{"logp":0.0, "use_a":1.0}, "weight":1.0 }, -{"head":5, "rule":"[VP] ||| [V,1] [NP,2] ||| [1] [2] ||| logp=0.0", "tails":[ 1,4 ], "f":{"logp":0.0}, "weight":1.0 }, -{"head":6, "rule":"[S] ||| [NP,1] [VP,2] ||| [1] [2] ||| logp=0.0", "tails":[ 0,5 ], "f":{"logp":0.0}, "weight":1.0 }, -{"head":7, "rule":"[Goal] ||| [S,1] ||| [1] ||| ", "tails":[ 6 ], "f":{}, "weight":1.0 } +{"head":3, "rule":"[NN] ||| [JJ,1] haus ||| [JJ,1] house ||| logp=0.0 use_house=1.0", "tails":[ 2 ], "f":{"logp":0.0, "use_house":1.0}, "weight":1.0 }, +{"head":3, "rule":"[NN] ||| [JJ,1] haus ||| [JJ,1] shell ||| logp=0.0 use_shell=1.0", "tails":[ 2 ], "f":{"logp":0.0, "use_shell":1.0}, "weight":2.71828182846 }, +{"head":4, "rule":"[NP] ||| ein [NN,1] ||| a [NN,1] ||| logp=0.0 use_a=1.0", "tails":[ 3 ], "f":{"logp":0.0, "use_a":1.0}, "weight":1.0 }, +{"head":5, "rule":"[VP] ||| [V,1] [NP,2] ||| [V,1] [NP,2] ||| logp=0.0", "tails":[ 1,4 ], "f":{"logp":0.0}, "weight":1.0 }, +{"head":6, "rule":"[S] ||| [NP,1] [VP,2] ||| [VP,1] [NP,2] ||| logp=0.0", "tails":[ 0,5 ], "f":{"logp":0.0}, "weight":1.0 }, +{"head":7, "rule":"[Goal] ||| [S,1] ||| [S,1] ||| ", "tails":[ 6 ], "f":{}, "weight":1.0 } ] } diff --git a/hg.rb b/hg.rb index 43dbf79..b73d860 100644 --- a/hg.rb +++ b/hg.rb @@ -151,7 +151,7 @@ def HG::derive path, cur, carry j = 0 edge.rule.target.each { |i| if i.class == Grammar::NT - derive path, edge.tails[j], carry + derive path, edge.tails[edge.rule.map[j]], carry j += 1 else carry << i diff --git a/parse.rb b/parse.rb index 6adf2c0..24b98c6 100644 --- a/parse.rb +++ b/parse.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require 'nlp_ruby' +require_relative 'grammar' class Chart @@ -30,7 +31,7 @@ class Chart end class Item < Grammar::Rule - attr_accessor :lhs, :rhs, :dot, :e + attr_accessor :dot def initialize rule_or_item, left, right, dot @lhs = Grammar::NT.new rule_or_item.lhs.symbol @@ -154,32 +155,3 @@ def parse input, n, active_chart, passive_chart, grammar } 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::Grammar.new 'example/grammars/grammar.3.gz' - 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 - diff --git a/test.rb b/test.rb deleted file mode 100755 index e65c9b3..0000000 --- a/test.rb +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env ruby - -require_relative 'hg' - - -semiring = ViterbiSemiring.new -hypergraph, nodes_by_id = HG::read_hypergraph_from_json('example/json/test.json', semiring, true) -path, score = HG::viterbi_path hypergraph, nodes_by_id[-1], semiring -s = HG::derive path, path.last.head, [] -puts "#{s.map { |i| i.word }.join ' '}" -puts - -hypergraph.reset -paths = HG::all_paths hypergraph, nodes_by_id[-1] -paths.each { |p| - s = HG::derive p, p.last.head, [] - puts "#{s.map { |i| i.word }.join ' '}" -} - diff --git a/test_hg.rb b/test_hg.rb new file mode 100755 index 0000000..e65c9b3 --- /dev/null +++ b/test_hg.rb @@ -0,0 +1,19 @@ +#!/usr/bin/env ruby + +require_relative 'hg' + + +semiring = ViterbiSemiring.new +hypergraph, nodes_by_id = HG::read_hypergraph_from_json('example/json/test.json', semiring, true) +path, score = HG::viterbi_path hypergraph, nodes_by_id[-1], semiring +s = HG::derive path, path.last.head, [] +puts "#{s.map { |i| i.word }.join ' '}" +puts + +hypergraph.reset +paths = HG::all_paths hypergraph, nodes_by_id[-1] +paths.each { |p| + s = HG::derive p, p.last.head, [] + puts "#{s.map { |i| i.word }.join ' '}" +} + -- cgit v1.2.3