From f9106c8353aeaf6456a3eb0ea0c1e429f5de7650 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 18 Jun 2014 16:19:32 +0200 Subject: integration --- hg.rb | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'hg.rb') diff --git a/hg.rb b/hg.rb index e252ced..1f17a66 100644 --- a/hg.rb +++ b/hg.rb @@ -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" 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 -- cgit v1.2.3