| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::EqLemma::CDB.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: DB dictionary-based equivalence-class expander, rewrite variant | ||
| 6 | |||
| 7 | package DTA::CAB::Analyzer::EqLemma::CDB; | ||
| 8 | use DTA::CAB::Analyzer ':child'; | ||
| 9 | use DTA::CAB::Analyzer::Dict::CDB; | ||
| 10 | use strict; | ||
| 11 | |||
| 12 | ##============================================================================== | ||
| 13 | ## Globals | ||
| 14 | ##============================================================================== | ||
| 15 | |||
| 16 | our @ISA = qw(DTA::CAB::Analyzer::Dict::CDB); | ||
| 17 | |||
| 18 | our $ANALYZE_GET_MOOT = '($_->{moot} ? $_->{moot}{lemma} : qw())'; | ||
| 19 | our $ANALYZE_GET_MORPH = '($_->{morph} ? (map {$_->{hi}} @{$_->{morph}}) : qw())'; | ||
| 20 | our $ANALYZE_GET_RW_MORPH = '($_->{rw} ? (map {$_->{morph} ? (map {$_->{lemma}} @{$_->{morph}}) : qw()} @{$_->{rw}}) : qw())'; | ||
| 21 | |||
| 22 | our $ANALYZE_GET_ALL = '('.join(",\n",$ANALYZE_GET_MOOT, $ANALYZE_GET_MORPH, $ANALYZE_GET_RW_MORPH).')'; | ||
| 23 | |||
| 24 | our $ANALYZE_GET_DEFAULT = $ANALYZE_GET_MOOT; | ||
| 25 | |||
| 26 | ##============================================================================== | ||
| 27 | ## Constructors etc. | ||
| 28 | ##============================================================================== | ||
| 29 | |||
| 30 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 31 | ## + object structure: see DTA::CAB::Analyzer::Dict::CDB | ||
| 32 | sub new { | ||
| 33 | my $that = shift; | ||
| 34 | return $that->SUPER::new( | ||
| 35 | ##-- options | ||
| 36 | label => 'eqlemma', | ||
| 37 | ## | ||
| 38 | analyzeGet => $ANALYZE_GET_DEFAULT, | ||
| 39 | ##analyzeSet => $DICT_SET_FST, ##-- NOT USED | ||
| 40 | ##allowRegex => '(?:^[[:alpha:]\-\¬]*[[:alpha:]]+$)|(?:^[[:alpha:]]+[[:alpha:]\-\¬]+(?:\.?)$)', | ||
| 41 | |||
| 42 | ##-- user args | ||
| 43 | @_ | ||
| 44 | ); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | ##======================================================================== | ||
| 49 | ## analysis overrides | ||
| 50 | |||
| 51 | ## $bool = $anl->doAnalyze(\%opts, $name) | ||
| 52 | sub doAnalyze { | ||
| 53 | my $anl = shift; | ||
| 54 | return 0 if ($_[1] eq 'Types'); | ||
| 55 | return $anl->SUPER::doAnalyze(@_); | ||
| 56 | } | ||
| 57 | |||
| 58 | ## $doc = $anl->analyzeTypes($doc,\%types,\%opts) | ||
| 59 | ## + perform type-wise analysis of all (text) types in $doc->{types} | ||
| 60 | ## + override does nothing | ||
| 61 | sub analyzeTypes { return $_[1]; } | ||
| 62 | |||
| 63 | ## $doc = $anl->analyzeSentences($doc,\%opts) | ||
| 64 | ## + perform sentence-wise analysis of all sentences $doc->{body}[$si] | ||
| 65 | ## + expand lemma equivalence | ||
| 66 | sub analyzeSentences { | ||
| 67 | my ($anl,$doc,$opts) = @_; | ||
| 68 | |||
| 69 | ##-- common vars | ||
| 70 | my $lab = $anl->{label}; | ||
| 71 | my $lab_key = $lab.'_key'; | ||
| 72 | |||
| 73 | ##-- dict structures | ||
| 74 | my $dhash = $anl->dictHash; | ||
| 75 | my $allow_re = defined($anl->{allowRegex}) ? qr($anl->{allowRegex}) : undef; | ||
| 76 | |||
| 77 | ##-- accessors | ||
| 78 | my $aget = $anl->analyzeCode($anl->{analyzeGet}); | ||
| 79 | |||
| 80 | ##-- get lemma types | ||
| 81 | my $ltypes = {}; | ||
| 82 | my ($tok,@lw,%lw,$l,$ltyp,$lw); | ||
| 83 | foreach $tok (map {@{$_->{tokens}}} @{$doc->{body}}) { | ||
| 84 | next if (defined($allow_re) && $tok->{text} !~ $allow_re); | ||
| 85 | @lw = qw(); | ||
| 86 | foreach $l ($aget->($tok)) { | ||
| 87 | if (!defined($ltyp=$ltypes->{$l})) { | ||
| 88 | $ltyp = $ltypes->{$l} = {lemma=>$l}; | ||
| 89 | $lw = $dhash->{$l}; | ||
| 90 | $ltyp->{$lab} = [({lo=>$l,hi=>$l,w=>0}), | ||
| 91 | (map {$_->{lo}=$l; $_} | ||
| 92 | map {DTA::CAB::Analyzer::parseFstString($_)} | ||
| 93 | grep {defined($_)} | ||
| 94 | split(/\t/,$lw)) | ||
| 95 | ] | ||
| 96 | if (defined($lw) && $lw ne ''); | ||
| 97 | } | ||
| 98 | push(@lw, @{$ltyp->{$lab}}) if ($ltyp->{$lab}); | ||
| 99 | } | ||
| 100 | |||
| 101 | %lw=qw(); | ||
| 102 | foreach (@lw) { | ||
| 103 | $lw{$_->{hi}} = $_ if (!exists($lw{$_->{hi}}) || $lw{$_->{hi}}{w} > $_->{w}); | ||
| 104 | } | ||
| 105 | delete($tok->{$lab}); | ||
| 106 | $tok->{$lab} = [sort {($a->{w}||0) <=> ($b->{w}||0) || ($a->{hi}||"") cmp ($b->{hi}||"")} values %lw]; | ||
| 107 | } | ||
| 108 | |||
| 109 | ##-- return | ||
| 110 | return $doc; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | 1; ##-- be happy | ||
| 115 | |||
| 116 | __END__ |