| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## File: DTA::CAB::Analyzer::Automaton::Gfsm::XL.pm | ||
| 3 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 4 | ## Description: Gfsm::XL::Cascade -based transductions | ||
| 5 | |||
| 6 | package DTA::CAB::Analyzer::Automaton::Gfsm::XL; | ||
| 7 | use DTA::CAB::Analyzer::Automaton; | ||
| 8 | use Gfsm; | ||
| 9 | use Gfsm::XL; | ||
| 10 | use Encode qw(encode decode); | ||
| 11 | use IO::File; | ||
| 12 | use Carp; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | |||
| 16 | ##============================================================================== | ||
| 17 | ## Globals | ||
| 18 | ##============================================================================== | ||
| 19 | |||
| 20 | our @ISA = qw(DTA::CAB::Analyzer::Automaton); | ||
| 21 | |||
| 22 | our $DEFAULT_ANALYZE_PRE = q($anl->setLookupOptions({src=>$_[0],map {($_=>$anl->{$_})} qw(max_paths max_weight max_ops)});); | ||
| 23 | |||
| 24 | ##============================================================================== | ||
| 25 | ## Constructors etc. | ||
| 26 | ##============================================================================== | ||
| 27 | |||
| 28 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 29 | ## + object structure: see DTA::CAB::Analyzer::Automaton | ||
| 30 | ## + new data / changes | ||
| 31 | ## ( | ||
| 32 | ## ##-- Analysis objects | ||
| 33 | ## fst => $cl, ##-- a Gfsm::XL::Cascade::Lookup object (default=new) | ||
| 34 | ## | ||
| 35 | ## ##-- Lookup options (obsolete: only used if you leave the default analyzePre method) | ||
| 36 | ## max_paths => $max_paths, ##-- sets $cl->max_paths() | ||
| 37 | ## max_ops => $max_ops_or_array, ##-- sets $cl->max_ops() ; default=16384 | ||
| 38 | ## max_weight => $max_weight_or_array, ##-- sets $cl->max_weight() | ||
| 39 | ## ) | ||
| 40 | ## + $max_ops, $max_weight may also be specified as an ARRAY-ref [$a,$b] | ||
| 41 | ## to compute the actual parameter on-the-fly as the linear function: | ||
| 42 | ## $max_thingy = $a * length($input_word) + $b | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $that = shift; | ||
| 46 | my $aut = $that->SUPER::new( | ||
| 47 | ##-- analysis objects | ||
| 48 | fst=>undef, #Gfsm::XL::Cascade::Lookup->new(undef), | ||
| 49 | |||
| 50 | ##-- lookup options | ||
| 51 | #max_weight => 3e38, | ||
| 52 | #max_paths => 1, | ||
| 53 | max_ops => 16384, | ||
| 54 | |||
| 55 | analyzePre => $DEFAULT_ANALYZE_PRE, | ||
| 56 | |||
| 57 | ##-- user args | ||
| 58 | @_ | ||
| 59 | ); | ||
| 60 | $aut->setLookupOptions($aut); | ||
| 61 | return $aut; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | ## $aut = $aut->clear() | ||
| 66 | sub clear { | ||
| 67 | my $aut = shift; | ||
| 68 | |||
| 69 | $aut->{fst}->_cascade_set(undef) if ($aut->{fst}); | ||
| 70 | |||
| 71 | ##-- inherited | ||
| 72 | $aut->SUPER::clear(); | ||
| 73 | } | ||
| 74 | |||
| 75 | ## $aut = $aut->resetProfilingData() | ||
| 76 | ## - inherited | ||
| 77 | |||
| 78 | ##-------------------------------------------------------------- | ||
| 79 | ## Methods: Lookup Options | ||
| 80 | |||
| 81 | ## $aut = $aut->setLookupOptions(\%opts) | ||
| 82 | ## + obsolete; remains for compatibility with pre-v1.31 code | ||
| 83 | ## + \%opts keys: | ||
| 84 | ## max_paths => $n_paths, | ||
| 85 | ## max_ops => $n_ops_or_array, | ||
| 86 | ## max_weight => $w_or_array, | ||
| 87 | sub setLookupOptions { | ||
| 88 | my ($aut,$opts) = @_; | ||
| 89 | my $cl = $aut->{fst}; | ||
| 90 | return if (!defined($cl)); | ||
| 91 | |||
| 92 | ##-- opts: max_weight | ||
| 93 | if (UNIVERSAL::isa($opts->{max_weight},'ARRAY')) { | ||
| 94 | $cl->max_weight($opts->{max_weight}[0] * length(($opts->{src}||'1')) + $opts->{max_weight}[1]); | ||
| 95 | } elsif (defined($opts->{max_weight})) { | ||
| 96 | $cl->max_weight($opts->{max_weight}); | ||
| 97 | } | ||
| 98 | |||
| 99 | ##-- opts: max_ops | ||
| 100 | if (UNIVERSAL::isa($opts->{max_ops},'ARRAY')) { | ||
| 101 | $cl->max_ops($opts->{max_ops}[0] * length(($opts->{src}||'1')) + $opts->{max_ops}[1]); | ||
| 102 | } elsif (defined($opts->{max_ops})) { | ||
| 103 | $cl->max_ops($opts->{max_ops}); | ||
| 104 | } | ||
| 105 | |||
| 106 | ##-- opts: max_paths | ||
| 107 | $cl->max_paths ($opts->{max_paths}) if (defined($opts->{max_paths})); | ||
| 108 | |||
| 109 | return $aut; | ||
| 110 | } | ||
| 111 | |||
| 112 | ##============================================================================== | ||
| 113 | ## Methods: Generic | ||
| 114 | ##============================================================================== | ||
| 115 | |||
| 116 | ## $class = $aut->fstClass() | ||
| 117 | ## + default FST class for loadFst() method | ||
| 118 | sub fstClass { return 'Gfsm::XL::Cascade'; } | ||
| 119 | |||
| 120 | ## $class = $aut->labClass() | ||
| 121 | ## + default labels class for loadLabels() method | ||
| 122 | sub labClass { return 'Gfsm::Alphabet'; } | ||
| 123 | |||
| 124 | ## $bool = $aut->fstOk() | ||
| 125 | ## + should return false iff fst is undefined or "empty" | ||
| 126 | sub fstOk { return defined($_[0]{fst}) && defined($_[0]{fst}->cascade) && $_[0]{fst}->cascade->depth>0; } | ||
| 127 | |||
| 128 | ## $bool = $aut->labOk() | ||
| 129 | ## + should return false iff label-set is undefined or "empty" | ||
| 130 | #(inherited) | ||
| 131 | |||
| 132 | ## $bool = $aut->dictOk() | ||
| 133 | ## + should return false iff dict is undefined or "empty" | ||
| 134 | ##(inherited) | ||
| 135 | |||
| 136 | |||
| 137 | ##============================================================================== | ||
| 138 | ## Methods: I/O | ||
| 139 | ##============================================================================== | ||
| 140 | |||
| 141 | ##-------------------------------------------------------------- | ||
| 142 | ## Methods: I/O: Input: all | ||
| 143 | |||
| 144 | ## $aut = $aut->load(fst=>$fstFile, lab=>$labFile, dict=>$dictFile) | ||
| 145 | ## + inherited | ||
| 146 | |||
| 147 | ##-------------------------------------------------------------- | ||
| 148 | ## Methods: I/O: Input: Dictionary | ||
| 149 | |||
| 150 | ## $aut = $aut->loadDict($dictfile) | ||
| 151 | ## + inherited | ||
| 152 | |||
| 153 | |||
| 154 | ##-------------------------------------------------------------- | ||
| 155 | ## Methods: I/O: Input: Transducer | ||
| 156 | |||
| 157 | ## $aut = $aut->loadCascade($cscfile) | ||
| 158 | ## $aut = $aut->loadFst ($cscfile) | ||
| 159 | *loadFst = \&loadCascade; | ||
| 160 | sub loadCascade { | ||
| 161 | my ($aut,$cscfile) = @_; | ||
| 162 | $aut->info("loading cascade file '$cscfile'"); | ||
| 163 | my $csc = Gfsm::XL::Cascade->new(); | ||
| 164 | if (!$csc->load($cscfile)) { | ||
| 165 | $aut->logconfess("loadCascade(): load failed for '$cscfile': $!"); | ||
| 166 | return undef; | ||
| 167 | } | ||
| 168 | $aut->{fst} = Gfsm::XL::Cascade::Lookup->new($csc); | ||
| 169 | $aut->setLookupOptions($aut); | ||
| 170 | #$aut->{result} = Gfsm::Automaton->new($csc->semiring_type); ##-- reset result automaton | ||
| 171 | delete($aut->{_analyze}); | ||
| 172 | #print STDERR sprintf("loadCascade(): csc=0x%0.8x, cl=0x%0.8x\n", $$csc, ${$aut->{fst}}); ##-- DEBUG | ||
| 173 | return $aut; | ||
| 174 | } | ||
| 175 | |||
| 176 | ## $result = $aut->resultFst() | ||
| 177 | ## + returns empty result FST | ||
| 178 | sub resultFst { | ||
| 179 | return Gfsm::Automaton->new($_[0]{fst}->cascade->semiring_type); | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | ##-------------------------------------------------------------- | ||
| 184 | ## Methods: I/O: Input: Labels | ||
| 185 | |||
| 186 | ## $aut = $aut->loadLabels($labfile) | ||
| 187 | ## + inherited | ||
| 188 | |||
| 189 | ## $aut = $aut->parseLabels() | ||
| 190 | ## + inherited | ||
| 191 | |||
| 192 | ##============================================================================== | ||
| 193 | ## Methods: Analysis | ||
| 194 | ## + inherited from DTA::CAB::Analyzer::Automaton | ||
| 195 | ##============================================================================== | ||
| 196 | |||
| 197 | |||
| 198 | 1; ##-- be happy | ||
| 199 | ##======================================================================== | ||
| 200 | ## POD DOCUMENTATION, auto-generated by podextract.perl & edited | ||
| 201 | |||
| 202 | ##======================================================================== | ||
| 203 | ## NAME | ||
| 204 | =pod | ||
| 205 | |||
| 206 | =head1 NAME | ||
| 207 | |||
| 208 | DTA::CAB::Analyzer::Automaton::Gfsm::XL - Gfsm::XL::Cascade-based transductions | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | ##======================================================================== | ||
| 213 | ## SYNOPSIS | ||
| 214 | =pod | ||
| 215 | |||
| 216 | =head1 SYNOPSIS | ||
| 217 | |||
| 218 | use DTA::CAB::Analyzer::Automaton::Gfsm::XL; | ||
| 219 | |||
| 220 | ##======================================================================== | ||
| 221 | ## Constructors etc. | ||
| 222 | |||
| 223 | $obj = DTA::CAB::Analyzer::Automaton::Gfsm::XL->new(%args); | ||
| 224 | $aut = $aut->clear(); | ||
| 225 | $aut = $aut->setLookupOptions(\%opts); | ||
| 226 | |||
| 227 | ##======================================================================== | ||
| 228 | ## Methods: Generic | ||
| 229 | |||
| 230 | $class = $aut->fstClass(); | ||
| 231 | $class = $aut->labClass(); | ||
| 232 | $bool = $aut->fstOk(); | ||
| 233 | |||
| 234 | ##======================================================================== | ||
| 235 | ## Methods: I/O | ||
| 236 | |||
| 237 | $aut = $aut->loadCascade($cscfile); | ||
| 238 | |||
| 239 | =cut | ||
| 240 | |||
| 241 | ##======================================================================== | ||
| 242 | ## DESCRIPTION | ||
| 243 | =pod | ||
| 244 | |||
| 245 | =head1 DESCRIPTION | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | ##---------------------------------------------------------------- | ||
| 250 | ## DESCRIPTION: DTA::CAB::Analyzer::Automaton::Gfsm::XL: Globals | ||
| 251 | =pod | ||
| 252 | |||
| 253 | =head2 Globals | ||
| 254 | |||
| 255 | =over 4 | ||
| 256 | |||
| 257 | =item Variable: @ISA | ||
| 258 | |||
| 259 | DTA::CAB::Analyzer::Automaton::Gfsm::XL | ||
| 260 | inherits from | ||
| 261 | L<DTA::CAB::Analyzer::Automaton|DTA::CAB::Analyzer::Automaton>. | ||
| 262 | |||
| 263 | =back | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | ##---------------------------------------------------------------- | ||
| 268 | ## DESCRIPTION: DTA::CAB::Analyzer::Automaton::Gfsm::XL: Constructors etc. | ||
| 269 | =pod | ||
| 270 | |||
| 271 | =head2 Constructors etc. | ||
| 272 | |||
| 273 | =over 4 | ||
| 274 | |||
| 275 | =item new | ||
| 276 | |||
| 277 | $aut = CLASS_OR_OBJ->new(%args); | ||
| 278 | |||
| 279 | Constructor override: see L<DTA::CAB::Analyzer::Automaton::new()|DTA::CAB::Analyzer::Automaton/item_new>. | ||
| 280 | |||
| 281 | new and/or changed %args, %$aut: | ||
| 282 | |||
| 283 | ##-- Analysis objects | ||
| 284 | fst => $cl, ##-- a Gfsm::XL::Cascade::Lookup object (default=new) | ||
| 285 | ## | ||
| 286 | ##-- Lookup options (new) | ||
| 287 | max_paths => $max_paths, ##-- sets $cl->max_paths() | ||
| 288 | max_ops => $max_ops, ##-- sets $cl->max_ops() | ||
| 289 | max_weight => $max_weight_or_array, ##-- sets $cl->max_weight() | ||
| 290 | ## + may also be specified as an ARRAY-ref [$a,$b] | ||
| 291 | ## to compute max-weight parameter on-the-fly as | ||
| 292 | ## the linear function: | ||
| 293 | ## $max_weight = $a * length($input_word) + $b | ||
| 294 | |||
| 295 | |||
| 296 | =item clear | ||
| 297 | |||
| 298 | $aut = $aut->clear(); | ||
| 299 | |||
| 300 | Override: clear automaton. | ||
| 301 | |||
| 302 | =item setLookupOptions | ||
| 303 | |||
| 304 | $aut = $aut->setLookupOptions(\%opts); | ||
| 305 | |||
| 306 | Sets lookup-local options %opts: | ||
| 307 | |||
| 308 | src => $input_text, | ||
| 309 | max_weight => $w, | ||
| 310 | max_paths => $n_paths, | ||
| 311 | max_ops => $n_ops, | ||
| 312 | |||
| 313 | =back | ||
| 314 | |||
| 315 | =cut | ||
| 316 | |||
| 317 | ##---------------------------------------------------------------- | ||
| 318 | ## DESCRIPTION: DTA::CAB::Analyzer::Automaton::Gfsm::XL: Methods: Generic | ||
| 319 | =pod | ||
| 320 | |||
| 321 | =head2 Methods: Generic | ||
| 322 | |||
| 323 | =over 4 | ||
| 324 | |||
| 325 | =item fstClass | ||
| 326 | |||
| 327 | $class = $aut->fstClass(); | ||
| 328 | |||
| 329 | Override: default FST class for L</loadFst>() method | ||
| 330 | |||
| 331 | =item labClass | ||
| 332 | |||
| 333 | $class = $aut->labClass(); | ||
| 334 | |||
| 335 | Override: default labels class for L</loadLabels>() method | ||
| 336 | |||
| 337 | =item fstOk | ||
| 338 | |||
| 339 | $bool = $aut->fstOk(); | ||
| 340 | |||
| 341 | Override: should return false iff fst is undefined or "empty" | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | ##---------------------------------------------------------------- | ||
| 348 | ## DESCRIPTION: DTA::CAB::Analyzer::Automaton::Gfsm::XL: Methods: I/O | ||
| 349 | =pod | ||
| 350 | |||
| 351 | =head2 Methods: I/O | ||
| 352 | |||
| 353 | =over 4 | ||
| 354 | |||
| 355 | =item loadCascade | ||
| 356 | |||
| 357 | $aut = $aut->loadCascade($cscfile); | ||
| 358 | $aut = $aut->loadFst ($cscfile) | ||
| 359 | |||
| 360 | Alias / override. | ||
| 361 | |||
| 362 | =back | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | ##======================================================================== | ||
| 367 | ## END POD DOCUMENTATION, auto-generated by podextract.perl | ||
| 368 | |||
| 369 | ##====================================================================== | ||
| 370 | ## Footer | ||
| 371 | ##====================================================================== | ||
| 372 | |||
| 373 | =pod | ||
| 374 | |||
| 375 | =head1 AUTHOR | ||
| 376 | |||
| 377 | Bryan Jurish E<lt>moocow@cpan.orgE<gt> | ||
| 378 | |||
| 379 | =head1 COPYRIGHT AND LICENSE | ||
| 380 | |||
| 381 | Copyright (C) 2009-2019 by Bryan Jurish | ||
| 382 | |||
| 383 | This package is free software; you can redistribute it and/or modify | ||
| 384 | it under the same terms as Perl itself, either Perl version 5.24.1 or, | ||
| 385 | at your option, any later version of Perl 5 you may have available. | ||
| 386 | |||
| 387 | =cut |