CAB/Format/TJ.pm

filter: Statementstmt 23.6% 22 / 93 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 33.3% 7 / 21 Podpod 71.4% 10 / 14 totaltotal 18.8% 39 / 207 CC 33 SCAR 49.6
CC
33
Cov
14%
parseTJFh
380.0
putToken
30.0
putSentence
20.0
CRAP 143.0
« CAB/Format/SQLite.pm ↑ Summary CAB/Format/XmlLing.pm »
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Format::TJ.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: Datum parser: one-token-per-line text
6  
7 package DTA::CAB::Format::TJ;
8 1 ▶use DTA::CAB::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DTA::CAB::Format::TT;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DTA::CAB::Datum ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ##==============================================================================
16 ## Globals
17 ##==============================================================================
18  
19 1 our @ISA = qw(DTA::CAB::Format::TT);
20  
21 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 DTA::CAB::Format->registerFormat(name=>__PACKAGE__, filenameRegex=>qr/\.(?i:tj|tjson|cab\-tj|cab\-tjson)$/);
23 }
24  
25 ##==============================================================================
26 ## Constructors etc.
27 ##==============================================================================
28  
29 ## $fmt = CLASS_OR_OBJ->new(%args)
30 ## + object structure: assumed HASH
31 ## {
32 ## ##-- Input
33 ## doc => $doc, ##-- buffered input document
34 ##
35 ## ##-- Output
36 ## #outbuf => $stringBuffer, ##-- buffered output
37 ## level => $formatLevel, ##-- <0:no 'text' attribute; >=0: all attributes; abs($_)>=2: canonical
38 ##
39 ## ##-- Common
40 ## raw => $bool, ##-- attempt to load/save raw data
41 ## defaultFieldName => $name, ##-- default name for unnamed fields; parsed into @{$tok->{other}{$name}}; default=''
42 ## }
43  
44 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
45 0 my $that = shift;
46 0 ▶ my $fmt = bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
47 ##-- input
48 doc => undef,
49  
50 ##-- output
51 #outbuf => '',
52 level => 0,
53  
54 ##-- common
55 utf8 => 1,
56 defaultFieldName => '',
57  
58 ##-- user args
59 @_
60 }, ref($that)||$that);
61 0 return $fmt;
62 }
63  
64 ##==============================================================================
65 ## Methods: Persistence
66 ##==============================================================================
67  
68 ## @keys = $class_or_obj->noSaveKeys()
69 ## + returns list of keys not to be saved
70 ## + default just returns empty list
71 0 ▶sub noSaveKeys {
Subroutine noSaveKeys: not called CC=1 SCAR=6.9
Pod documented
72 0 return ($_[0]->SUPER::noSaveKeys, qw(doc outbuf jxs));
73 }
74  
75 ##==============================================================================
76 ## Methods: I/O: Generic
77 ##==============================================================================
78  
79 ## $jxs = $fmt->jsonxs()
80 0 ▶sub jsonxs {
Subroutine jsonxs: not called CC=3 SCAR=24.8
Pod undocumented
81 0 require JSON::XS;
82 0 ▶ return $_[0]{jxs} if (defined($_[0]{jxs}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"jxs"} 0 0 0
83 0 ▶ return $_[0]{jxs} = JSON::XS->new->utf8(0)->relaxed(1)->canonical(abs($_[0]{level})>=2 ? 1 : 0)->allow_blessed(1)->convert_blessed(1);
Branch: 1 branch
BranchTrueFalseTotal
abs $_[0]{'level'} >= 2 ? : 0 0 0
84 }
85  
86 ##==============================================================================
87 ## Methods: I/O: Block-wise
88 ##==============================================================================
89  
90 ## \%head = blockScanHead(\$buf,\%opts)
91 ## + gets header offset, length from (mmaped) \$buf
92 ## + %opts are as for blockScan()
93 0 ▶sub blockScanHead {
Subroutine blockScanHead: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my ($fmt,$bufr,$opts) = @_;
95 0 ▶ return [0,$+[0]] if ($$bufr =~ m(\A\n*+(?:%%\$TJ:DOC=.*\n++)?));
Branch: 1 branch
BranchTrueFalseTotal
if $$bufr =~ /\A\n*+(?:%%\$TJ:DOC=.*\n++)?/ 0 0 0
96 0 return [0,0];
97 }
98  
99  
100 ##==============================================================================
101 ## Methods: Input
102 ##==============================================================================
103  
104 ##--------------------------------------------------------------
105 ## Methods: Input: Input selection
106  
107 ## $fmt = $fmt->fromFh($filename_or_handle)
108 ## + new override calls Format::fromFh
109 0 ▶sub fromFh {
Subroutine fromFh: not called CC=2 SCAR=17.9
Pod documented
110 #return $_[0]->fromFh_str(@_[1..$#_]);
111 0 my $fmt = shift;
112 0 ▶ $fmt->DTA::CAB::Format::fromFh(@_)
Branch: 1 branch
BranchTrueFalseTotal
$fmt->DTA::CAB::Format::fromFh(@_) or $fmt->logconfess("fromFh(): inherited Format::fromFh() failed: $!") 0 0 0
113 or $fmt->logconfess("fromFh(): inherited Format::fromFh() failed: $!");
114 0 return $fmt->parseTJFh($_[0]);
115 }
116  
117 ## $fmt = $fmt->fromString(\$string)
118 ## + select input from string $string
119 ## + new override calls Format::fromString() [-> fromFh]
120 0 ▶sub fromString {
Subroutine fromString: not called CC=1 SCAR=6.9
Pod documented
121 0 my $fmt = shift;
122 0 $fmt->close();
123 #return $fmt->parseTJString(ref($_[0]) ? $_[0] : \$_[0]);
124 0 return $fmt->DTA::CAB::Format::fromString(@_);
125 }
126  
127 ##--------------------------------------------------------------
128 ## Methods: Input: Local
129  
130 ## $fmt = $fmt->parseTJFh($fh)
131 ## + guts for fromFh(): parse handle $fh into local document buffer.
132 0 ▶sub parseTJFh {
Subroutine parseTJFh: not called CC=19 SCAR=59.4
Pod undocumented
133 0 my ($fmt,$fh) = @_;
134 0 $fmt->setLayers($fh);
135 0 my $jxs = $fmt->jsonxs();
136  
137 ##-- ye olde loope
138 0 my (%sa,%doca);
139 0 my $toks = [];
140 0 my @body = qw();
141 0 my ($tok,$text,$json);
142 0 ▶ while (defined($_=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
143 0 ▶ if ($_ =~ /^\%\%\$TJ\:DOC=(.+)$/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($_ =~ /^\%\%\$TJ\:DOC=(.+)$/) { } 0 0 0
elsif ($_ =~ /^\%\%\$TJ\:SENT=(.+)$/) { } 0 0 0
elsif ($_ =~ /^\%\% (?:xml\:)?base=(.*)$/) { } 0 0 0
elsif ($_ =~ /^\%\% Sentence (.*)$/) { } 0 0 0
elsif ($_ =~ /^\%\%(.*)$/) { } 0 0 0
elsif ($_ =~ /^$/) { } 0 0 0
144 ##-- tj directive: document attributes
145 0 ▶ $json = defined($1) && $1 ? $jxs->decode($1) : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && $1 ? : 0 0 0
Condition0%0/3
defined $1 and $1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $1 and $1
defined $1 $1
Truth table0%0/3
A and B
A= defined $1B= $1
ABresult
0X0
100
111
146 0 @doca{keys %$json} = values %$json;
147 }
148 elsif ($_ =~ /^\%\%\$TJ\:SENT=(.+)$/) {
149 ##-- tj directive: sentence attributes
150 0 ▶ $json = defined($1) && $1 ? $jxs->decode($1) : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && $1 ? : 0 0 0
Condition0%0/3
defined $1 and $1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $1 and $1
defined $1 $1
Truth table0%0/3
A and B
A= defined $1B= $1
ABresult
0X0
100
111
151 0 @sa{keys %$json} = values %$json;
152 }
153 elsif ($_ =~ /^\%\% (?:xml\:)?base=(.*)$/) {
154 ##-- (tt-compat) special comment: document attribute: xml:base
155 0 $doca{'base'} = $1;
156 }
157 elsif ($_ =~ /^\%\% Sentence (.*)$/) {
158 ##-- (tt-compat) special comment: sentence attribute: xml:id
159 0 $sa{'id'} = $1;
160 }
161 elsif ($_ =~ /^\%\%(.*)$/) {
162 ##-- (tt-compat) generic line: add to _cmts
163 0 push(@{$sa{_cmts}},$1); ##-- generic doc- or sentence-level comment
164 }
165 elsif ($_ =~ /^$/) {
166 ##-- empty line: EOS
167 0 ▶ if (%sa || @$toks) {
Branch: 1 branch
BranchTrueFalseTotal
if (%sa or @$toks) 0 0 0
Condition0%0/3
%sa or @$toks
l!l&&r!l&&!r
000
MC/DC0%0/2
%sa or @$toks
%sa @$toks
Truth table0%0/3
A or B
A= %saB= @$toks
ABresult
1X1
011
000
168 0 push(@body,{%sa,tokens=>$toks});
169 0 $toks = [];
170 0 %sa = qw();
171 }
172 }
173 else {
174 ##-- vanilla token
175 0 chomp;
176 0 ($text,$json) = split(/\t/,$_,2);
177 0 ▶ push(@$toks, $tok = (defined($json) && $json ne '' ? $jxs->decode($json) : {}));
Branch: 1 branch
BranchTrueFalseTotal
defined $json && $json ne '' ? : 0 0 0
Condition0%0/3
defined $json and $json ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json and $json ne ""
defined $json $json ne ""
Truth table0%0/3
A and B
A= defined $jsonB= $json ne ""
ABresult
0X0
100
111
178 0 ▶ $tok->{text}=$text if (!defined($tok->{text}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tok->{"text"} 0 0 0
179 }
180 }
181 0 ▶ push(@body, {%sa,tokens=>$toks}) if (%sa || @$toks); ##-- handle missing EOS at EOF
Branch: 1 branch
BranchTrueFalseTotal
if %sa or @$toks 0 0 0
Condition0%0/3
%sa or @$toks
l!l&&r!l&&!r
000
MC/DC0%0/2
%sa or @$toks
%sa @$toks
Truth table0%0/3
A or B
A= %saB= @$toks
ABresult
1X1
011
000
182  
183 ##-- construct & buffer output document
184 #$_ = bless($_,'DTA::CAB::Sentence') foreach (@$sents);
185 0 $fmt->{doc} = bless({%doca,body=>\@body}, 'DTA::CAB::Document');
186 0 return $fmt;
187 }
188  
189  
190 ##--------------------------------------------------------------
191 ## Methods: Input: Generic API
192  
193 ## $doc = $fmt->parseDocument()
194 0 ▶sub parseDocument { return $_[0]{doc}; }
Subroutine parseDocument: not called CC=1 SCAR=6.9
Pod documented
195  
196  
197 ##==============================================================================
198 ## Methods: Output
199 ##==============================================================================
200  
201 ##--------------------------------------------------------------
202 ## Methods: Output: Generic
203  
204 ## $type = $fmt->mimeType()
205 ## + default returns text/plain
206 0 ▶sub mimeType { return 'text/plain'; }
Subroutine mimeType: not called CC=1 SCAR=6.9
Pod documented
207  
208 ## $ext = $fmt->defaultExtension()
209 ## + returns default filename extension for this format
210 0 ▶sub defaultExtension { return '.tj'; }
Subroutine defaultExtension: not called CC=1 SCAR=6.9
Pod documented
211  
212 ##--------------------------------------------------------------
213 ## Methods: Output: output selection
214 ## + inherited
215  
216  
217 ##--------------------------------------------------------------
218 ## Methods: Output: Generic API
219  
220 ## $fmt = $fmt->putToken($tok)
221 0 ▶sub putToken {
Subroutine putToken: not called CC=5 SCAR=34.0
Pod documented
222 #my ($fmt,$tok) = @_;
223  
224 $_[0]{fh}->print
225 (
226 0 ▶ ($_[1]{_cmts} ? join('', map {"%%$_\n"} map {split(/\n/,$_)} @{$_[1]{_cmts}}) : ''),
Branch: 1 branch
BranchTrueFalseTotal
($_[0]{'level'} || 0) >= 0 ? : 0 0 0
Condition0%0/2
$_[0]{'level'} || 0
l!l
00
MC/DC0%0/1
$_[0]{'level'} || 0
$_[0]{'level'}
Truth table0%0/2
A
A= $_[0]{'level'}
Aresult
11
00
227 $_[1]{text},
228 "\t",
229 $_[0]->jsonxs->encode(($_[0]{level}||0) >= 0
230 ? $_[1]
231 0 ▶ : {(map {$_ eq 'text' ? qw() : ($_=>$_[1]{$_})} keys %{$_[1]})}
Branch: 2 branches
BranchTrueFalseTotal
$_[1]{'_cmts'} ? : 0 0 0
$_ eq 'text' ? : 0 0 0
232 ),
233 "\n",
234 );
235  
236 0 return $_[0];
237 }
238  
239 ## $fmt = $fmt->putSentence($sent)
240 ## + concatenates formatted tokens, adding sentence-id comment if available
241 0 ▶sub putSentence {
Subroutine putSentence: not called CC=4 SCAR=30.0
Pod documented
242 #my ($fmt,$sent) = @_;
243 0 ▶ my $sh = {(map {$_ eq 'tokens' ? qw() : ($_=>$_[1]{$_})} keys %{$_[1]})};
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'tokens' ? : 0 0 0
244 0 ▶ $_[0]{fh}->print('%%$TJ:SENT=', $_[0]->jsonxs->encode($sh), "\n") if (%$sh);
Branch: 1 branch
BranchTrueFalseTotal
if %$sh 0 0 0
245 0 $_[0]->putToken($_) foreach (@{toSentence($_[1])->{tokens}});
246 0 $_[0]{fh}->print("\n");
247 0 return $_[0];
248 }
249  
250 ## $fmt = $fmt->putDocument($doc)
251 ## + concatenates formatted sentences, adding document 'xmlbase' comment if available
252 1 our %TJ_BAD_DOC_KEYS = (body=>1, teibufr=>1, textbufr=>1);
253 0 ▶sub putDocument {
Subroutine putDocument: not called CC=3 SCAR=24.8
Pod documented
254 #my ($fmt,$doc) = @_;
255 0 my $dh = { (map {($_=>$_[1]{$_})} grep {!exists($TJ_BAD_DOC_KEYS{$_})} keys %{$_[1]}) };
256 0 ▶ $_[0]{fh}->print('%%$TJ:DOC=', $_[0]->jsonxs->encode($dh), "\n") if (%$dh);
Branch: 1 branch
BranchTrueFalseTotal
if %$dh 0 0 0
257 0 $_[0]->putSentence($_) foreach (@{toDocument($_[1])->{body}});
258 0 return $_[0];
259 }
260  
261  
262 ## $fmt = $fmt->putData($data)
263 ## + puts raw data (json)
264 0 ▶sub putData {
Subroutine putData: not called CC=1 SCAR=6.9
Pod undocumented
265 0 $_[0]{fh}->print($_[0]->jsonxs->encode($_[1]));
266 }
267  
268  
269 1 1; ##-- be happy
270  
271 __END__
« CAB/Format/SQLite.pm ↑ Summary CAB/Format/XmlLing.pm »