[adb4c8d] | 1 | #!/usr/bin/ruby -I /usr/local/lib/utt -I $HOME/.local/lib/utt |
---|
[6b3be72] | 2 | |
---|
| 3 | require 'getoptlong' |
---|
| 4 | |
---|
| 5 | opts = GetoptLong.new( |
---|
| 6 | [ '--help', '-h', GetoptLong::NO_ARGUMENT ], |
---|
| 7 | [ '--debug', '-d', GetoptLong::NO_ARGUMENT ], |
---|
| 8 | [ '--format', '-F', GetoptLong::REQUIRED_ARGUMENT ], |
---|
| 9 | [ '--info', '-I', GetoptLong::REQUIRED_ARGUMENT ], |
---|
| 10 | [ '--only-trees','-t', GetoptLong::NO_ARGUMENT ]) |
---|
| 11 | |
---|
| 12 | $helptext= |
---|
| 13 | "The program generates trees from the graph output by dgp. dgp must\n"+ |
---|
| 14 | "must be run with '-i ds' option.\n\n"+ |
---|
| 15 | "Command: tre [options]\n\n"+ |
---|
| 16 | "Options:\n"+ |
---|
| 17 | "--help -h Print help (this text) and exit.\n"+ |
---|
| 18 | "--debug -d Verbose output. For developers only.\n"+ |
---|
| 19 | "--format=s -F s Output format. Recognized values:\n"+ |
---|
| 20 | " a root + list of arcs\n"+ |
---|
| 21 | " p parenthesized notation\n"+ |
---|
| 22 | " h human readable indented tree format\n"+ |
---|
| 23 | " Multiple values are allowed. (default p)\n"+ |
---|
| 24 | "--info=s -I s Information printed. Recognized values:\n"+ |
---|
| 25 | " n node identifier\n"+ |
---|
| 26 | " f surface form\n"+ |
---|
| 27 | " m morphological information\n"+ |
---|
| 28 | " l arc labels\n"+ |
---|
| 29 | "--only-trees -t Do not copy input. Print trees only.\n" |
---|
| 30 | |
---|
| 31 | $DEBUG=false |
---|
| 32 | $FORMAT='p' |
---|
| 33 | $INFO='DEFAULT' |
---|
| 34 | $ONLYTREES=false |
---|
| 35 | |
---|
| 36 | opts.each do |opt, arg| |
---|
| 37 | case opt |
---|
| 38 | when '--help' |
---|
| 39 | print $helptext |
---|
| 40 | exit 0 |
---|
| 41 | when '--debug' |
---|
| 42 | $DEBUG=true |
---|
| 43 | when '--format' |
---|
| 44 | $FORMAT=arg |
---|
| 45 | when '--info' |
---|
| 46 | $INFO=arg |
---|
| 47 | when '--only-trees' |
---|
| 48 | $ONLYTREES=true |
---|
| 49 | else |
---|
| 50 | print "Unknown option #{opt}. Ignored.\n" |
---|
| 51 | end |
---|
| 52 | end |
---|
| 53 | |
---|
| 54 | if $INFO=='DEFAULT' |
---|
| 55 | case $FORMAT |
---|
| 56 | when 'p','a' |
---|
| 57 | $INFO='nl' |
---|
| 58 | when 'h' |
---|
| 59 | $INFO='fmnl' |
---|
| 60 | end |
---|
| 61 | end |
---|
| 62 | |
---|
[adb4c8d] | 63 | #require File.expand_path(File.dirname(__FILE__) + "../lib/utt/seg.rb") |
---|
| 64 | require 'seg.rb' |
---|
[6b3be72] | 65 | |
---|
| 66 | $dgpsep=';' |
---|
| 67 | |
---|
| 68 | def tre(input) |
---|
| 69 | $gphid=[] |
---|
| 70 | $form=[] |
---|
| 71 | $lem=[] |
---|
| 72 | nodes=[] |
---|
| 73 | count=0 |
---|
| 74 | seg=Seg.new |
---|
| 75 | for line in input |
---|
| 76 | print line unless $ONLYTREES |
---|
| 77 | seg.set(line) |
---|
| 78 | if dgp=seg['dgp'] |
---|
| 79 | if nodes==[] && seg[3]!='BOS' |
---|
| 80 | print "A sentence must start with BOS segment. Aborting.\n" |
---|
| 81 | return |
---|
| 82 | end |
---|
| 83 | |
---|
| 84 | id=dgp[/^\d+/].to_i |
---|
| 85 | |
---|
| 86 | if gph=seg['gph'] |
---|
| 87 | $gphid[id]=gph[/^\d+/].to_i |
---|
| 88 | else |
---|
| 89 | print "No gph field. Aborting.\n" |
---|
| 90 | return |
---|
| 91 | end |
---|
| 92 | |
---|
| 93 | $form[$gphid[id]]=seg[4] |
---|
| 94 | $lem[$gphid[id]]=seg['lem'] |
---|
| 95 | |
---|
| 96 | nodes[id] = [seg[1].to_i,dgp] |
---|
| 97 | |
---|
| 98 | if seg[3]=='EOS' |
---|
| 99 | $pref = "#{seg[1]} #{seg[2]} SYN *" |
---|
| 100 | parsegraph(nodes) |
---|
| 101 | printgraph if $DEBUG |
---|
| 102 | $thetrees=[] |
---|
| 103 | gentrees2 |
---|
| 104 | for t in $thetrees |
---|
| 105 | count += 1 |
---|
| 106 | t1=ground(t) |
---|
| 107 | case $FORMAT |
---|
| 108 | when /a/ |
---|
| 109 | print "#{$pref} tre:#{count} arc:" |
---|
| 110 | printarcs(t1[0],t1[1]) |
---|
| 111 | print "\n" |
---|
| 112 | when /p/ |
---|
| 113 | print "#{$pref} tre:#{count} par:" |
---|
| 114 | printpar(t1[0],t1[1]) |
---|
| 115 | print "\n" |
---|
| 116 | when /h/ |
---|
| 117 | print "#\n# tree #{count}\n# ------\n" |
---|
| 118 | printtree(t1[0],t1[1],0) |
---|
| 119 | end |
---|
| 120 | end |
---|
| 121 | nodes=[] |
---|
| 122 | end |
---|
| 123 | end |
---|
| 124 | end |
---|
| 125 | end |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | def nodeinfo(id) |
---|
| 129 | info="" |
---|
| 130 | if $INFO =~ /n/ |
---|
| 131 | info += id.to_s |
---|
| 132 | info += '.' if $INFO =~ /[fm]/ |
---|
| 133 | end |
---|
| 134 | if $INFO =~ /f/ |
---|
| 135 | info += $form[id] |
---|
| 136 | info += ';' if $INFO =~ /m/ |
---|
| 137 | end |
---|
| 138 | if $INFO =~ /m/ |
---|
| 139 | info += $lem[id] |
---|
| 140 | end |
---|
| 141 | info |
---|
| 142 | end |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | def printarcs(root,arcs) |
---|
| 146 | print nodeinfo(root) |
---|
| 147 | for a in arcs |
---|
| 148 | print ';' |
---|
| 149 | print "#{a[2]}:" if $INFO =~ /l/ |
---|
| 150 | print nodeinfo(a[0])+'-'+nodeinfo(a[1]) |
---|
| 151 | end |
---|
| 152 | end |
---|
| 153 | |
---|
| 154 | def printtree(root,arcs,o) |
---|
| 155 | if o==0 |
---|
| 156 | print "# %-16s" % "root: " |
---|
| 157 | end |
---|
| 158 | print nodeinfo(root),"\n" |
---|
| 159 | for arc in arcs.select{ |a| a[0]==root }.sort{|a,b| a[1]<=>b[1] } |
---|
| 160 | print '# '," "*(o+1) |
---|
| 161 | print "%-16s" % (arc[2]+": ") |
---|
| 162 | printtree(arc[1],arcs,o+1) |
---|
| 163 | end |
---|
| 164 | end |
---|
| 165 | |
---|
| 166 | def printpar(root,arcs) |
---|
| 167 | print nodeinfo(root) |
---|
| 168 | deps = arcs.select{ |a| a[0]==root }.sort{|a,b| a[1]<=>b[1] } |
---|
| 169 | unless deps == [] |
---|
| 170 | print '(' |
---|
| 171 | cont=false |
---|
| 172 | for arc in deps |
---|
| 173 | if cont then print ',' else cont=true end |
---|
| 174 | print arc[2],':' if $INFO =~ /l/ |
---|
| 175 | printpar(arc[1],arcs) |
---|
| 176 | end |
---|
| 177 | print ')' |
---|
| 178 | end |
---|
| 179 | end |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | def parsegraph(nodes) |
---|
| 183 | |
---|
| 184 | $n =nodes.length |
---|
| 185 | $sat =[]; |
---|
| 186 | |
---|
| 187 | $vis =[]; |
---|
| 188 | $succ=[]; |
---|
| 189 | $lhs =[]; |
---|
| 190 | $arcs=[]; |
---|
| 191 | $pos=[] |
---|
| 192 | |
---|
| 193 | for dgp in nodes |
---|
| 194 | |
---|
| 195 | parts = dgp[1].split($dgpsep,6) |
---|
| 196 | |
---|
| 197 | i = parts[0].to_i |
---|
| 198 | $pos[i] = dgp[0].to_i |
---|
| 199 | $sat << i if parts[1]=="s" |
---|
| 200 | $arcs |= parts[2].split(',').map{ |a| case a |
---|
| 201 | when /\-\-(\w+)-(\d+)\/(\d+)/ |
---|
| 202 | [i, $2.to_i, $1, $3.to_i] |
---|
| 203 | when /\+\+(\d+)-(\w+)\/(\d+)/ |
---|
| 204 | [$1.to_i, i, $2, $3.to_i] |
---|
| 205 | end } |
---|
| 206 | $succ |= parts[3][1..-2].split(',').map{|x| [x.to_i,i]} |
---|
| 207 | $vis |= parts[4][1..-2].split(',').map{|x| [x.to_i,i]} |
---|
| 208 | $lhs |= parts[5][1..-2].split(',').map{|x| [x.to_i,i]} + [[i,i]] |
---|
| 209 | |
---|
| 210 | end |
---|
| 211 | end |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | def ground(t) |
---|
| 215 | [ $gphid[t[0]] , t[1].map{|a| [$gphid[a[0]],$gphid[a[1]],a[2]]} ] |
---|
| 216 | end |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | def gentrees2() |
---|
| 220 | $thetrees=[]; |
---|
| 221 | bos=0; eos=$n-1; |
---|
| 222 | roots = (1...eos).select{|i| $vis.include? [i,eos]}.select{|i| $vis.include? [bos,i]} |
---|
| 223 | if $DEBUG then print "ROOTS: #{roots.inspect}\n" end |
---|
| 224 | for i in roots |
---|
| 225 | $theroot=i |
---|
| 226 | for r in buildR(i , eos, []) |
---|
| 227 | (rmin,rmax,rtree) = r |
---|
| 228 | buildR(bos, rmin, rtree) |
---|
| 229 | end |
---|
| 230 | end |
---|
| 231 | end |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | def buildR(min, max, tree) |
---|
| 235 | if $DEBUG then print "buildR--#{min}--#{max}--#{tree.inspect}\n" end |
---|
| 236 | trees=[] |
---|
| 237 | for a in $arcs.select{|a| a[0]==max && $vis.include?([min,a[1]]) } |
---|
| 238 | if $DEBUG then print "ARC: #{a.inspect}\n" end |
---|
| 239 | for r in buildR(a[1],a[3],tree+[a]) |
---|
| 240 | (rmin,rmax,rarcs) = r |
---|
| 241 | for l in buildR(min,rmin,rarcs) |
---|
| 242 | (lmin,lmax,larcs) = l |
---|
| 243 | trees << [lmin,rmax,larcs] |
---|
| 244 | end |
---|
| 245 | end |
---|
| 246 | end |
---|
| 247 | for i in (0...$n).select{|i| $succ.include?([i,max])}.select{|i| $lhs.include?([min,i])} |
---|
| 248 | for l in buildL(min,i,tree) |
---|
| 249 | (lmin,lmax,larcs) = l |
---|
| 250 | trees << [lmin,lmax,larcs] |
---|
| 251 | end |
---|
| 252 | end |
---|
| 253 | trees |
---|
| 254 | end |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | def buildL(min,max,tree) |
---|
| 258 | if $DEBUG then print "buildL--#{min}--#{max}--#{tree.inspect}\n" end |
---|
| 259 | if $pos[min]==$pos[max] |
---|
| 260 | if min==0 && max==0 |
---|
| 261 | $thetrees.push [$theroot,tree] |
---|
| 262 | if $DEBUG then print "adding tree: #{tree.inspect}\n" end |
---|
| 263 | end |
---|
| 264 | return [[max,max,tree]] |
---|
| 265 | end |
---|
| 266 | trees=[] |
---|
| 267 | for arc in $arcs.select{|a| a[1]==max && $lhs.include?([min,a[0]]) } |
---|
| 268 | if $DEBUG then print "ARC: #{arc.inspect}\n" end |
---|
| 269 | for r in buildR(arc[3],max,tree+[arc]) |
---|
| 270 | (rmin,rmax,rarcs) = r |
---|
| 271 | for l in buildL(min,rmin,rarcs) |
---|
| 272 | (lmin,lmax,larcs) = l |
---|
| 273 | trees << [lmin,lmax,larcs] |
---|
| 274 | end |
---|
| 275 | end |
---|
| 276 | end |
---|
| 277 | trees |
---|
| 278 | end |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | def printgraph() |
---|
| 282 | |
---|
| 283 | print "N: #{$n}\n" |
---|
| 284 | print "SAT: #{set_to_s($sat)}\n" |
---|
| 285 | print "SUCC: #{rel_to_s($succ)}\n" |
---|
| 286 | print "VIS: #{rel_to_s($vis)}\n" |
---|
| 287 | print "LHS: #{rel_to_s($lhs)}\n" |
---|
| 288 | print "ARCS: #{arcs_to_s($arcs)}\n" |
---|
| 289 | end |
---|
| 290 | |
---|
| 291 | def set_to_s(s) "{#{s.join(',')}}" end |
---|
| 292 | def rel_to_s(r) "{#{r.map{|p| "(#{p[0]},#{p[1]})"}.join(',')}}" end |
---|
| 293 | def arc_to_s(q) "-#{q[0]}-#{q[2]}-#{q[1]}/#{q[3]}" end |
---|
| 294 | def arcs_to_s(a) "{#{a.map{|q| arc_to_s(q)}.join(',')}}" end |
---|
| 295 | |
---|
| 296 | ###################################################################### |
---|
| 297 | |
---|
| 298 | tre($stdin) |
---|