| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::TextPhonetic.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: phonetic digest analysis using Text::Phonetic | ||
| 6 | |||
| 7 | package DTA::CAB::Analyzer::TextPhonetic; | ||
| 8 | use DTA::CAB::Analyzer; | ||
| 9 | use Carp; | ||
| 10 | use strict; | ||
| 11 | our @ISA = qw(DTA::CAB::Analyzer); | ||
| 12 | |||
| 13 | ##============================================================================== | ||
| 14 | ## Globals | ||
| 15 | |||
| 16 | ## $DEFAULT_ANALYZE_GET | ||
| 17 | ## + default coderef or eval-able string for {analyzeGet} | ||
| 18 | ## + eval()d in list context, may return multiples | ||
| 19 | ## + parameters: | ||
| 20 | ## $_[0] => token object being analyzed | ||
| 21 | ## + closure vars: | ||
| 22 | ## $anl => analyzer (automaton) | ||
| 23 | our $DEFAULT_ANALYZE_GET = '$_[0]{xlit} ? $_[0]{xlit}{latin1Text} : $_[0]{text}'; | ||
| 24 | |||
| 25 | |||
| 26 | ##============================================================================== | ||
| 27 | ## Methods: Constructors etc. | ||
| 28 | |||
| 29 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 30 | ## + object structure, %args | ||
| 31 | ## ( | ||
| 32 | ## alg => $alg, ##-- Text::Phonetic subclass, e.g. 'Soundex','Koeln','Metaphone' (default='Soundex') | ||
| 33 | ## tpo => $obj, ##-- underlying Text::Phonetic::Whatever object | ||
| 34 | ## analyzeGet => $codestr, ##-- accessor: coderef or string: source text (default=$DEFAULT_ANALYZE_GET) | ||
| 35 | ## ) | ||
| 36 | sub new { | ||
| 37 | my $that = shift; | ||
| 38 | my $tp = $that->SUPER::new( | ||
| 39 | ##-- defaults | ||
| 40 | alg => 'Soundex', | ||
| 41 | tpo => undef, ##-- see ensureLoaded() | ||
| 42 | analyzeGet => $DEFAULT_ANALYZE_GET, | ||
| 43 | |||
| 44 | ##-- analysis selection | ||
| 45 | label => 'tpho', | ||
| 46 | |||
| 47 | ##-- user args | ||
| 48 | @_ | ||
| 49 | ); | ||
| 50 | return $tp; | ||
| 51 | } | ||
| 52 | |||
| 53 | ## $bool = $anl->ensureLoaded() | ||
| 54 | ## + ensures analysis data is loaded from default files | ||
| 55 | ## + default version always returns true | ||
| 56 | sub ensureLoaded { | ||
| 57 | my $tp = shift; | ||
| 58 | return 1 if ($tp->{tpo}); | ||
| 59 | eval "use Text::Phonetic; use Text::Phonetic::$tp->{alg};"; | ||
| 60 | if ($@ || !$INC{"Text/Phonetic/$tp->{alg}.pm"}) { | ||
| 61 | $tp->logwarn("cannot use Text::Phonetic::$tp->{alg}: $@"); | ||
| 62 | return 0; | ||
| 63 | } | ||
| 64 | $tp->info("using Text::Phonetic version ", ($Text::Phonetic::VERSION || '-undef-')); | ||
| 65 | $tp->{tpo} = "Text::Phonetic::$tp->{alg}"->new() | ||
| 66 | or $tp->logwarn("cannot create Text::Phonetic::$tp->{alg} object"); | ||
| 67 | return $tp; | ||
| 68 | } | ||
| 69 | |||
| 70 | ## @keys = $class_or_obj->noSaveKeys() | ||
| 71 | ## + returns list of keys not to be saved | ||
| 72 | ## + default just greps for CODE-refs | ||
| 73 | sub noSaveKeys { | ||
| 74 | my $tp = shift; | ||
| 75 | return ($tp->SUPER::noSaveKeys, 'tpo'); | ||
| 76 | } | ||
| 77 | |||
| 78 | ##============================================================================== | ||
| 79 | ## Methods: Analysis: v1.x | ||
| 80 | |||
| 81 | ##------------------------------------------------------------------------ | ||
| 82 | ## Methods: Analysis: v1.x: Utils | ||
| 83 | |||
| 84 | ## $bool = $anl->canAnalyze(); | ||
| 85 | ## + returns true iff analyzer can perform its function (e.g. data is loaded & non-empty) | ||
| 86 | ## + default implementation always returns true | ||
| 87 | sub canAnalyze { return $_[0]{tpo} ? 1 : 0; } | ||
| 88 | |||
| 89 | |||
| 90 | ## $doc = $anl->analyzeTypes($doc,\%types,\%opts) | ||
| 91 | ## + perform type-wise analysis of all (text) types in $doc->{types} | ||
| 92 | ## + default implementation does nothing | ||
| 93 | sub analyzeTypes { | ||
| 94 | my ($tp,$doc,$types,$opts) = @_; | ||
| 95 | $types = $doc->types if (!$types); | ||
| 96 | |||
| 97 | ##-- common variables | ||
| 98 | my $tpo = $tp->{tpo}; | ||
| 99 | my $label = $tp->{label}; | ||
| 100 | my $aget = $tp->accessClosure(defined($tp->{analyzeGet}) ? $tp->{analyzeGet} : $DEFAULT_ANALYZE_GET); | ||
| 101 | |||
| 102 | my ($tok,$txt,$pho); | ||
| 103 | foreach $tok (values(%$types)) { | ||
| 104 | $txt = $aget->($tok); | ||
| 105 | $pho = $tpo->encode(defined($txt) ? $txt : ''); | ||
| 106 | $tok->{$label} = $pho; | ||
| 107 | } | ||
| 108 | return $doc; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | 1; ##-- be happy | ||
| 114 | |||
| 115 | __END__ |