| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::DmootSub.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: sub-analysis (Morph,toka) of dmoot targets | ||
| 6 | |||
| 7 | ##============================================================================== | ||
| 8 | ## Package: Analyzer::DmootSub | ||
| 9 | ##============================================================================== | ||
| 10 | package DTA::CAB::Analyzer::DmootSub; | ||
| 11 | use DTA::CAB::Analyzer ':child'; | ||
| 12 | use DTA::CAB::Chain; | ||
| 13 | use DTA::CAB::Analyzer::Morph; | ||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | our @ISA = qw(DTA::CAB::Chain); | ||
| 17 | |||
| 18 | ##============================================================================== | ||
| 19 | ## Methods: Constructors etc. | ||
| 20 | |||
| 21 | ## $obj = $CLASS_OR_OBJ->new(chain=>\@analyzers, %args) | ||
| 22 | ## + basic object structure: (see also DTA::CAB::Chain) | ||
| 23 | ## chain => [$a1, ..., $aN], ##-- sub-analysis chain (e.g. chain=>[$morph,$mlatin]) | ||
| 24 | ## + new object structure: | ||
| 25 | ## dmootLabel => $label, ##-- label of source dmoot object (default='dmoot') | ||
| 26 | ## standalone => $bool, ##-- if true, no sub-analysis or morph parsing will be done (default=false) | ||
| 27 | sub new { | ||
| 28 | my $that = shift; | ||
| 29 | my $asub = $that->SUPER::new( | ||
| 30 | ##-- defaults | ||
| 31 | #analysisClass => 'DTA::CAB::Analyzer::Rewrite::Analysis', | ||
| 32 | label => 'dmsub', | ||
| 33 | |||
| 34 | ##-- analysis selection | ||
| 35 | dmootLabel => 'dmoot', | ||
| 36 | standalone => 0, | ||
| 37 | |||
| 38 | ##-- user args | ||
| 39 | @_ | ||
| 40 | ); | ||
| 41 | return $asub; | ||
| 42 | } | ||
| 43 | |||
| 44 | ##============================================================================== | ||
| 45 | ## Methods: I/O | ||
| 46 | |||
| 47 | ##------------------------------------------------------------------------ | ||
| 48 | ## Methods: I/O: Input: all | ||
| 49 | |||
| 50 | ## \@analyzers = $ach->chain() | ||
| 51 | ## \@analyzers = $ach->chain(\%opts) | ||
| 52 | ## + get selected analyzer chain | ||
| 53 | ### + NEW: just return $ach->{chain}, since analyzers may still be disabled here (argh) | ||
| 54 | sub chain { | ||
| 55 | my $ach = shift; | ||
| 56 | return $ach->{chain}; | ||
| 57 | #return [grep {$_ && $_->enabled} @{$ach->{chain}}]; | ||
| 58 | } | ||
| 59 | |||
| 60 | ## $bool = $ach->ensureLoaded() | ||
| 61 | ## + returns true if any chain member loads successfully (or if the chain is empty) | ||
| 62 | sub ensureLoaded { | ||
| 63 | my $ach = shift; | ||
| 64 | @{$ach->{chain}} = grep {$_} @{$ach->{chain}}; ##-- hack: chuck undef chain-links here | ||
| 65 | return 1 if (!@{$ach->{chain}}); | ||
| 66 | my $rc = 0; | ||
| 67 | foreach (@{$ach->{chain}}) { | ||
| 68 | $rc = $_->ensureLoaded() || $rc; | ||
| 69 | } | ||
| 70 | return $rc; | ||
| 71 | } | ||
| 72 | |||
| 73 | ##============================================================================== | ||
| 74 | ## Methods: Analysis | ||
| 75 | |||
| 76 | ## $bool = $anl->doAnalyze(\%opts, $name) | ||
| 77 | ## + override: only allow analyzeSentences() | ||
| 78 | sub doAnalyze { | ||
| 79 | my $anl = shift; | ||
| 80 | return 0 if (defined($_[1]) && $_[1] ne 'Sentences'); | ||
| 81 | return $anl->SUPER::doAnalyze(@_); | ||
| 82 | } | ||
| 83 | |||
| 84 | ## $doc = $anl->analyzeSentences($doc,\%opts) | ||
| 85 | ## + post-processing for 'dmoot' object | ||
| 86 | ## + extracts dmoot targets, builds pseudo-type hash, calls sub-chain analyzeTypes(), & expands back into 'dmoot' sources | ||
| 87 | sub analyzeSentences { | ||
| 88 | my ($asub,$doc,$opts) = @_; | ||
| 89 | return $doc if (!$asub->enabled($opts)); | ||
| 90 | |||
| 91 | ##-- load | ||
| 92 | #$asub->ensureLoaded(); | ||
| 93 | |||
| 94 | ##-- get dmoot target types | ||
| 95 | my $dmkey = $asub->{dmootLabel}; | ||
| 96 | my $standalone = $asub->{standalone}; | ||
| 97 | my $dmtypes = {}; ##-- $dmtypes: {$tokText => {text=>$dmootTag, morph=>\@dmootMorph}, ... } ##-- analyzed types | ||
| 98 | my $udmtypes = {}; ##-- $udmtypes: {$tokText => {text=>$dmootTag, morph=>undef, ...}} ##-- un-analyzed types | ||
| 99 | my $nil = []; | ||
| 100 | my ($tok,$txt,$dm,$dmtag,$dmtyp); | ||
| 101 | TOK: | ||
| 102 | foreach $tok (map {@{$_->{tokens}}} @{$doc->{body}}) { | ||
| 103 | next if (!defined($dm=$tok->{$dmkey})); | ||
| 104 | $dmtag = $dm->{tag}; | ||
| 105 | |||
| 106 | ##-- check for existing analyses | ||
| 107 | $txt = $tok->{xlit} ? $tok->{xlit}{latin1Text} : $tok->{text}; | ||
| 108 | if (($tok->{toka} && @{$tok->{toka}}) || ($tok->{tokpp} && @{$tok->{tokpp}})) { | ||
| 109 | ##-- existing analyses: toka|tokpp | ||
| 110 | $dm->{morph} = [ map { {hi=>$_,w=>0} } @{$tok->{toka} // $tok->{tokpp} // $nil} ]; | ||
| 111 | $dm->{tag} = $tok->{xlit} && $tok->{xlit}{isLatinExt} ? $tok->{xlit}{latin1Text} : $tok->{text}; ##-- force literal text for tokenizer-analyzed tokens | ||
| 112 | } | ||
| 113 | elsif (!$standalone) { | ||
| 114 | #$dmtyp = $dmtypes->{$dmtag} = { text=>$dmtag } if (!defined($dmtyp=$dmtypes->{$txt})); | ||
| 115 | $dmtyp = $dmtypes->{$txt} = { text=>$dmtag } if (!defined($dmtyp=$dmtypes->{$txt})); | ||
| 116 | if ($dmtyp->{morph} && @{$dmtyp->{morph}}) { | ||
| 117 | $dm->{morph} = $dmtyp->{morph} if (!$dm->{morph} || !@{$dm->{morph}}); | ||
| 118 | next; | ||
| 119 | } | ||
| 120 | |||
| 121 | if ($dmtag eq $txt) { | ||
| 122 | ##-- existing analyses: morph: from text | ||
| 123 | $dm->{morph} = $dmtyp->{morph} = $tok->{morph}; | ||
| 124 | |||
| 125 | ##-- latin analyses exist: add them | ||
| 126 | $dm->{morph} = $dmtyp->{morph} = [@{$dm->{morph}||[]}, @{$tok->{mlatin}}] if ($tok->{mlatin}); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | foreach (grep {$_->{hi} eq $dmtag && $_->{morph}} @{$tok->{rw}}) { | ||
| 130 | ##-- existing analyses: morph: from rewrite | ||
| 131 | $dm->{morph} = $dmtyp->{morph} = $_->{morph}; | ||
| 132 | last; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | ##-- oops... might need to re-analyze | ||
| 136 | $udmtypes->{$txt} = $dmtyp if (!$dmtyp->{morph} || !@{$dmtyp->{morph}}); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | ##-- analyze remaining dmoot types | ||
| 141 | if (!$standalone) { | ||
| 142 | my ($sublabel); | ||
| 143 | foreach (@{$asub->{chain}}) { | ||
| 144 | #$sublabel = $asub->{label}.'_'.$_->{label}; | ||
| 145 | $sublabel = $_->{label}; | ||
| 146 | next if (defined($opts->{$sublabel}) && !$opts->{$sublabel}); | ||
| 147 | $_->{label} =~ s/^\Q$asub->{label}_\E//; ##-- sanitize label ("dmoot_morph" --> "morph"), because it's also used as output key | ||
| 148 | $_->analyzeTypes($doc,$udmtypes,$opts); | ||
| 149 | $_->{label} = $sublabel; | ||
| 150 | } | ||
| 151 | |||
| 152 | ##-- delete dmoot target type 'text' | ||
| 153 | delete($_->{text}) foreach (values %$dmtypes); | ||
| 154 | |||
| 155 | ##-- re-expand dmoot target fields (morph,mlatin): UNKNOWN ONLY | ||
| 156 | foreach $tok (map {@{$_->{tokens}}} @{$doc->{body}}) { | ||
| 157 | next if (!defined($dm=$tok->{$dmkey}) | ||
| 158 | || !defined($dmtag=$dm->{tag}) | ||
| 159 | || !defined($txt=$tok->{xlit} ? $tok->{xlit}{latin1Text} : $tok->{text}) | ||
| 160 | #|| !defined($dmtyp=$udmtypes->{$dmtag}) | ||
| 161 | || !defined($dmtyp=$udmtypes->{$txt}) | ||
| 162 | ); | ||
| 163 | @$dm{keys %$dmtyp} = values %$dmtyp; | ||
| 164 | $dm->{morph} = [@{$dm->{morph}||[]}, @{$dm->{mlatin}}] if ($dm->{mlatin}); ##-- hack: adopt 'mlatin' into 'morph' | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | ##-- return | ||
| 169 | return $doc; | ||
| 170 | } | ||
| 171 | |||
| 172 | ## @keys = $anl->typeKeys() | ||
| 173 | ## + returns list of type-wise keys to be expanded for this analyzer by expandTypes() | ||
| 174 | ## + override returns empty list | ||
| 175 | sub typeKeys { | ||
| 176 | #return $_[0]{dmootLabel}; | ||
| 177 | return qw(); | ||
| 178 | } | ||
| 179 | |||
| 180 | ##------------------------------------------------------------------------ | ||
| 181 | ## Methods: Analysis: Generic | ||
| 182 | |||
| 183 | ## $bool = $ach->canAnalyze() | ||
| 184 | ## $bool = $ach->canAnalyze(\%opts) | ||
| 185 | ## + returns true if analyzer can perform its function (e.g. data is loaded & non-empty) | ||
| 186 | ## + override always returns 1 because of 'toka' hack | ||
| 187 | sub canAnalyze { return 1; } | ||
| 188 | |||
| 189 | sub canAnalyzeOLD { | ||
| 190 | my $ach = shift; | ||
| 191 | @{$ach->{chain}} = grep {$_ && $_->canAnalyze} @{$ach->chain(@_)}; | ||
| 192 | foreach (@{$ach->chain(@_)}) { | ||
| 193 | return 1 if ($_->canAnalyze); | ||
| 194 | } | ||
| 195 | return 1; | ||
| 196 | } | ||
| 197 | |||
| 198 | ## $bool = $anl->enabled(\%opts) | ||
| 199 | ## + returns $anl->{enabled} and disjunction over all sub-analyzers | ||
| 200 | ## + returns true if just $ach is enabled | ||
| 201 | sub enabled { | ||
| 202 | my $ach = shift; | ||
| 203 | return $ach->DTA::CAB::Analyzer::enabled(@_); #&& scalar(grep {$_->enabled(@_)} @{$ach->subAnalyzers(@_)}); | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | 1; ##-- be happy | ||
| 209 | |||
| 210 | __END__ |