| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Format::ExpandList.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: Datum formatter: lemma-list (closed classes) or best-lemma (open classes) for use with DDC | ||
| 6 | |||
| 7 | package DTA::CAB::Format::LemmaList; | ||
| 8 | use DTA::CAB::Format; | ||
| 9 | use DTA::CAB::Format::TT; | ||
| 10 | use DTA::CAB::Format::ExpandList; | ||
| 11 | use DTA::CAB::Datum ':all'; | ||
| 12 | use IO::File; | ||
| 13 | use Encode qw(encode decode); | ||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | ##============================================================================== | ||
| 18 | ## Globals | ||
| 19 | ##============================================================================== | ||
| 20 | |||
| 21 | our @ISA = qw(DTA::CAB::Format::ExpandList); | ||
| 22 | |||
| 23 | BEGIN { | ||
| 24 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_, filenameRegex=>qr/\.(?i:ll|llist|lemma)/) | ||
| 25 | foreach (qw(LemmaList llist ll lemma)); | ||
| 26 | |||
| 27 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_, opts=>{cctagre=>''}) | ||
| 28 | foreach (qw(LemmaListAll LemmasAll llist-all ll-all lla lemmas lemmata)); | ||
| 29 | } | ||
| 30 | |||
| 31 | ##============================================================================== | ||
| 32 | ## Constructors etc. | ||
| 33 | ##============================================================================== | ||
| 34 | |||
| 35 | ## $fmt = CLASS_OR_OBJ->new(%args) | ||
| 36 | ## + object structure: assumed HASH | ||
| 37 | ## ( | ||
| 38 | ## ##---- Input | ||
| 39 | ## doc => $doc, ##-- buffered input document (TT-style) | ||
| 40 | ## | ||
| 41 | ## ##---- Output | ||
| 42 | ## level => $formatLevel, ##-- output formatting level: | ||
| 43 | ## ## 0:TAB-separated (default); 1:sorted,NEWLINE-separated; 2:sorted,NEWLINE+TAB separated | ||
| 44 | ## #outbuf => $stringBuffer, ##-- buffered output | ||
| 45 | ## keys => \@expandKeys, ##-- IGNORED: keys to include (default: [qw(text xlit eqpho eqrw eqlemma eqtagh gn-syn gn-isa gn-asi ot-syn ot-isa ot-asi)]) | ||
| 46 | ## cctagre => $cctagre, ##-- regex matching closed-class tags (default='^(?:[CKP\$]|A[PR]|V[AM])', for STTS) | ||
| 47 | ## | ||
| 48 | ## | ||
| 49 | ## ##---- Common | ||
| 50 | ## utf8 => $bool, ##-- default: 1 | ||
| 51 | ## defaultFieldName => $name, ##-- default name for unnamed fields; parsed into @{$tok->{other}{$name}}; default='' | ||
| 52 | ## ) | ||
| 53 | ## + inherited from DTA::CAB::Format::TT | ||
| 54 | sub new { | ||
| 55 | my $that = shift; | ||
| 56 | my $obj = $that->SUPER::new( | ||
| 57 | keys=>[], | ||
| 58 | cctagre=>'^(?:[CKP\$]|A[PR]|V[AM])', | ||
| 59 | @_); | ||
| 60 | |||
| 61 | return $obj; | ||
| 62 | } | ||
| 63 | |||
| 64 | ##============================================================================== | ||
| 65 | ## Methods: Persistence | ||
| 66 | ##============================================================================== | ||
| 67 | |||
| 68 | ## @keys = $class_or_obj->noSaveKeys() | ||
| 69 | ## + returns list of keys not to be saved: qw(doc outbuf) | ||
| 70 | ## + inherited from DTA::CAB::Format::TT | ||
| 71 | sub noSaveKeys { | ||
| 72 | my $that = shift; | ||
| 73 | return ($that->SUPER::noSaveKeys(), 'cctagre'); | ||
| 74 | } | ||
| 75 | |||
| 76 | ##============================================================================== | ||
| 77 | ## Methods: Input | ||
| 78 | ##============================================================================== | ||
| 79 | |||
| 80 | ##-------------------------------------------------------------- | ||
| 81 | ## Methods: Input: Input selection | ||
| 82 | |||
| 83 | ## $fmt = $fmt->close() | ||
| 84 | ## + inherited from DTA::CAB::Format::TT | ||
| 85 | |||
| 86 | ## $fmt = $fmt->fromFile($filename_or_handle) | ||
| 87 | ## + default calls $fmt->fromFh() | ||
| 88 | |||
| 89 | ## $fmt = $fmt->fromFh($fh) | ||
| 90 | ## + default calls $fmt->fromString() on file contents | ||
| 91 | |||
| 92 | ## $fmt = $fmt->fromString($string) | ||
| 93 | ## + wrapper for: $fmt->close->parseTTString($_[0]) | ||
| 94 | ## + inherited from DTA::CAB::Format::TT | ||
| 95 | ## + name is aliased here to parseTextString() ! | ||
| 96 | |||
| 97 | ##-------------------------------------------------------------- | ||
| 98 | ## Methods: Input: Local | ||
| 99 | # (none) | ||
| 100 | |||
| 101 | ##-------------------------------------------------------------- | ||
| 102 | ## Methods: Input: Generic API | ||
| 103 | |||
| 104 | ## $doc = $fmt->parseDocument() | ||
| 105 | ## + just returns $fmt->{doc} | ||
| 106 | ## + inherited from DTA::CAB::Format::TT | ||
| 107 | |||
| 108 | |||
| 109 | ##============================================================================== | ||
| 110 | ## Methods: Output | ||
| 111 | ##============================================================================== | ||
| 112 | |||
| 113 | ##-------------------------------------------------------------- | ||
| 114 | ## Methods: Output: Generic | ||
| 115 | |||
| 116 | ## $type = $fmt->mimeType() | ||
| 117 | ## + default returns text/plain | ||
| 118 | sub mimeType { return 'text/plain'; } | ||
| 119 | |||
| 120 | ## $ext = $fmt->defaultExtension() | ||
| 121 | ## + returns default filename extension for this format | ||
| 122 | sub defaultExtension { return '.ll'; } | ||
| 123 | |||
| 124 | ##-------------------------------------------------------------- | ||
| 125 | ## Methods: Output: output selection | ||
| 126 | |||
| 127 | ##-------------------------------------------------------------- | ||
| 128 | ## Methods: Output: Generic API | ||
| 129 | |||
| 130 | ## $fmt = $fmt->putToken($tok) | ||
| 131 | ## + appends $tok to $fmt->{fh} | ||
| 132 | sub putToken { | ||
| 133 | my ($fmt,$tok) = @_; | ||
| 134 | my $level = $fmt->{level}||0; | ||
| 135 | my $sep = ($level>=2 ? "\n\t" | ||
| 136 | : ($level>=1 ? "\n" | ||
| 137 | : "\t")); | ||
| 138 | my $cctagre = $fmt->{cctagre} // ''; | ||
| 139 | $cctagre = qr{$cctagre} if (!ref($cctagre)); | ||
| 140 | |||
| 141 | return $fmt if (!$tok->{moot}); | ||
| 142 | my (@lemmas); | ||
| 143 | |||
| 144 | if ($tok->{moot}{tag} =~ $cctagre) { | ||
| 145 | ##-- closed-class word: return all lemmata | ||
| 146 | my $tmp=''; | ||
| 147 | @lemmas = ( | ||
| 148 | map {$tmp eq $_ ? qw() : ($tmp=$_)} | ||
| 149 | sort | ||
| 150 | grep {defined($_) && $_ ne ''} | ||
| 151 | map { $_->{lemma} } | ||
| 152 | $tok->{moot}, | ||
| 153 | @{$tok->{moot}{analyses}//[]} | ||
| 154 | ); | ||
| 155 | } else { | ||
| 156 | ##-- lexical word: return only best lemma | ||
| 157 | @lemmas = ($tok->{moot}{lemma}); | ||
| 158 | } | ||
| 159 | |||
| 160 | $fmt->{fh}->print(join($sep, ($level > 0 ? sort(@lemmas) : @lemmas)), "\n"); | ||
| 161 | return $fmt; | ||
| 162 | } | ||
| 163 | |||
| 164 | ## $fmt = $fmt->putSentence($sent) | ||
| 165 | ## + concatenates formatted tokens *without* any sentence-comments | ||
| 166 | ## + INHERITED fromk ExpandList | ||
| 167 | |||
| 168 | |||
| 169 | 1; ##-- be happy | ||
| 170 | |||
| 171 | __END__ |