summaryrefslogtreecommitdiff
path: root/hg.rb
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-04 20:06:53 +0200
committerPatrick Simianer <p@simianer.de>2014-06-04 20:06:53 +0200
commitdbc15de7a63b939d7c3c51c39b34286aed56739f (patch)
treeba8a8150aa9b95bf18064dd4e3b728fed44b7614 /hg.rb
parent6f1ed883692e7f9e082cadb92673e1eeed0cdc27 (diff)
fix all_paths, better json handling
Diffstat (limited to 'hg.rb')
-rw-r--r--hg.rb76
1 files changed, 40 insertions, 36 deletions
diff --git a/hg.rb b/hg.rb
index c7ee6ba..455a22b 100644
--- a/hg.rb
+++ b/hg.rb
@@ -9,10 +9,10 @@ module HG
class HG::Node
- attr_accessor :label, :cat, :outgoing, :incoming, :score
+ attr_accessor :id, :cat, :outgoing, :incoming, :score
- def initialize label=nil, cat=nil, outgoing=[], incoming=[], score=nil
- @label = label
+ def initialize id=nil, cat=nil, outgoing=[], incoming=[], score=nil
+ @id = id
@cat = cat
@outgoing = outgoing
@incoming = incoming
@@ -20,7 +20,7 @@ class HG::Node
end
def to_s
- "Node<label:\"#{@label}\", cat:\"#{@cat}\", outgoing:#{@outgoing.size}, incoming:#{@incoming.size}>"
+ "Node<id:#{@id}, cat:\"#{@cat}\", outgoing:#{@outgoing.size}, incoming:#{@incoming.size}>"
end
end
@@ -36,6 +36,10 @@ class HG::Hypergraph
@edges.map { |e| e.arity }.max
end
+ def reset
+ @edges.each { |e| e.mark = 0 }
+ end
+
def to_s
"Hypergraph<nodes:[#{@nodes.to_s}], edges:[#{@edges.to_s}], arity:#{arity}>"
end
@@ -62,7 +66,7 @@ class HG::Hyperedge
end
def to_s
- "Hyperedge<head:\"#{@head.label}\", rule:\"#{@rule.to_s}, \"tails:#{@tails.map{|n|n.label}}, arity:#{arity}, weight:#{@weight}, f:#{f.to_s}, mark:#{@mark}>"
+ "Hyperedge<head:\"#{@head.id}\", rule:\"#{@rule.to_s}, \"tails:#{@tails.map{|n|n.id}}, arity:#{arity}, weight:#{@weight}, f:#{f.to_s}, mark:#{@mark}>"
end
end
@@ -141,29 +145,46 @@ def HG::viterbi_string hypergraph, root, semiring=ViterbiSemiring.new
return s, toposorted.last.score
end
+def HG::all_paths hypergraph, root
+ toposorted = topological_sort hypergraph.nodes
+ paths = [[]]
+ toposorted.each { |n|
+ next if n.incoming.empty?
+ new_paths = []
+ while !paths.empty?
+ p = paths.pop
+ n.incoming.each { |e|
+ new_paths << p+[e]
+ }
+ end
+ paths = new_paths
+ }
+ return paths
+end
+
def HG::read_hypergraph_from_json fn, semiring=RealSemiring.new, log_weights=false
nodes = []
edges = []
- nodes_by_label = {}
- nodes_by_index = []
+ nodes_by_id = {}
h = JSON.parse File.new(fn).read
w = SparseVector.from_h h['weights']
h['nodes'].each { |x|
- n = Node.new x['label'], x['cat']
+ n = Node.new x['id'], x['cat']
nodes << n
- nodes_by_label[n.label] = n
- nodes_by_index << n
+ nodes_by_id[n.id] = n
}
h['edges'].each { |x|
- e = Hyperedge.new(nodes_by_label[x['head']], \
- x['tails'].map { |j| nodes_by_label[j] }.to_a, \
- semiring.convert.call(x['weight'].to_f), \
- SparseVector.from_h(x['f']), \
+ e = Hyperedge.new(nodes_by_id[x['head']], \
+ x['tails'].map { |j| nodes_by_id[j] }.to_a, \
+ (x['weight'] ? semiring.convert.call(x['weight'].to_f) : nil), \
+ (x['f'] ? SparseVector.from_h(x['f']) : nil), \
x['rule'])
- if log_weights
- e.weight = Math.exp(w.dot(e.f))
- else
- e.weight = w.dot(e.f)
+ if x['f']
+ if log_weights
+ e.weight = Math.exp(w.dot(e.f))
+ else
+ e.weight = w.dot(e.f)
+ end
end
e.tails.each { |m|
m.outgoing << e
@@ -171,24 +192,7 @@ def HG::read_hypergraph_from_json fn, semiring=RealSemiring.new, log_weights=fal
e.head.incoming << e
edges << e
}
- return Hypergraph.new(nodes, edges), nodes_by_label, nodes_by_index
-end
-
-def HG::all_paths hypergraph, root, semiring=ViterbiSemiring.new #FIXME?
- toposorted = topological_sort hypergraph.nodes
- paths = [[]]
- toposorted.each { |n|
- next if n.incoming.empty?
- new_paths = []
- while !paths.empty?
- p = paths.pop
- n.incoming.each { |e|
- new_paths << p+[e]
- }
- end
- paths = new_paths
- }
- return paths
+ return Hypergraph.new(nodes, edges), nodes_by_id
end
def HG::derive path, cur, carry