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 /parse.rb | |
parent | ae3bc3e1870d009c4d54c55416ebb4b7c07a483f (diff) |
better integration, chart.to_json
Diffstat (limited to 'parse.rb')
-rw-r--r-- | parse.rb | 45 |
1 files changed, 43 insertions, 2 deletions
@@ -16,6 +16,7 @@ end class Chart def initialize n + @n = n @m = [] (n+1).times { a = [] @@ -38,8 +39,48 @@ class Chart return @b["#{i},#{j},#{symbol}"] end - def to_json - #TODO + def to_json weights=nil + id = 0 + json_s = "{\n" + json_s += "\"weights\":#{weights.to_json},\n" + json_s += "\"nodes\":\n" + json_s += "[\n" + seen = {} + Parse::visit(1, 0, @n) { |i,j| + self.at(i,j).each { |item| + _ = "#{item.lhs.symbol},#{i},#{j}" + if !seen[_] + json_s += "{ \"id\":#{id}, \"cat\":\"#{item.lhs.symbol.to_json.slice(1..-1).chomp('"')}\", \"span\":[#{i},#{j}] },\n" + seen[_] = id + id += 1 + end + } + } + json_s += "{ \"id\":-1, \"cat\":\"root\", \"span\":[-1, -1] }\n" + json_s += "],\n" + json_s += "\"edges\":\n" + json_s += "[\n" + a = [] + Parse::visit(1, 0, @n) { |i,j| + self.at(i,j).each { |item| + _ = '{ ' + _ += "\"head\":#{seen[item.lhs.symbol+','+i.to_s+','+j.to_s]}, " + _ += "\"rule\":\"[#{item.lhs.symbol.to_json.slice(1..-1).chomp('"')}] ||| " + _ += "#{item.rhs.map{|x| (x.class==Grammar::NT ? '['+x.symbol.to_json.slice(1..-1).chomp('"')+','+x.index.to_s+']' : x.word.to_json).slice(1..-1).chomp('"') }.join(' ')} ||| #{item.target.map{|x| (x.class==Grammar::NT ? '['+x.symbol.to_json.slice(1..-1).chomp('"')+','+(x.index+1).to_s+']' : x.word.to_json.slice(1..-1).chomp('"')) }.join(' ')}\ |||\", " + if item.tail_spans.empty? + _ += "\"tails\":[-1], " + else + _ += "\"tails\":[#{item.rhs.zip((0..item.rhs.size-1).map{|q| item.tail_spans[q] }).select{|x| x[0].class==Grammar::NT }.map{|x| seen[x[0].symbol.to_json.slice(1..-1).chomp('"')+','+x[1].left.to_s+','+x[1].right.to_s]}.join ', '}], " + end + _ += "\"f\":#{(item.f ? item.f.to_json : '{}')} }" + a << _ + } + } + json_s += a.join ",\n" + json_s += "\n]\n" + json_s += "}\n" + + return json_s end end |