| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::Dict::JsonDB.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: generic analysis dictionary API using JSON values | ||
| 6 | |||
| 7 | package DTA::CAB::Analyzer::Dict::JsonDB; | ||
| 8 | use DTA::CAB::Analyzer ':child'; | ||
| 9 | use DTA::CAB::Analyzer::Dict::Json; | ||
| 10 | use DTA::CAB::Analyzer::Dict::BDB; | ||
| 11 | use IO::File; | ||
| 12 | use Carp; | ||
| 13 | use Encode qw(encode decode); | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | ##============================================================================== | ||
| 17 | ## Globals | ||
| 18 | ##============================================================================== | ||
| 19 | |||
| 20 | our @ISA = qw(DTA::CAB::Analyzer::Dict::BDB DTA::CAB::Analyzer::Dict::Json); | ||
| 21 | |||
| 22 | ##============================================================================== | ||
| 23 | ## Constructors etc. | ||
| 24 | ##============================================================================== | ||
| 25 | |||
| 26 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 27 | ## + object structure: | ||
| 28 | ## ( | ||
| 29 | ## ##-- Filename Options | ||
| 30 | ## dictFile => $filename, ##-- filename (default=undef): should be TT-dict with JSON-encoded hash values | ||
| 31 | ## | ||
| 32 | ## ##-- Analysis Output | ||
| 33 | ## label => $lab, ##-- analyzer label | ||
| 34 | ## analyzeCode => $code, ##-- pseudo-accessor to perform actual analysis for token ($_); see DTA::CAB::Analyzer::Dict for details | ||
| 35 | ## | ||
| 36 | ## ##-- Analysis Options | ||
| 37 | ## encoding => $enc, ##-- encoding of db file: OVERRIDE DEFAULT: 'raw' | ||
| 38 | ## keyEncoding => $enc, ##-- NEW: encoding of db file keys (default='UTF-8') | ||
| 39 | ## | ||
| 40 | ## ##-- Analysis objects | ||
| 41 | ## dbf => $dbf, ##-- underlying Lingua::TT::DBFile object (default=undef) | ||
| 42 | ## dba => \%dba, ##-- args for Lingua::TT::DBFile->new() | ||
| 43 | ## #={ | ||
| 44 | ## # mode => $mode, ##-- default: 0644 | ||
| 45 | ## # dbflags => $flags, ##-- default: O_RDONLY | ||
| 46 | ## # type => $type, ##-- one of 'HASH', 'BTREE', 'RECNO', 'GUESS' (default: 'GUESS') | ||
| 47 | ## # dbinfo => \%dbinfo, ##-- default: "DB_File::${type}INFO"->new(); | ||
| 48 | ## # dbopts => \%opts, ##-- db options (e.g. cachesize,bval,...) -- defaults to none (uses DB_File defaults) | ||
| 49 | ## # } | ||
| 50 | ## ) | ||
| 51 | sub new { | ||
| 52 | my $that = shift; | ||
| 53 | my $dic = $that->DTA::CAB::Analyzer::Dict::BDB::new( | ||
| 54 | ##-- filenames | ||
| 55 | dictFile => undef, | ||
| 56 | |||
| 57 | ##-- options | ||
| 58 | encoding => 'raw', ##-- override | ||
| 59 | keyEncoding => 'UTF-8', | ||
| 60 | |||
| 61 | ##-- analysis output | ||
| 62 | label => 'dict_json', | ||
| 63 | analyzeCode => $DTA::CAB::Analyzer::Dict::Json::CODE_DEFAULT, | ||
| 64 | |||
| 65 | ##-- JSON parser (segfaults; see Analyzer::Dict::Json::jsonxs() method) | ||
| 66 | #jxs => __PACKAGE__->jsonxs, | ||
| 67 | |||
| 68 | ##-- user args | ||
| 69 | @_ | ||
| 70 | ); | ||
| 71 | return $dic; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | ##============================================================================== | ||
| 77 | ## Methods: Embedded API | ||
| 78 | ##============================================================================== | ||
| 79 | |||
| 80 | ##-------------------------------------------------------------- | ||
| 81 | ## Methods: I/O: Input: all | ||
| 82 | |||
| 83 | ## $bool = $dic->ensureLoaded() | ||
| 84 | ## + ensures analyzer data is loaded from default files | ||
| 85 | sub ensureLoaded { | ||
| 86 | my $dic = shift; | ||
| 87 | my $rc = $dic->DTA::CAB::Analyzer::Dict::BDB::ensureLoaded(@_); | ||
| 88 | |||
| 89 | ##-- shutoff the value filters | ||
| 90 | ## + BUG (2011-04-08, kaskade, perl 5.10.0): 'Undefined subroutine &main:: called' | ||
| 91 | ## - appears whenever we disable existing filters with filter_OP_KEYVAL(undef) | ||
| 92 | ## - workaround: pass 'raw' encoding into DB_File and install only the filters we really want here | ||
| 93 | ## - implemented as workaround 'keyEncoding','valEncoding' in Analyzer::Dict::BDB | ||
| 94 | # if ($rc && $dic->dictOk) { | ||
| 95 | # my $tied = $dic->{dbf}{tied}; | ||
| 96 | # $tied->filter_fetch_value(undef); ##-- BUG | ||
| 97 | # $tied->filter_store_value(undef); ##-- BUG | ||
| 98 | # } | ||
| 99 | |||
| 100 | return $rc; | ||
| 101 | } | ||
| 102 | |||
| 103 | ##============================================================================== | ||
| 104 | ## Methods: Persistence | ||
| 105 | ##============================================================================== | ||
| 106 | |||
| 107 | ##====================================================================== | ||
| 108 | ## Methods: Persistence: Perl | ||
| 109 | |||
| 110 | ## @keys = $class_or_obj->noSaveKeys() | ||
| 111 | ## + returns list of keys not to be saved | ||
| 112 | sub noSaveKeys { | ||
| 113 | my $that = shift; | ||
| 114 | return ($that->DTA::CAB::Analyzer::Dict::BDB::noSaveKeys, | ||
| 115 | $that->DTA::CAB::Analyzer::Dict::Json::noSaveKeys, | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | ## @keys = $class_or_obj->noSaveBinKeys() | ||
| 120 | sub noSaveBinKeys { | ||
| 121 | my $that = shift; | ||
| 122 | return ($that->DTA::CAB::Analyzer::Dict::BDB::noSaveBinKeys, | ||
| 123 | $that->DTA::CAB::Analyzer::Dict::Json::noSaveBinKeys, | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | ##============================================================================== | ||
| 129 | ## Methods: Analysis | ||
| 130 | ##============================================================================== | ||
| 131 | |||
| 132 | ##------------------------------------------------------------------------ | ||
| 133 | ## Methods: Analysis: v1.x: API | ||
| 134 | |||
| 135 | ## $doc = $anl->analyzeTypes($doc,\%types,\%opts) | ||
| 136 | ## + perform type-wise analysis of all (text) types in $doc->{types} | ||
| 137 | ## + INHERITED from Dict | ||
| 138 | |||
| 139 | ##------------------------------------------------------------------------ | ||
| 140 | ## Methods: Analysis: Utils | ||
| 141 | |||
| 142 | ## $prefix = $dict->analyzePre() | ||
| 143 | sub analyzePre { | ||
| 144 | my $dic = shift; | ||
| 145 | return $dic->DTA::CAB::Analyzer::Dict::Json::analyzePre(@_); | ||
| 146 | } | ||
| 147 | |||
| 148 | ## $coderef = $dict->analyzeCode() | ||
| 149 | ## $coderef = $dict->analyzeCode($code) | ||
| 150 | ## + inherited | ||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | 1; ##-- be happy | ||
| 155 | |||
| 156 | __END__ |