From adf10dca9353bd9fa443eec67a30e2bbe58cbff4 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Mon, 16 Nov 2015 17:21:40 +0100 Subject: new obj. --- prototype/hypergraph.rb | 39 +++++++++++++++++++++++++++++++++++++-- prototype/new_obj.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100755 prototype/new_obj.rb diff --git a/prototype/hypergraph.rb b/prototype/hypergraph.rb index d43d5ee..a628c71 100644 --- a/prototype/hypergraph.rb +++ b/prototype/hypergraph.rb @@ -142,6 +142,31 @@ def HG::viterbi_path hypergraph, root, semiring=ViterbiSemiring.new } best_path << best_edge if best_edge } + + return best_path, toposorted.last.score +end + +def HG::new_obj_path hypergraph, root, semiring=ViterbiSemiring.new + toposorted = topological_sort hypergraph.nodes + hypergraph.nodes.each { |n| n.score=1.0/0 } + root.score = 1 + best_path = [] + toposorted.each { |n| + best_edge = n.incoming.first + n.incoming.each { |e| + s = 0 + e.tails.each { |m| + s += m.score + } + s *= e.score + if n.score >= s + best_edge = e + end + n.score = [s, n.score].min + } + best_path << best_edge if best_edge + } + return best_path, toposorted.last.score end @@ -163,6 +188,7 @@ def HG::all_paths hypergraph, root end paths = new_paths } + return paths end @@ -182,11 +208,12 @@ def HG::derive path, cur, carry return carry end -def HG::read_hypergraph_from_json fn, semiring=RealSemiring.new, log_weights=false +def HG::read_hypergraph_from_json fn, semiring=RealSemiring.new, log_weights=false, new_obj_model=nil nodes = [] edges = [] nodes_by_id = {} - h = JSON.parse File.new(fn).read + f = ReadFile.new fn + h = JSON.parse f.read w = SparseVector.from_h h['weights'] h['nodes'].each { |x| n = Node.new x['id'], x['symbol'], x['span'] @@ -202,6 +229,14 @@ def HG::read_hypergraph_from_json fn, semiring=RealSemiring.new, log_weights=fal if x['f'] if log_weights e.score = Math.exp(w.dot(e.f)) + elsif new_obj_model + e.score = e.f.dot(e.f) + new_obj_model.each { |x| + wx = x[0] + wf = x[1] + e.score -= wx*wf.dot(e.f) + } + puts e.score else e.score = w.dot(e.f) end diff --git a/prototype/new_obj.rb b/prototype/new_obj.rb new file mode 100755 index 0000000..3380fd0 --- /dev/null +++ b/prototype/new_obj.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require_relative 'hypergraph' + +class NewObjSemiring < Semiring + def initialize + @add = Proc.new { |a,b| [a,b].min } + @multiply = Proc.new { |a,b| a*b } + @one = 1.0 + @null = 0.0 + @convert = Proc.new { |v| v } + end +end + +def main + #semiring = NewObjSemiring.new + semiring = ViterbiSemiring.new + #h = { "logp" => 0, "use_house" => 100, "use_small" => 100 } + #model = [[1, SparseVector.from_h(h)]] + hypergraph, nodes_by_id = HG::read_hypergraph_from_json('739.json.gz', semiring, true) + path, score = HG::viterbi_path hypergraph, nodes_by_id[-1], semiring + path.each { |e| puts "#{e.head.to_s}\t#{e.rule.to_s}" } + puts path.size + puts "---" + s = HG::derive path, path.last.head, [] + puts s.size + puts "#{s.map { |i| i.word }.join ' '}" + puts Math.log(score) + puts + +end + +main + -- cgit v1.2.3