diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-18 08:28:36 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-18 08:28:36 +0200 |
commit | d92209e3ec223a380c7ad540622caa15712c490c (patch) | |
tree | 91f985fc2d9726a4d1e9f7c86e17cbf7dd2b6ee6 /test_parse.rb | |
parent | ae3bc3e1870d009c4d54c55416ebb4b7c07a483f (diff) |
better integration, chart.to_json
Diffstat (limited to 'test_parse.rb')
-rwxr-xr-x | test_parse.rb | 58 |
1 files changed, 15 insertions, 43 deletions
diff --git a/test_parse.rb b/test_parse.rb index d184892..835b08a 100755 --- a/test_parse.rb +++ b/test_parse.rb @@ -3,45 +3,6 @@ require_relative 'parse' -def chart2json passive_chart, n - id = 0 - spancat2id = {} - puts "{" - puts "\"weights\":{ \"logp\":2.0, \"use_shell\":1.0 }," - puts "\"nodes\":" - puts "[" - # nodes - visit(1, 0, n) { |i,j| - seen = {} - passive_chart.at(i,j).each { |item| - if !seen[item.lhs.symbol] - puts "{ \"id\":#{id}, \"cat\":\"#{item.lhs.symbol}\", \"span\":[#{i},#{j}] }," - spancat2id["#{item.lhs.symbol},#{i},#{j}"] = id - id += 1 - end - seen[item.lhs.symbol]=true - } - } - puts "{ \"id\":-1, \"cat\":\"root\", \"span\":[-1, -1] }" - puts "]," - puts "\"edges\":" - puts "[" - a = [] - visit(1, 0, n) { |i,j| - passive_chart.at(i,j).each { |item| - if item.tail_spans.empty? - a << "{ \"head\":#{spancat2id[item.lhs.symbol+','+i.to_s+','+j.to_s]}, \"rule\":\"[#{item.lhs.symbol}] ||| #{item.rhs.map{|x|(x.class==Grammar::NT ? '['+x.symbol+','+x.index.to_s+']' : x.word.gsub('"','\"'))}.join(' ')} ||| #{item.target.map{|x|(x.class==Grammar::NT ? '['+x.symbol+','+(x.index+0).to_s+']' : x.word.gsub('"', '\"'))}.join(' ')}\ |||\", \"tails\":[-1], \"f\":#{item.f.to_s.gsub('=>',':')} }" - else - a << "{ \"head\":#{spancat2id[item.lhs.symbol+','+i.to_s+','+j.to_s]}, \"rule\":\"[#{item.lhs.symbol}] ||| #{item.rhs.map{|x|(x.class==Grammar::NT ? '['+x.symbol+','+x.index.to_s+']' : x.word.gsub('"','\"'))}.join(' ')} ||| #{item.target.map{|x|(x.class==Grammar::NT ? '['+x.symbol+','+(x.index+1).to_s+']' : x.word.gsub('"', '\"'))}.join(' ')}\ |||\", \"tails\":[#{item.rhs.zip((0..item.rhs.size-1).map{|q|item.tail_spans[q]}).select{|x|x[0].class==Grammar::NT}.map{|x|spancat2id[x[0].symbol+','+x[1].left.to_s+','+x[1].right.to_s]}.join ', '}], \"f\":#{item.f.to_s.gsub('=>',':')} }" - end - } - } - puts a.join ",\n" - puts "]" - puts "}" -end - - def main STDERR.write "> reading input from TODO\n" #input = 'ich sah ein kleines haus'.split @@ -50,9 +11,13 @@ def main 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 + #grammar.add_glue_rules + STDERR.write ">> adding pass-through grammar\n" #grammar.add_pass_through_rules input @@ -65,9 +30,16 @@ def main Parse::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 " #{j} #{item.to_s}" }; puts } - - chart2json passive_chart, n + #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 |