diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-18 16:19:32 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-18 16:19:32 +0200 |
commit | f9106c8353aeaf6456a3eb0ea0c1e429f5de7650 (patch) | |
tree | 9b4c2baad153d189821633ef6649647d0992a1fa /hg.rb | |
parent | 33fda7f79c02b8ef152b88a11f3810c9a25a7381 (diff) |
integration
Diffstat (limited to 'hg.rb')
-rw-r--r-- | hg.rb | 34 |
1 files changed, 28 insertions, 6 deletions
@@ -24,12 +24,13 @@ class HG::Node end class HG::Hypergraph - attr_accessor :nodes, :edges + attr_accessor :nodes, :edges, :nodes_by_id - def initialize nodes=[], edges=[] - @nodes = nodes - @edges = edges - @arity_ = nil + def initialize nodes=[], edges=[], nodes_by_id={} + @nodes = nodes + @edges = edges + @nodes_by_id = nodes_by_id + @arity_ = nil end def arity @@ -44,6 +45,27 @@ class HG::Hypergraph def to_s "Hypergraph<nodes:#{@nodes.size}, edges:#{@edges.size}, arity=#{arity}>" end + + def to_json weights=nil + json_s = "{\n" + json_s += "\"weights\":#{(weights ? weights.to_json : '{}')},\n" + json_s += "\"nodes\":\n" + json_s += "[\n" + json_s += @nodes.map { |n| + "{ \"id\":#{n.id}, \"cat\":\"#{n.symbol.to_json.slice(1..-1).chomp('"')}\", \"span\":[#{n.left},#{n.right}] }" + }.join ",\n" + json_s += "\n],\n" + json_s += "\"edges\":\n" + json_s += "[\n" + json_s += @edges.map { |e| + "{ \"head\":#{e.head.id}, \"rule\":#{e.rule.to_json}, \"tails\":#{e.tails.map{ |n| n.id }}, \"f\":#{(e.f ? e.f.to_json : '{}')} }" + }.join ",\n" + json_s += "\n]\n" + json_s += "}\n" + + return json_s + + end end class HG::Hyperedge @@ -177,7 +199,7 @@ def HG::read_hypergraph_from_json fn, semiring=RealSemiring.new, log_weights=fal e = Hyperedge.new(nodes_by_id[x['head']], \ x['tails'].map { |j| nodes_by_id[j] }.to_a, \ (x['score'] ? semiring.convert.call(x['score'].to_f) : nil), \ - (x['f'] ? SparseVector.from_h(x['f']) : nil), \ + (x['f'] ? SparseVector.from_h(x['f']) : SparseVector.new), \ x['rule']) if x['f'] if log_weights |