| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::TokPP::Waste.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: heuristic text-based analyzer (for punctutation, numbers, etc): moot/waste version | ||
| 6 | |||
| 7 | package DTA::CAB::Analyzer::TokPP::Waste; | ||
| 8 | |||
| 9 | use DTA::CAB::Analyzer; | ||
| 10 | use DTA::CAB::Datum ':all'; | ||
| 11 | use DTA::CAB::Token; | ||
| 12 | use Moot; | ||
| 13 | use Moot::Waste::Annotator; | ||
| 14 | |||
| 15 | use Encode qw(encode decode); | ||
| 16 | use IO::File; | ||
| 17 | use Carp; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | |||
| 21 | ##============================================================================== | ||
| 22 | ## Globals | ||
| 23 | ##============================================================================== | ||
| 24 | |||
| 25 | our @ISA = qw(DTA::CAB::Analyzer); | ||
| 26 | |||
| 27 | ##============================================================================== | ||
| 28 | ## Constructors etc. | ||
| 29 | ##============================================================================== | ||
| 30 | |||
| 31 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 32 | ## + %$obj, %args: | ||
| 33 | ## ( | ||
| 34 | ## label => 'tokpp', ##-- analyzer label | ||
| 35 | ## annot => $annot, ##-- underlying Moot::Waste::Annotator object | ||
| 36 | ## ) | ||
| 37 | sub new { | ||
| 38 | my $that = shift; | ||
| 39 | return $that->SUPER::new( | ||
| 40 | ##-- options | ||
| 41 | label => 'tokpp', | ||
| 42 | annot => undef, ##-- don't create here, else segfaults ensue on loadPerlString() -> loadPerlRef() calls | ||
| 43 | |||
| 44 | ##-- user args | ||
| 45 | @_ | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | ##============================================================================== | ||
| 50 | ## Methods: I/O | ||
| 51 | ##============================================================================== | ||
| 52 | |||
| 53 | ## $bool = $anl->ensureLoaded() | ||
| 54 | ## + ensures analysis data is loaded | ||
| 55 | ## + override just returns 1 | ||
| 56 | sub ensureLoaded { | ||
| 57 | my $anl = shift; | ||
| 58 | return ($anl->{loaded}=1); | ||
| 59 | } | ||
| 60 | |||
| 61 | ## $annot = $anl->annotator() | ||
| 62 | ## + returns cached Moot::Waste::Annotator $anl->{annot} or sets it | ||
| 63 | sub annotator { | ||
| 64 | return $_[0]{annot} if (defined($_[0]{annot})); | ||
| 65 | return $_[0]{annot} = Moot::Waste::Annotator->new(); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | ##====================================================================== | ||
| 70 | ## Methods: Persistence: Perl | ||
| 71 | |||
| 72 | ## @keys = $class_or_obj->noSaveKeys() | ||
| 73 | ## + returns list of keys not to be saved | ||
| 74 | ## + override appends 'annot' | ||
| 75 | sub noSaveKeys { | ||
| 76 | my $anl = shift; | ||
| 77 | return ($anl->SUPER::noSaveKeys(@_), qw(annot)); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub noSaveBinKeys { | ||
| 81 | return ($_[0]->SUPER::noSaveBinKeys(), $_[0]->noSaveKeys()); | ||
| 82 | } | ||
| 83 | |||
| 84 | ##============================================================================== | ||
| 85 | ## Methods: Analysis | ||
| 86 | ##============================================================================== | ||
| 87 | |||
| 88 | ## $doc = $tpp->analyzeTypes($doc,\%types,\%opts) | ||
| 89 | ## + perform type-wise analysis of all (text) types in values(%types) | ||
| 90 | ## + sets: | ||
| 91 | ## $tok->{$anl->{label}} = \@morphHiStrings | ||
| 92 | sub analyzeTypes { | ||
| 93 | my ($tpp,$doc,$types,$opts) = @_; | ||
| 94 | $types = $doc->types if (!$types); | ||
| 95 | my $akey = $tpp->{label}; | ||
| 96 | my $annot = $tpp->annotator(); | ||
| 97 | |||
| 98 | my ($tok,$a); | ||
| 99 | foreach $tok (values(%$types)) { | ||
| 100 | next if (defined($tok->{$akey})); ##-- avoid re-analysis | ||
| 101 | $a = $annot->annotate($tok)->{analyses}; | ||
| 102 | |||
| 103 | delete($tok->{$akey}); | ||
| 104 | $tok->{$akey} = [map {$_->{tag}} @$a] if ($a && @$a); | ||
| 105 | } | ||
| 106 | |||
| 107 | return $doc; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | 1; ##-- be happy | ||
| 112 | |||
| 113 | __END__ |