From f9106c8353aeaf6456a3eb0ea0c1e429f5de7650 Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Wed, 18 Jun 2014 16:19:32 +0200 Subject: integration --- test/hg.rb | 29 ----------------------------- test/parse.rb | 47 ----------------------------------------------- test/test_hg.rb | 30 ++++++++++++++++++++++++++++++ test/test_parse.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 76 deletions(-) delete mode 100755 test/hg.rb delete mode 100755 test/parse.rb create mode 100755 test/test_hg.rb create mode 100755 test/test_parse.rb (limited to 'test') diff --git a/test/hg.rb b/test/hg.rb deleted file mode 100755 index 16e6b6f..0000000 --- a/test/hg.rb +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env ruby - -require_relative '../hg' - - -def main - # viterbi - semiring = ViterbiSemiring.new - hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/toy/toy.json', semiring, true) - #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/glue/glue.json', semiring, true) - #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/3/3.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 Math.log score - puts - - # all paths - hypergraph.reset - paths = HG::all_paths hypergraph, nodes_by_id[-1] - paths.each_with_index { |p,i| - s = HG::derive p, p.last.head, [] - puts "#{i+1}. #{s.map { |x| x.word }.join ' '}" - } -end - - -main - diff --git a/test/parse.rb b/test/parse.rb deleted file mode 100755 index e139ea4..0000000 --- a/test/parse.rb +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env ruby - -require_relative '../parse' - - -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/toy/grammar' - #grammar = Grammar::Grammar.new '../example/glue/grammar' - #grammar = Grammar::Grammar.new '../example/3/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 = Parse::Chart.new n - active_chart = Parse::Chart.new n - Parse::init input, n, active_chart, passive_chart, grammar - - STDERR.write "> parsing\n" - Parse::parse input, n, active_chart, passive_chart, grammar - - puts "\n---\npassive chart" - Parse::visit(1, 0, 5) { |i,j| puts "#{i},#{j}"; passive_chart.at(i,j).each { |item| puts " #{j} #{item.to_s}" }; puts } - - weights_file = '../example/toy/weights' - #weights_file = '../example/glue/weights' - #weights_file = '../example/3/weights.init' - weights = SparseVector.from_kv(ReadFile.read(weights_file), ' ', "\n") - if !weights - weights = SparseVector.new - end - puts passive_chart.to_json weights -end - - -main - diff --git a/test/test_hg.rb b/test/test_hg.rb new file mode 100755 index 0000000..420d77f --- /dev/null +++ b/test/test_hg.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +require_relative '../hg' + + +def main + # viterbi + semiring = ViterbiSemiring.new + hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/toy/toy.json', semiring, true) + #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/toy/toy-test.json', semiring, true) + #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/glue/glue.json', semiring, true) + #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/3/3.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 Math.log score + puts + + # all paths + hypergraph.reset + paths = HG::all_paths hypergraph, nodes_by_id[-1] + paths.each_with_index { |p,i| + s = HG::derive p, p.last.head, [] + puts "#{i+1}. #{s.map { |x| x.word }.join ' '}" + } +end + + +main + diff --git a/test/test_parse.rb b/test/test_parse.rb new file mode 100755 index 0000000..c3be5ae --- /dev/null +++ b/test/test_parse.rb @@ -0,0 +1,49 @@ +#!/usr/bin/env ruby + +require_relative '../parse' + + +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/toy/grammar' + #grammar = Grammar::Grammar.new '../example/toy/grammar-test' + #grammar = Grammar::Grammar.new '../example/glue/grammar' + #grammar = Grammar::Grammar.new '../example/3/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 = Parse::Chart.new n + active_chart = Parse::Chart.new n + Parse::init input, n, active_chart, passive_chart, grammar + + STDERR.write "> parsing\n" + Parse::parse input, n, active_chart, passive_chart, grammar + + puts "\n---\npassive chart" + Parse::visit(1, 0, 5) { |i,j| puts "#{i},#{j}"; passive_chart.at(i,j).each { |item| puts " #{j} #{item.to_s}" }; puts } + + weights_file = '../example/toy/weights' + #weights_file = '../example/glue/weights' + #weights_file = '../example/3/weights.init' + weights = SparseVector.from_kv(ReadFile.read(weights_file), ' ', "\n") + if !weights + weights = SparseVector.new + end + + puts passive_chart.to_hg.to_json weights +end + + +main + -- cgit v1.2.3