| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::TokPP::Perl.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: heuristic text-based analyzer (for punctutation, numbers, etc): pure-perl | ||
| 6 | |||
| 7 | package DTA::CAB::Analyzer::TokPP::Perl; | ||
| 8 | |||
| 9 | use DTA::CAB::Analyzer; | ||
| 10 | use DTA::CAB::Datum ':all'; | ||
| 11 | use DTA::CAB::Token; | ||
| 12 | |||
| 13 | use Encode qw(encode decode); | ||
| 14 | use IO::File; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | |||
| 19 | ##============================================================================== | ||
| 20 | ## Globals | ||
| 21 | ##============================================================================== | ||
| 22 | |||
| 23 | our @ISA = qw(DTA::CAB::Analyzer); | ||
| 24 | |||
| 25 | ##============================================================================== | ||
| 26 | ## Constructors etc. | ||
| 27 | ##============================================================================== | ||
| 28 | |||
| 29 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 30 | ## + %$obj, %args: | ||
| 31 | ## ( | ||
| 32 | ## label => 'tokpp', ##-- analyzer label | ||
| 33 | ## ) | ||
| 34 | sub new { | ||
| 35 | my $that = shift; | ||
| 36 | return $that->SUPER::new( | ||
| 37 | ##-- options | ||
| 38 | label => 'tokpp', | ||
| 39 | |||
| 40 | ##-- user args | ||
| 41 | @_ | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | |||
| 45 | ##============================================================================== | ||
| 46 | ## Methods: I/O | ||
| 47 | ##============================================================================== | ||
| 48 | |||
| 49 | ## $bool = $anl->ensureLoaded() | ||
| 50 | ## + ensures analysis data is loaded | ||
| 51 | ## + always returns 1 | ||
| 52 | sub ensureLoaded { | ||
| 53 | my $anl = shift; | ||
| 54 | return $anl->{loaded}=1; | ||
| 55 | } | ||
| 56 | |||
| 57 | ##============================================================================== | ||
| 58 | ## Methods: Analysis | ||
| 59 | ##============================================================================== | ||
| 60 | |||
| 61 | ## $doc = $tpp->analyzeTypes($doc,\%types,\%opts) | ||
| 62 | ## + perform type-wise analysis of all (text) types in values(%types) | ||
| 63 | ## + sets: | ||
| 64 | ## $tok->{$anl->{label}} = \@morphHiStrings | ||
| 65 | sub analyzeTypes { | ||
| 66 | my ($tpp,$doc,$types,$opts) = @_; | ||
| 67 | $types = $doc->types if (!$types); | ||
| 68 | my $akey = $tpp->{label}; | ||
| 69 | |||
| 70 | my ($tok,$w,@wa); | ||
| 71 | foreach $tok (values(%$types)) { | ||
| 72 | next if (defined($tok->{$akey})); ##-- avoid re-analysis | ||
| 73 | $w = $tok->{text}; | ||
| 74 | @wa = qw(); | ||
| 75 | |||
| 76 | if ($w =~ m(^[\.\!\?]+$)) { | ||
| 77 | push(@wa, '$.'); | ||
| 78 | } | ||
| 79 | elsif ($w =~ m(^[\,\;\-\¬]+$)) { | ||
| 80 | push(@wa, '$,'); | ||
| 81 | } | ||
| 82 | elsif ($w =~ m(^[[:punct:]]+$)) { | ||
| 83 | push(@wa, '$('); | ||
| 84 | } | ||
| 85 | elsif ($w =~ m([[:alpha:]])) { | ||
| 86 | if ($w =~ m(^[^\x{00}-\x{ff}]*$)) { | ||
| 87 | push(@wa, 'FM'); | ||
| 88 | } | ||
| 89 | if ($w =~ /\.$/ || length($w)<=1) { | ||
| 90 | push(@wa, 'XY'); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | elsif ($w =~ m(^[[:digit:]]+$)) { | ||
| 94 | push(@wa, 'CARD'); | ||
| 95 | } | ||
| 96 | elsif ($w =~ m(^[[:digit:][:punct:]]+$)) { | ||
| 97 | push(@wa, 'XY'); | ||
| 98 | } | ||
| 99 | elsif ($w =~ m([^\x{00}-\x{ff}])) { | ||
| 100 | push(@wa, 'XY'); | ||
| 101 | } | ||
| 102 | |||
| 103 | ##-- update token | ||
| 104 | delete($tok->{$akey}); | ||
| 105 | $tok->{$akey} = [@wa] if (@wa); | ||
| 106 | } | ||
| 107 | |||
| 108 | return $doc; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | 1; ##-- be happy | ||
| 113 | |||
| 114 | __END__ |