1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
require 'zipf'
require_relative 'grammar'
module HG
class HG::Node
attr_accessor :id, :symbol, :left, :right, :outgoing, :incoming, :score
def initialize id=nil, symbol='', span=[-1,-1], outgoing=[], incoming=[], score=nil
@id = id
@symbol = symbol
@left = span[0]
@right = span[1]
@outgoing = outgoing
@incoming = incoming
@score = score
end
def to_s
"Node<id=#{@id}, symbol='#{symbol}', span=(#{@left},#{@right}), outgoing:#{@outgoing.size}, incoming:#{@incoming.size}>"
end
end
class HG::Hypergraph
attr_accessor :nodes, :edges, :nodes_by_id
def initialize nodes=[], edges=[], nodes_by_id={}
@nodes = nodes
@edges = edges
@nodes_by_id = nodes_by_id
@arity_ = nil
end
def arity
@arity_ = @edges.map { |e| e.arity }.max if !@arity_
return @arity_
end
def reset
@edges.each { |e| e.mark = 0 }
end
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
attr_accessor :head, :tails, :score, :f, :mark, :rule
def initialize head=Node.new, tails=[], score=0.0, f=SparseVector.new, rule=nil
@head = head
@tails = tails
@score = score
@f = f
@mark = 0
@rule = (rule.class==String ? Grammar::Rule.from_s(rule) : rule)
end
def arity
return @tails.size
end
def marked?
arity == @mark
end
def to_s
"Hyperedge<head=#{@head.id}, rule:'#{@rule.to_s}', tails=#{@tails.map{|n|n.id}}, arity=#{arity}, score=#{@score}, f=#{f.to_s}, mark=#{@mark}>"
end
end
def HG::topological_sort nodes
sorted = []
s = nodes.reject { |n| !n.incoming.empty? }
while !s.empty?
sorted << s.shift
sorted.last.outgoing.each { |e|
next if e.marked?
e.mark += 1
s << e.head if e.head.incoming.reject{ |f| f.marked? }.empty?
}
end
return sorted
end
def HG::init nodes, semiring, root
nodes.each { |n| n.score=semiring.null }
root.score = semiring.one
end
def HG::viterbi hypergraph, root, semiring=ViterbiSemiring.new
toposorted = topological_sort hypergraph.nodes
init toposorted, semiring, root
toposorted.each { |n|
n.incoming.each { |e|
s = semiring.one
e.tails.each { |m|
s = semiring.multiply.call(s, m.score)
}
n.score = semiring.add.call(n.score, semiring.multiply.call(s, e.score))
}
}
end
def HG::viterbi_path hypergraph, root, semiring=ViterbiSemiring.new
toposorted = topological_sort hypergraph.nodes
init toposorted, semiring, root
best_path = []
toposorted.each { |n|
best_edge = nil
n.incoming.each { |e|
s = semiring.one
e.tails.each { |m|
s = semiring.multiply.call(s, m.score)
}
if n.score < semiring.multiply.call(s, e.score) # ViterbiSemiring add
best_edge = e
end
n.score = semiring.add.call(n.score, semiring.multiply.call(s, e.score))
}
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
def HG::k_best hypergraph, root, semiring=nil
#TODO
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::derive path, cur, carry
edge = path.select { |e| e.head.symbol==cur.symbol \
&& e.head.left==cur.left \
&& e.head.right==cur.right }.first
j = 0
edge.rule.target.each { |i|
if i.class == Grammar::NT
derive path, edge.tails[edge.rule.map[j]], carry
j += 1
else
carry << i
end
}
return carry
end
def HG::read_hypergraph_from_json fn, semiring=RealSemiring.new, log_weights=false, new_obj_model=nil
nodes = []
edges = []
nodes_by_id = {}
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']
nodes << n
nodes_by_id[n.id] = n
}
h['edges'].each { |x|
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']) : SparseVector.new), \
x['rule'])
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)
}
else
e.score = w.dot(e.f)
end
end
e.tails.each { |m|
m.outgoing << e
}
e.head.incoming << e
edges << e
}
return Hypergraph.new(nodes, edges), nodes_by_id
end
end #module
|