1 | #!/usr/bin/ruby1.9.1 -I /usr/local/lib/utt -I $HOME/.local/lib/utt |
---|
2 | # -*- coding: iso-8859-2 -*- |
---|
3 | |
---|
4 | $: << "#{ENV['HOME']}/.local/lib/utt" |
---|
5 | $: << "/usr/local/lib/utt" |
---|
6 | |
---|
7 | require 'getoptlong' |
---|
8 | require 'seg.rb' |
---|
9 | |
---|
10 | opts = GetoptLong.new( |
---|
11 | [ '--help', '-h', GetoptLong::NO_ARGUMENT ], |
---|
12 | [ '--debug', '-d', GetoptLong::NO_ARGUMENT ], |
---|
13 | [ '--format', '-F', GetoptLong::REQUIRED_ARGUMENT ], |
---|
14 | [ '--info', '-I', GetoptLong::REQUIRED_ARGUMENT ], |
---|
15 | [ '--span', '-s', GetoptLong::REQUIRED_ARGUMENT ], |
---|
16 | [ '--maxsize', GetoptLong::REQUIRED_ARGUMENT ], |
---|
17 | [ '--forest', GetoptLong::NO_ARGUMENT ], |
---|
18 | [ '--ground', GetoptLong::NO_ARGUMENT ], |
---|
19 | [ '--only-trees','-t', GetoptLong::NO_ARGUMENT ]) |
---|
20 | |
---|
21 | $helptext= |
---|
22 | "The program generates trees from the graph output by dgp. dgp must\n"+ |
---|
23 | "must be run with '-i ds' option.\n\n"+ |
---|
24 | "Command: tre [options]\n\n"+ |
---|
25 | "Options:\n"+ |
---|
26 | "--help -h Print help (this text) and exit.\n"+ |
---|
27 | "--debug -d Verbose output. For developers only.\n"+ |
---|
28 | "--format=s -F s Output format. Recognized values:\n"+ |
---|
29 | " a root + list of arcs\n"+ |
---|
30 | " p parenthesized notation\n"+ |
---|
31 | " h human readable indented tree format\n"+ |
---|
32 | " Multiple values are allowed. (default p)\n"+ |
---|
33 | "--info=s -I s Information printed. Recognized values:\n"+ |
---|
34 | " n node identifier\n"+ |
---|
35 | " f surface form\n"+ |
---|
36 | " m morphological information\n"+ |
---|
37 | " l arc labels\n"+ |
---|
38 | "--only-trees -t Do not copy input. Print trees only.\n" |
---|
39 | |
---|
40 | $DEBUG=false |
---|
41 | $FORMAT='p' |
---|
42 | $INFO='DEFAULT' |
---|
43 | $ONLYTREES=false |
---|
44 | $START=nil |
---|
45 | $END=nil |
---|
46 | $FOREST=false |
---|
47 | $MAXSIZE=nil |
---|
48 | |
---|
49 | opts.each do |opt, arg| |
---|
50 | case opt |
---|
51 | when '--help' |
---|
52 | print $helptext |
---|
53 | exit 0 |
---|
54 | when '--debug' |
---|
55 | $DEBUG=true |
---|
56 | when '--format' |
---|
57 | $FORMAT=arg |
---|
58 | when '--info' |
---|
59 | $INFO=arg |
---|
60 | when '--only-trees' |
---|
61 | $ONLYTREES=true |
---|
62 | when '--forest' |
---|
63 | $FOREST=true |
---|
64 | when '--ground' |
---|
65 | $GROUND=true |
---|
66 | when '--maxsize' |
---|
67 | $MAXSIZE=arg.to_i |
---|
68 | when '--span' |
---|
69 | $START,$END = arg.split ',' |
---|
70 | else |
---|
71 | print "Unknown option #{opt}. Ignored.\n" |
---|
72 | end |
---|
73 | end |
---|
74 | |
---|
75 | if $INFO=='DEFAULT' |
---|
76 | case $FORMAT |
---|
77 | when 'p','a' |
---|
78 | $INFO='nl' |
---|
79 | when 'h' |
---|
80 | $INFO='fmnl' |
---|
81 | end |
---|
82 | end |
---|
83 | |
---|
84 | $dgpsep=';' |
---|
85 | |
---|
86 | def tre(input) |
---|
87 | $gphid=[] |
---|
88 | $form=[] |
---|
89 | $lem=[] |
---|
90 | $ord1=[] |
---|
91 | $count=0 |
---|
92 | nodes=[] |
---|
93 | prevpos=-1 |
---|
94 | tokennumber=0 |
---|
95 | for line in input |
---|
96 | seg=Seg.new(line) |
---|
97 | print line unless $ONLYTREES || seg.field(3) == 'EOS' |
---|
98 | |
---|
99 | if dgp=seg['dgp'] |
---|
100 | if nodes==[] && seg[3]!='BOS' |
---|
101 | print "A sentence must start with BOS segment. Aborting.\n" |
---|
102 | return |
---|
103 | end |
---|
104 | |
---|
105 | id=dgp[/^\d+/].to_i |
---|
106 | |
---|
107 | if gph=seg['gph'] |
---|
108 | $gphid[id]=gph[/^\d+/].to_i |
---|
109 | else |
---|
110 | print "No gph field. Aborting.\n" |
---|
111 | return |
---|
112 | end |
---|
113 | |
---|
114 | $form[$gphid[id]] = seg[4] |
---|
115 | $lem[$gphid[id]] = seg['lem'] |
---|
116 | $ord1[$gphid[id]] = if prevpos==seg[1].to_i then tokennumber |
---|
117 | else prevpos=seg[1].to_i; tokennumber+=1 end |
---|
118 | |
---|
119 | nodes[id] = [seg[1].to_i,seg[2].to_i,dgp] |
---|
120 | |
---|
121 | if seg[3]=='EOS' |
---|
122 | |
---|
123 | $pref = "#{seg[1]} #{seg[2]} SYN *" |
---|
124 | |
---|
125 | parsegraph(nodes) |
---|
126 | |
---|
127 | set_ord #(0...(nodes.length)).each{|i| set_distance_from_i i } |
---|
128 | |
---|
129 | printgraph if $DEBUG |
---|
130 | |
---|
131 | if $GROUND |
---|
132 | printground |
---|
133 | else |
---|
134 | thetrees = $FOREST ? genforest : gentrees |
---|
135 | |
---|
136 | output_trees thetrees |
---|
137 | |
---|
138 | print line unless $ONLYTREES |
---|
139 | |
---|
140 | $gphid=[] # POWTÓRZENIE |
---|
141 | $form=[] |
---|
142 | $lem=[] |
---|
143 | $ord1=[] |
---|
144 | $count=0 |
---|
145 | nodes=[] |
---|
146 | prevpos=-1 |
---|
147 | tokennumber=0 |
---|
148 | end |
---|
149 | end |
---|
150 | end |
---|
151 | end |
---|
152 | end |
---|
153 | |
---|
154 | |
---|
155 | def output_trees trees |
---|
156 | for t in trees |
---|
157 | $count += 1 |
---|
158 | # t1=ground(t) |
---|
159 | |
---|
160 | t1=t |
---|
161 | |
---|
162 | span = $FOREST ? " span:" + (ground_tree_min(t1).to_s + ","+ground_tree_max(t1).to_s)+";" : "" |
---|
163 | case $FORMAT |
---|
164 | when /a/ |
---|
165 | print "#{$pref} tre:#{$count}#{span} #{arcsinfo(t1[0],t1[1])}" |
---|
166 | # print arcsinfo(t1[0],t1[1]) |
---|
167 | print "\n" |
---|
168 | when /p/ |
---|
169 | print "#{$pref}#{span} tre:#{$count} par:" |
---|
170 | printpar(t1[0],t1[1]) |
---|
171 | print "\n" |
---|
172 | when /h/ |
---|
173 | print "#\n# tree #{$count}\n# ------\n" |
---|
174 | printtree_dgp(t1[0],t1[1],0) |
---|
175 | end |
---|
176 | end |
---|
177 | end |
---|
178 | |
---|
179 | |
---|
180 | def nodeinfo(id) |
---|
181 | info="" |
---|
182 | gphid = $gphid[id] |
---|
183 | if $INFO =~ /o/ |
---|
184 | info += $ord1[gphid].to_s + '/' + gphid.to_s + '/' + id.to_s |
---|
185 | info += '.' if $INFO =~ /[nfm]/ |
---|
186 | end |
---|
187 | if $INFO =~ /n/ |
---|
188 | info += gphid.to_s |
---|
189 | info += '.' if $INFO =~ /[fm]/ |
---|
190 | end |
---|
191 | if $INFO =~ /f/ |
---|
192 | info += $form[gphid] |
---|
193 | info += ';' if $INFO =~ /m/ |
---|
194 | end |
---|
195 | if $INFO =~ /m/ |
---|
196 | info += $lem[gphid] |
---|
197 | end |
---|
198 | info |
---|
199 | end |
---|
200 | |
---|
201 | |
---|
202 | def arcsinfo(root,arcs) |
---|
203 | "head:#{nodeinfo(root)} links:" + arcs.map{|a| "(#{($INFO =~ /l/) ? a[2]+":" : ""}#{nodeinfo(a[0])}-#{nodeinfo(a[1])})"}.join("") |
---|
204 | # for a in arcs |
---|
205 | # print ';' |
---|
206 | # print "#{a[2]}:" if $INFO =~ /l/ |
---|
207 | # print nodeinfo(a[0])+'-'+nodeinfo(a[1]) |
---|
208 | # end |
---|
209 | end |
---|
210 | |
---|
211 | def printtree(root,arcs,o) |
---|
212 | if o==0 |
---|
213 | print "# %-16s" % "root: " |
---|
214 | end |
---|
215 | print nodeinfo(root),"\n" |
---|
216 | for arc in arcs.select{ |a| a[0]==root }.sort{|a,b| a[1]<=>b[1] } |
---|
217 | print '# '," "*(o+1) |
---|
218 | print "%-16s" % (arc[2]+": ") |
---|
219 | printtree(arc[1],arcs,o+1) |
---|
220 | end |
---|
221 | end |
---|
222 | |
---|
223 | def printtree_dgp(root,arcs,o) |
---|
224 | if o==0 |
---|
225 | print "%-16s" % "root: " |
---|
226 | end |
---|
227 | print nodeinfo(root),"\n" |
---|
228 | for arc in arcs.select{ |a| a[0]==root }.sort{|a,b| a[1]<=>b[1] } |
---|
229 | print " "*(o+1) |
---|
230 | print "%-16s" % (arc[2]+": ") |
---|
231 | printtree_dgp(arc[1],arcs,o+1) |
---|
232 | end |
---|
233 | end |
---|
234 | |
---|
235 | def printpar(root,arcs) |
---|
236 | print nodeinfo(root) |
---|
237 | deps = arcs.select{ |a| a[0]==root }.sort{|a,b| a[1]<=>b[1] } |
---|
238 | unless deps == [] |
---|
239 | print '(' |
---|
240 | cont=false |
---|
241 | for arc in deps |
---|
242 | if cont then print ',' else cont=true end |
---|
243 | print arc[2],':' if $INFO =~ /l/ |
---|
244 | printpar(arc[1],arcs) |
---|
245 | end |
---|
246 | print ')' |
---|
247 | end |
---|
248 | end |
---|
249 | |
---|
250 | |
---|
251 | def ground_tree_min t |
---|
252 | ([t[0]]+t[1].map{|e| [e[0],e[1]]}).flatten.min |
---|
253 | end |
---|
254 | |
---|
255 | def ground_tree_max t |
---|
256 | ([t[0]]+t[1].map{|e| [e[0],e[1]]}).flatten.max |
---|
257 | end |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | def parsegraph(nodes) |
---|
262 | |
---|
263 | $n =nodes.length |
---|
264 | $sat =[]; $vis =[]; $succ=[]; $lhs =[]; $arcs=[]; $pos=[]; $len=[]; $ord=[]; $distance={} |
---|
265 | |
---|
266 | for dgp in nodes |
---|
267 | |
---|
268 | parts = dgp[2].split($dgpsep,7) |
---|
269 | |
---|
270 | if parts[3]==nil || parts[4]==nil || parts[5]==nil |
---|
271 | $stderr.print "ERR: tre requires dgp be called with '--info s' option. Aborting.\n" |
---|
272 | exit |
---|
273 | end |
---|
274 | |
---|
275 | i = parts[0].to_i |
---|
276 | $pos[i] = dgp[0].to_i |
---|
277 | $len[i] = dgp[1].to_i |
---|
278 | $sat << i if parts[1]=="s" |
---|
279 | |
---|
280 | $arcs |= parts[2].split(',').map{ |a| case a |
---|
281 | when /\-\-(\w+)-(\d+)\((\d+)~(\d+)\)/ |
---|
282 | [i, $2.to_i, $1, $3.to_i, $4.to_i] |
---|
283 | when /\+\+(\w+)-(\d+)\((\d+)~(\d+)\)/ |
---|
284 | [$2.to_i, i, $1, $3.to_i, $4.to_i] |
---|
285 | end } |
---|
286 | $succ |= parts[3][1..-2].split(',').map{|x| [x.to_i,i]} |
---|
287 | $vis |= parts[4][1..-2].split(',').map{|x| [x.to_i,i]} |
---|
288 | $lhs |= parts[5][1..-2].split(',').map{|x| [x.to_i,i]} + [[i,i]] |
---|
289 | |
---|
290 | end |
---|
291 | |
---|
292 | end |
---|
293 | |
---|
294 | |
---|
295 | def ground(t) |
---|
296 | [ $gphid[t[0]] , t[1].map{|a| [$gphid[a[0]],$gphid[a[1]],a[2]]} ] |
---|
297 | end |
---|
298 | |
---|
299 | |
---|
300 | #NOWE-START |
---|
301 | |
---|
302 | def successors i |
---|
303 | $succ.select{|e| e[0]==i}.map{|e| e[1]} |
---|
304 | end |
---|
305 | |
---|
306 | def predecessors i |
---|
307 | $succ.select{|e| e[1]==i}.map{|e| e[0]} |
---|
308 | end |
---|
309 | |
---|
310 | def start_nodes |
---|
311 | $succ.map{|e| e[1]}.map{|e| predecessors(e)}.uniq.map{|e| e[0]} |
---|
312 | end |
---|
313 | |
---|
314 | def end_nodes |
---|
315 | $succ.map{|e| e[0]}.map{|e| successors(e)}.uniq.map{|e| e[0]} |
---|
316 | end |
---|
317 | |
---|
318 | def set_ord |
---|
319 | positions = $pos.uniq.sort |
---|
320 | (0...$n).each{|i| $ord[i] = positions.index($pos[i]) } |
---|
321 | end |
---|
322 | |
---|
323 | |
---|
324 | def set_distance_from_i i |
---|
325 | set_distance_from_i_to_jth_successors_to_v i, i, 1 |
---|
326 | end |
---|
327 | |
---|
328 | def set_distance_from_i_to_jth_successors_to_v i, j , v |
---|
329 | succ = successors(j) |
---|
330 | for j1 in succ |
---|
331 | $distance[[i,j1]] = v |
---|
332 | set_distance_from_i_to_jth_successors_to_v i, j1, v+1 |
---|
333 | end |
---|
334 | end |
---|
335 | |
---|
336 | #NOWE-END |
---|
337 | |
---|
338 | |
---|
339 | def gentrees |
---|
340 | bos=0; eos=$n-1; |
---|
341 | gentrees2 bos, eos |
---|
342 | end |
---|
343 | |
---|
344 | |
---|
345 | def genforest |
---|
346 | forest=[] |
---|
347 | for bos in start_nodes |
---|
348 | for eos in end_nodes # tu s± te¿ wierzcho³ki poprzedzaj±ce!!! |
---|
349 | next if $ord[bos] > $ord[eos] or ($MAXSIZE != nil and $ord[eos] - $ord[bos] > $MAXSIZE+1) |
---|
350 | forest += gentrees2(bos,eos) |
---|
351 | end |
---|
352 | end |
---|
353 | forest |
---|
354 | end |
---|
355 | |
---|
356 | def gentrees2 bos, eos |
---|
357 | $thetrees=[]; |
---|
358 | roots = (bos..eos).select{|i| $vis.include? [i,eos]}.select{|i| $vis.include? [bos,i]} |
---|
359 | |
---|
360 | if $DEBUG then print "ROOTS: #{roots.inspect}\n" end |
---|
361 | for root in roots |
---|
362 | gentrees3 bos, eos, root |
---|
363 | end |
---|
364 | $thetrees |
---|
365 | end |
---|
366 | |
---|
367 | def gentrees3 bos, eos, root |
---|
368 | $theroot=root |
---|
369 | $thebos=bos |
---|
370 | $theeos=eos |
---|
371 | for r in buildR(root , eos, []) |
---|
372 | (rmin,rmax,rtree) = r |
---|
373 | buildR(bos, rmin, rtree) |
---|
374 | end |
---|
375 | end |
---|
376 | |
---|
377 | def buildR(min, max, tree) |
---|
378 | if $DEBUG then print "buildR--#{min}--#{max}--#{tree.inspect}\n" end |
---|
379 | trees=[] |
---|
380 | for arc in $arcs.select{|a| a[0]==max && $vis.include?([min,a[1]]) } |
---|
381 | if $DEBUG then print "ARC: #{arc.inspect}\n" end |
---|
382 | for r in buildR(arc[1],arc[0],tree+[arc]) #!!! buildR(a[1],a[3],tree+[a]) |
---|
383 | # for r in buildR(arc[4],arc[3],tree+[arc]) #!!! buildR(a[1],a[3],tree+[a]) |
---|
384 | (rmin,rmax,rarcs) = r |
---|
385 | for l in buildR(min,rmin,rarcs) |
---|
386 | (lmin,lmax,larcs) = l |
---|
387 | trees << [lmin,rmax,larcs] |
---|
388 | end |
---|
389 | end |
---|
390 | end |
---|
391 | for i in (0...$n).select{|i| $succ.include?([i,max])}.select{|i| $lhs.include?([min,i])} |
---|
392 | for l in buildL(min,i,tree) |
---|
393 | (lmin,lmax,larcs) = l |
---|
394 | trees << [lmin,lmax,larcs] |
---|
395 | end |
---|
396 | end |
---|
397 | trees |
---|
398 | end |
---|
399 | |
---|
400 | |
---|
401 | def buildL(min,max,tree) |
---|
402 | if $DEBUG then print "buildL--#{min}--#{max}--#{tree.inspect}\n" end |
---|
403 | if $pos[min]==$pos[max] |
---|
404 | if min==$thebos && max==$thebos |
---|
405 | $thetrees.push [$theroot,tree] |
---|
406 | if $DEBUG then print "adding tree: #{tree.inspect}\n" end |
---|
407 | end |
---|
408 | return [[max,max,tree]] |
---|
409 | end |
---|
410 | trees=[] |
---|
411 | for arc in $arcs.select{|a| a[1]==max && $lhs.include?([min,a[0]]) } |
---|
412 | if $DEBUG then print "ARC: #{arc.inspect}\n" end |
---|
413 | for r in buildR(arc[0],arc[1],tree+[arc]) ### buildR(arc[3],max,tree+[arc]) |
---|
414 | #for r in buildR(arc[3],arc[4],tree+[arc]) ### buildR(arc[3],max,tree+[arc]) |
---|
415 | (rmin,rmax,rarcs) = r |
---|
416 | for l in buildL(min,rmin,rarcs) |
---|
417 | (lmin,lmax,larcs) = l |
---|
418 | trees << [lmin,lmax,larcs] |
---|
419 | end |
---|
420 | end |
---|
421 | end |
---|
422 | trees |
---|
423 | end |
---|
424 | |
---|
425 | |
---|
426 | def printgraph() |
---|
427 | print "N: #{$n}\n" |
---|
428 | print "SAT: #{set_to_s($sat)}\n" |
---|
429 | print "SUCC: #{rel_to_s($succ)}\n" |
---|
430 | print "VIS: #{rel_to_s($vis)}\n" |
---|
431 | print "LHS: #{rel_to_s($lhs)}\n" |
---|
432 | print "ARCS: #{arcs_to_s($arcs)}\n" |
---|
433 | end |
---|
434 | |
---|
435 | |
---|
436 | def printground |
---|
437 | for i in 1...($form.length-1) |
---|
438 | print "#{$ord1[i]} #{$form[i]} #{$lem[i]} " |
---|
439 | print $arcs.select{|a| $ord1[$gphid[a[1]]] == $ord1[i]}.map{|a| "#{a[2]}:#{$ord1[$gphid[a[0]]]}"}.sort.uniq.join(' ') |
---|
440 | print "\n" |
---|
441 | end |
---|
442 | end |
---|
443 | |
---|
444 | |
---|
445 | def set_to_s(s) "{#{s.join(',')}}" end |
---|
446 | def rel_to_s(r) "{#{r.map{|p| "(#{p[0]},#{p[1]})"}.join(',')}}" end |
---|
447 | def arc_to_s(q) "-#{q[0]}-#{q[2]}-#{q[1]}/#{q[3]}" end |
---|
448 | def arcs_to_s(a) "{#{a.map{|q| arc_to_s(q)}.join(',')}}" end |
---|
449 | |
---|
450 | ###################################################################### |
---|
451 | |
---|
452 | tre($stdin) |
---|