| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Format::ExpandList.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: Datum formatter: expansion list for use with DDC | ||
| 6 | |||
| 7 | package DTA::CAB::Format::ExpandList; | ||
| 8 | use DTA::CAB::Format; | ||
| 9 | use DTA::CAB::Format::TT; | ||
| 10 | use DTA::CAB::Datum ':all'; | ||
| 11 | use DTA::CAB::Utils ':data'; ##-- path_value() | ||
| 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::TT); | ||
| 22 | |||
| 23 | BEGIN { | ||
| 24 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, filenameRegex=>qr/\.(?i:xl|xlist|l|lst)$/); | ||
| 25 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'xl'); | ||
| 26 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'xlist'); | ||
| 27 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_, opts=>{keys=>[[qw(moot lemma)]]}) | ||
| 28 | foreach (qw(BestLemmaList BestLemma bllist bll bl bestlemma blemma lemma)); | ||
| 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, ##-- keys to include (default: [qw(text xlit eqpho eqrw eqlemma eqtagh gn-syn gn-isa gn-asi ot-syn ot-isa ot-asi)]) | ||
| 46 | ## | ||
| 47 | ## ##---- Common | ||
| 48 | ## utf8 => $bool, ##-- default: 1 | ||
| 49 | ## defaultFieldName => $name, ##-- default name for unnamed fields; parsed into @{$tok->{other}{$name}}; default='' | ||
| 50 | ## ) | ||
| 51 | ## + inherited from DTA::CAB::Format::TT | ||
| 52 | sub new { | ||
| 53 | my $that = shift; | ||
| 54 | my $obj = $that->SUPER::new(keys=>[qw(text xlit eqpho eqrw eqlemma eqtagh gn-syn gn-isa gn-asi ot-syn ot-isa ot-asi)],@_); | ||
| 55 | $obj->{keys} = [grep {($_//'') ne ''} split(/[\s\,]+/, $obj->{keys})] if (!ref($obj->{keys})); | ||
| 56 | return $obj; | ||
| 57 | } | ||
| 58 | |||
| 59 | ##============================================================================== | ||
| 60 | ## Methods: Persistence | ||
| 61 | ##============================================================================== | ||
| 62 | |||
| 63 | ## @keys = $class_or_obj->noSaveKeys() | ||
| 64 | ## + returns list of keys not to be saved: qw(doc outbuf) | ||
| 65 | ## + inherited from DTA::CAB::Format::TT | ||
| 66 | |||
| 67 | ##============================================================================== | ||
| 68 | ## Methods: Input | ||
| 69 | ##============================================================================== | ||
| 70 | |||
| 71 | ##-------------------------------------------------------------- | ||
| 72 | ## Methods: Input: Input selection | ||
| 73 | |||
| 74 | ## $fmt = $fmt->close() | ||
| 75 | ## + inherited from DTA::CAB::Format::TT | ||
| 76 | |||
| 77 | ## $fmt = $fmt->fromFile($filename_or_handle) | ||
| 78 | ## + default calls $fmt->fromFh() | ||
| 79 | |||
| 80 | ## $fmt = $fmt->fromFh($fh) | ||
| 81 | ## + default calls $fmt->fromString() on file contents | ||
| 82 | |||
| 83 | ## $fmt = $fmt->fromString($string) | ||
| 84 | ## + wrapper for: $fmt->close->parseTTString($_[0]) | ||
| 85 | ## + inherited from DTA::CAB::Format::TT | ||
| 86 | ## + name is aliased here to parseTextString() ! | ||
| 87 | |||
| 88 | ##-------------------------------------------------------------- | ||
| 89 | ## Methods: Input: Local | ||
| 90 | # (none) | ||
| 91 | |||
| 92 | ##-------------------------------------------------------------- | ||
| 93 | ## Methods: Input: Generic API | ||
| 94 | |||
| 95 | ## $doc = $fmt->parseDocument() | ||
| 96 | ## + just returns $fmt->{doc} | ||
| 97 | ## + inherited from DTA::CAB::Format::TT | ||
| 98 | |||
| 99 | |||
| 100 | ##============================================================================== | ||
| 101 | ## Methods: Output | ||
| 102 | ##============================================================================== | ||
| 103 | |||
| 104 | ##-------------------------------------------------------------- | ||
| 105 | ## Methods: Output: Generic | ||
| 106 | |||
| 107 | ## $type = $fmt->mimeType() | ||
| 108 | ## + default returns text/plain | ||
| 109 | sub mimeType { return 'text/plain'; } | ||
| 110 | |||
| 111 | ## $ext = $fmt->defaultExtension() | ||
| 112 | ## + returns default filename extension for this format | ||
| 113 | sub defaultExtension { return '.xl'; } | ||
| 114 | |||
| 115 | ##-------------------------------------------------------------- | ||
| 116 | ## Methods: Output: output selection | ||
| 117 | |||
| 118 | ##-------------------------------------------------------------- | ||
| 119 | ## Methods: Output: Generic API | ||
| 120 | |||
| 121 | ## $fmt = $fmt->putToken($tok) | ||
| 122 | ## + appends $tok to $fmt->{fh} | ||
| 123 | sub putToken { | ||
| 124 | my ($fmt,$tok) = @_; | ||
| 125 | my $level = $fmt->{level}||0; | ||
| 126 | my $sep = ($level>=2 ? "\n\t" | ||
| 127 | : ($level>=1 ? "\n" | ||
| 128 | : "\t")); | ||
| 129 | my @words = ( | ||
| 130 | grep {defined($_) && $_ ne ''} | ||
| 131 | map { | ||
| 132 | (UNIVERSAL::isa($_,'HASH') | ||
| 133 | ? (exists($_->{hi}) | ||
| 134 | ? $_->{hi} | ||
| 135 | : (exists($_->{latin1Text}) ? $_->{latin1Text} : $_)) | ||
| 136 | : $_) | ||
| 137 | } | ||
| 138 | map {UNIVERSAL::isa($_,'ARRAY') ? @$_ : $_} | ||
| 139 | map {path_value($tok,$_)} | ||
| 140 | @{$fmt->{keys}} | ||
| 141 | ); | ||
| 142 | |||
| 143 | $fmt->{fh}->print(join($sep, ($level > 0 ? sort(@words) : @words)), "\n"); | ||
| 144 | return $fmt; | ||
| 145 | } | ||
| 146 | |||
| 147 | ## $fmt = $fmt->putSentence($sent) | ||
| 148 | ## + concatenates formatted tokens *without* any sentence-comments | ||
| 149 | sub putSentence { | ||
| 150 | my ($fmt,$sent) = @_; | ||
| 151 | $fmt->putToken($_) foreach (@{toSentence($sent)->{tokens}}); | ||
| 152 | $fmt->{fh}->print("\n"); | ||
| 153 | return $fmt; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | 1; ##-- be happy | ||
| 158 | |||
| 159 | __END__ |