lib/Brat/Handler/File.pm

filter: Statementstmt 72.5% 243 / 335 Branchbran 40.1% 49 / 122 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 82.3% 28 / 34 Podpod 100% 14 / 14 totaltotal 64.8% 337 / 520 CC 82 SCAR 35.7
CC
82
Cov
63%
print
210.0
printStats
42.0
printTermList
42.0
CRAP 35.4
« lib/Brat/Handler.pm ↑ Summary script/brat2relationlist.pl »
1 package Brat::Handler::File;
2  
3  
4 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use open qw(:utf8 :std);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 our $VERSION='0.11';
10  
11 27 ▶sub new {
Subroutine new: called CC=10 SCAR=23.7
Pod documented
12  
13 27 my ($class, $filename) = @_;
14 27 my $annotationFilename;
15 my $textFilename;
16 27 my $textSize = 0;
17 27 my @annFiles;
18 my @textFiles;
19 27 my @files;
20 27 my $f;
21 27 my $line;
22  
23 # print STDERR "\n==> $filename - " . ref($filename) . ";\n";
24 27 ▶ if (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 21 6 27
25 21 ▶ if (ref($filename) eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filename eq '') { } 21 0 21
elsif (ref $filename eq 'ARRAY') { } 0 0 0
26 21 push @files, $filename;
27 } elsif (ref($filename) eq "ARRAY") {
28 0 push @files, @$filename;
29 }
30 }
31 27 foreach $f (@files) {
32 # warn "$f\n";
33 21 $annotationFilename = $f;
34 21 $annotationFilename =~ s/\.txt$/.ann/;
35 21 $textFilename = $f;
36 21 $textFilename =~ s/\.ann$/.txt/;
37 21 $textSize = 0;
38 21 ▶ open FILE, $textFilename or die "no such file $textFilename\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $textFilename or die "no such file $textFilename\n" 21 0 21
39 21 ▶ while($line = <FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 42 21 63
40 42 $textSize += length($line);
41 }
42 21 close FILE;
43 # my @s = stat($textFilename);
44 # $textSize += $s[7];
45 21 push @annFiles, $annotationFilename;
46 21 push @textFiles, $textFilename;
47 }
48 27 my $bratfile = {
49 'annotationFilename' => [@annFiles],
50 'textFilename' => [@textFiles],
51 'terms' => {},
52 'relations' => {},
53 'attributes' => {},
54 'maxTermId' => 0,
55 'maxRelationId' => 0,
56 'maxAttributeId' => 0,
57 'textSize' => $textSize,
58 };
59
60 27 bless($bratfile, $class);
61  
62 27 ▶ if (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 21 6 27
63 21 ▶ if (ref($filename) eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filename eq '') { } 21 0 21
elsif (ref $filename eq 'ARRAY') { } 0 0 0
64 21 $bratfile->loadBratFile;
65 } elsif (ref($filename) eq "ARRAY") {
66 0 warn "load of several brat files not implemented\n";
67 }
68 }
69 27 return($bratfile);
70 }
71  
72 6 ▶sub _textFilename {
Subroutine _textFilename: called CC=2 SCAR=6.9
73 6 my $self = shift;
74  
75 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
76 3 my $arg = shift;
77 3 push @{$self->{'textFilename'}}, $arg;
78 }
79 6 return($self->{'textFilename'});
80 }
81  
82 7 ▶sub _textSize {
Subroutine _textSize: called CC=2 SCAR=6.9
83 7 my $self = shift;
84  
85 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 6 7
86 1 $self->{'textSize'} = shift;
87 }
88 7 return($self->{'textSize'});
89 }
90  
91 27 ▶sub _annotationFilename {
Subroutine _annotationFilename: called CC=3 SCAR=11.0
92 27 my $self = shift;
93 27 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 24 3 27
94 24 my $arg = shift;
95 24 ▶ if ($arg =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\d+$/) { } 21 3 24
96 21 return($self->{'annotationFilename'}->[$arg]);
97 } else {
98 3 push @{$self->{'annotationFilename'}}, $arg;
99 }
100 }
101 6 return($self->{'annotationFilename'});
102 }
103  
104 5577 ▶sub _terms {
Subroutine _terms: called CC=3 SCAR=15.1
105 5577 my $self = shift;
106  
107 5577 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5577 5577
108 0 my $list = shift;
109 0 my $term;
110 0 foreach $term (@$list) {
111 0 $self->_addTerm($term->{'id'}, $term);
112 }
113 # $self->{'terms'} = shift;
114  
115 # max Term Id
116 }
117 5577 return($self->{'terms'});
118 }
119  
120 669 ▶sub _addTerm {
Subroutine _addTerm: called CC=3 SCAR=11.1
121 669 my $self = shift;
122  
123 669 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 669 0 669
124 669 my $id = shift;
125 669 $self->_terms->{$id} = shift;
126 # max Term Id
127 669 ▶ if ($self->_maxTermId < $self->_terms->{$id}->{'numId'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_maxTermId < $self->_terms->{$id}{"numId"}) 596 73 669
128 596 $self->_maxTermId($self->_terms->{$id}->{'numId'});
129 }
130 669 return($self->_getTermFromId($id));
131 }
132 0 return(undef);
133 }
134  
135 1270 ▶sub _maxTermId {
Subroutine _maxTermId: called CC=2 SCAR=6.9
136 1270 my $self = shift;
137  
138 1270 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 596 674 1270
139 596 $self->{'maxTermId'} = shift;
140 }
141 1270 return($self->{'maxTermId'});
142 }
143  
144 1812 ▶sub _getTermFromId {
Subroutine _getTermFromId: called CC=3 SCAR=12.0
145 1812 my $self = shift;
146 1812 my $id = shift;
147  
148 1812 ▶ if ((defined $id) && (exists $self->_terms->{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and exists $self->_terms->{$id}) 1812 0 1812
Condition33%1/3
defined $id and exists $self->_terms->{$id}
!ll&&!rl&&r
001812
MC/DC0%0/2
defined $id and exists $self->_terms->{$id}
defined $id exists $self->_terms->{$id}
Truth table33%1/3
A and B
A= defined $idB= exists $self->_terms->{$id}
ABresult
0X0
100
111
149 1812 return($self->_terms->{$id});
150 }
151 }
152  
153 1043 ▶sub _relations {
Subroutine _relations: called CC=3 SCAR=15.1
154 1043 my $self = shift;
155  
156 1043 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1043 1043
157 0 my $list = shift;
158 0 my $relation;
159 0 foreach $relation (@$list) {
160 0 $self->_addRelation($relation->{'id'}, $relation);
161 }
162 # $self->{'relations'} = shift;
163 # max Relation Id
164 }
165 1043 return($self->{'relations'});
166 }
167  
168 105 ▶sub _addRelation {
Subroutine _addRelation: called CC=3 SCAR=11.1
169 105 my $self = shift;
170  
171 105 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 105 0 105
172 105 my $id = shift;
173 105 $self->_relations->{$id} = shift;
174 # max Relation Id
175 # warn $self->_maxRelationId . " < " . $self->_relations->{$id}->{'numId'} . "\n";
176 105 ▶ if ($self->_maxRelationId < $self->_relations->{$id}->{'numId'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_maxRelationId < $self->_relations->{$id}{"numId"}) 96 9 105
177 96 $self->_maxRelationId($self->_relations->{$id}->{'numId'});
178 }
179 105 return($self->_getRelationFromId($id));
180 }
181 0 return(undef);
182 }
183  
184 206 ▶sub _maxRelationId {
Subroutine _maxRelationId: called CC=2 SCAR=6.9
185 206 my $self = shift;
186  
187 206 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 96 110 206
188 96 $self->{'maxRelationId'} = shift;
189 }
190 206 return($self->{'maxRelationId'});
191 }
192  
193 359 ▶sub _getRelationFromId {
Subroutine _getRelationFromId: called CC=3 SCAR=12.0
194 359 my $self = shift;
195 359 my $id = shift;
196  
197 359 ▶ if ((defined $id) && (exists $self->_relations->{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and exists $self->_relations->{$id}) 359 0 359
Condition33%1/3
defined $id and exists $self->_relations->{$id}
!ll&&!rl&&r
00359
MC/DC0%0/2
defined $id and exists $self->_relations->{$id}
defined $id exists $self->_relations->{$id}
Truth table33%1/3
A and B
A= defined $idB= exists $self->_relations->{$id}
ABresult
0X0
100
111
198 359 return($self->_relations->{$id});
199 }
200 }
201  
202 94 ▶sub _attributes {
Subroutine _attributes: called CC=3 SCAR=15.1
203 94 my $self = shift;
204  
205 94 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 94 94
206 0 my $list = shift;
207 0 my $attribute;
208 0 foreach $attribute (@$list) {
209 0 $self->_addAttribute($attribute->{'id'}, $attribute);
210 }
211 # $self->{'attributes'} = shift;
212 # max Attribute Id
213 }
214 94 return($self->{'attributes'});
215 }
216  
217 15 ▶sub _addAttribute {
Subroutine _addAttribute: called CC=3 SCAR=11.4
218 15 my $self = shift;
219  
220 15 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 0 15
221 15 my $id = shift;
222 15 $self->_attributes->{$id} = shift;
223 # max Attribute Id
224 15 ▶ if ($self->_maxAttributeId < $self->_attributes->{$id}->{'numId'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_maxAttributeId < $self->_attributes->{$id}{"numId"}) 15 0 15
225 15 $self->_maxAttributeId($self->_attributes->{$id}->{'numId'});
226 }
227 15 return($self->_getAttributeFromId($id));
228 }
229 0 return(undef);
230 }
231  
232 35 ▶sub _maxAttributeId {
Subroutine _maxAttributeId: called CC=2 SCAR=6.9
233 35 my $self = shift;
234  
235 35 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 20 35
236 15 $self->{'maxAttributeId'} = shift;
237 }
238 35 return($self->{'maxAttributeId'});
239 }
240  
241 22 ▶sub _getAttributeFromId {
Subroutine _getAttributeFromId: called CC=3 SCAR=12.0
242 22 my $self = shift;
243 22 my $id = shift;
244  
245 22 ▶ if ((defined $id) && (exists $self->_attributes->{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and exists $self->_attributes->{$id}) 22 0 22
Condition33%1/3
defined $id and exists $self->_attributes->{$id}
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $id and exists $self->_attributes->{$id}
defined $id exists $self->_attributes->{$id}
Truth table33%1/3
A and B
A= defined $idB= exists $self->_attributes->{$id}
ABresult
0X0
100
111
246 22 return($self->_attributes->{$id});
247 }
248 }
249  
250 21 ▶sub loadBratFile {
Subroutine loadBratFile: called CC=8 SCAR=20.8
Pod documented
251 21 my ($self) = @_;
252 21 my $line;
253 my $id;
254 21 my $info;
255 21 my $str;
256 21 my $type;
257 21 my $termId;
258 21 my $value;
259 21 my $arg1;
260 21 my $arg2;
261 21 my $o;
262 21 my $s;
263 21 my $e;
264 21 my @starts;
265 21 my @ends;
266 21 my $numId;
267 # warn "===> " . $self->_filename . "\n";
268 21 ▶ open FILE, "<:utf8", $self->_annotationFilename(0) or die "no such file " . $self->_annotationFilename(0) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<:utf8", $self->_annotationFilename(0) or die 'no such file ' . $self->_annotationFilename(0) . "\n" 21 0 21
269 21 ▶ while($line = <FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 692 21 713
270 692 chomp $line;
271 692 @starts = ();
272 692 @ends = ();
273  
274 692 ($id, $info, $str) = split /\t/, $line;
275 692 ▶ if ($id =~ /^TS?(?<numid>[\d]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^TS?(?<numid>[\d]+)/) 586 106 692
276 586 $numId = $+{numid};
277 # ($type, $start, $end) = split / /, $info;
278 # warn "info: $info\n";
279 586 ▶ if ($info =~ /^(?<type>[^ ]+) (?<offsets>.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info =~ /^(?<type>[^ ]+) (?<offsets>.*)/) 586 0 586
280 586 $type = $+{type};
281 586 foreach $o (split /;/, $+{offsets}) {
282 594 ($s, $e) = split / /, $o;
283 # warn "\t$s : $e\n";
284 594 push @starts, $s;
285 594 push @ends, $e;
286 }
287 # warn "->$id\n";
288 586 $self->_addTerm($id, {
289 'id' => $id,
290 'numId' => $numId,
291 'type' => $type,
292 'start' => [@starts],
293 'end' => [@ends],
294 'str' => $str,
295 'attrlst' => [],
296 });
297 }
298 }
299 692 ▶ if ($id =~ /^A(?<numid>\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^A(?<numid>\d+)/) 13 679 692
300 13 $numId = $+{numid};
301 13 ($type, $termId, $value) = split / /, $info;
302 13 $self->_addAttribute($id, {
303 'id' => $id,
304 'numId' => $numId,
305 'type' => $type,
306 'termId' => $termId,
307 'value' => $value,
308 });
309 # warn "termId: $start\n";
310 13 push @{$self->_getTermFromId($termId)->{'attrlst'}}, $id;
311
312 }
313 692 ▶ if ($id =~ /^R(?<numid>\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^R(?<numid>\d+)/) 93 599 692
314 # warn "==> $id " . $+{numid} . "\n";
315 93 $numId = $+{numid};
316 93 ($type, $arg1, $arg2) = split / /, $info;
317 93 $arg1 =~ s/^Arg1://;
318 93 $arg2 =~ s/^Arg2://;
319 93 $self->_addRelation($id, {
320 'id' => $id,
321 'numId' => $numId,
322 'type' => $type,
323 'arg1' => $arg1,
324 'arg2' => $arg2,
325 });
326 }
327 }
328 21 close FILE;
329 }
330  
331 0 ▶sub printStats {
Subroutine printStats: not called CC=6 SCAR=37.4
Pod documented
332 0 my ($self, $filename, $addmode) = @_;
333  
334 0 my $id;
335 my %terms;
336 0 my %termTypes;
337 0 my %relations;
338 0 my %relationTypes;
339  
340 0 my $fh;
341 0 ▶ if ($filename eq "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
342 0 $fh = \*STDOUT;
343 } else {
344 0 ▶ if (defined $addmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addmode) { } 0 0 0
345 0 ▶ open $fh, ">>:utf8", $filename or die "no such file " . $filename . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>:utf8", $filename or die 'no such file ' . $filename . "\n" 0 0 0
346 } else {
347 0 ▶ open $fh, ">:utf8", $filename or die "no such file " . $filename . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:utf8", $filename or die 'no such file ' . $filename . "\n" 0 0 0
348 }
349 }
350  
351 0 print $fh $self->getStats;
352  
353 0 ▶ if ($filename ne "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename ne "-") 0 0 0
354 0 close $fh;
355 }
356 }
357  
358 0 ▶sub getTermTypes {
Subroutine getTermTypes: not called CC=2 SCAR=17.9
Pod documented
359 0 my ($self) = @_;
360  
361 0 my $id;
362 my %termTypes;
363 0 foreach $id (keys %{$self->_terms}) {
364 0 $termTypes{$self->_getTermFromId($id)->{'type'}}++;
365 }
366 0 return(%termTypes);
367 }
368  
369 0 ▶sub getRelationTypes {
Subroutine getRelationTypes: not called CC=2 SCAR=17.9
Pod documented
370 0 my ($self) = @_;
371  
372 0 my $id;
373 my %relationTypes;
374 0 foreach $id (keys %{$self->_relations}) {
375 0 $relationTypes{$self->_getRelationFromId($id)->{'type'}}++;
376 }
377 0 return(%relationTypes);
378 }
379  
380 2 ▶sub getStats {
Subroutine getStats: called CC=3 SCAR=11.0
Pod documented
381 2 my ($self) = @_;
382  
383 2 my $id;
384 my %terms;
385 2 my %termTypes;
386 2 my %relations;
387 2 my %relationTypes;
388 2 my $stats;
389  
390 2 foreach $id (keys %{$self->_terms}) {
391 119 $terms{$self->_getTermFromId($id)->{'str'}}++;
392 119 $termTypes{$self->_getTermFromId($id)->{'type'}}++;
393 }
394
395 2 foreach $id (keys %{$self->_relations}) {
396 23 $relations{$self->_getTermFromId($self->_getRelationFromId($id)->{'arg1'})->{'str'} . " : " . $self->_getTermFromId($self->_getRelationFromId($id)->{'arg2'})->{'str'}}++;
397 23 $relationTypes{$self->_getRelationFromId($id)->{'type'}}++;
398 }
399  
400  
401 2 $stats = "number of annotated terms: " . scalar(keys %{$self->_terms}) . "\n";
402 2 $stats .= "number of annotated relations: " . scalar(keys %{$self->_relations}) . "\n";
403  
404 2 $stats .= "number of terms: " . scalar(keys %terms) . "\n";
405 2 $stats .= "number of term type: " . scalar(keys %termTypes) . "\n";
406  
407 2 $stats .= "number of relations: " . scalar(keys %relations) . "\n";
408 2 $stats .= "number of relation type: " . scalar(keys %relationTypes) . "\n";
409 2 return($stats);
410 # print "number of relations: " . scalar(keys %{$data->{'relations'}}) . "\n";
411 }
412  
413  
414 0 ▶sub printTermList {
Subroutine printTermList: not called CC=6 SCAR=37.4
Pod documented
415 0 my ($self, $filename, $addmode) = @_;
416 0 my $id;
417  
418 my $fh;
419 0 ▶ if ($filename eq "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
420 0 $fh = \*STDOUT;
421 } else {
422
423 0 ▶ if (defined $addmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addmode) { } 0 0 0
424 0 ▶ open $fh, ">>:utf8", $filename or die "no such file " . $filename . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>:utf8", $filename or die 'no such file ' . $filename . "\n" 0 0 0
425 } else {
426 0 ▶ open $fh, ">:utf8", $filename or die "no such file " . $filename . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:utf8", $filename or die 'no such file ' . $filename . "\n" 0 0 0
427 }
428 }
429 # foreach $id (keys %{$self->_terms}) {
430 # print $fh $self->_getTermFromId($id)->{'str'} . " : : " . $self->_getTermFromId($id)->{$id}->{'type'} . " :\n";
431 # }
432 0 print $fh $self->getTermList;
433  
434 0 ▶ if ($filename ne "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename ne "-") 0 0 0
435 0 close $fh;
436 }
437  
438 }
439  
440 2 ▶sub getTermList {
Subroutine getTermList: called CC=2 SCAR=6.9
Pod documented
441 2 my ($self) = @_;
442 2 my $id;
443 2 my $termlistStr = "";
444  
445 2 foreach $id (keys %{$self->_terms}) {
446 119 $termlistStr .= $self->_getTermFromId($id)->{'str'} . " : : " . $self->_getTermFromId($id)->{'type'} . " :\n";
447 }
448 2 return($termlistStr);
449 }
450  
451  
452 4 ▶sub getTerms {
Subroutine getTerms: called CC=2 SCAR=6.9
Pod documented
453 4 my ($self) = @_;
454 4 my $id;
455 my @terms;
456  
457 4 foreach $id (keys %{$self->_terms}) {
458 104 push @terms, {"str" => $self->_getTermFromId($id)->{'str'}, "lmstr" => undef, "type" => $self->_getTermFromId($id)->{'type'}};
459 }
460 4 return(\@terms);
461 }
462  
463 0 ▶sub printRelationList {
Subroutine printRelationList: not called CC=6 SCAR=37.4
Pod documented
464 0 my ($self, $filename, $addmode) = @_;
465 0 my $fh;
466
467 0 ▶ if ($filename eq "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
468 0 $fh = \*STDOUT;
469 } else {
470 0 ▶ if (defined $addmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addmode) { } 0 0 0
471 0 ▶ open $fh, ">>:utf8", $filename or die "no such file " . $filename . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>:utf8", $filename or die 'no such file ' . $filename . "\n" 0 0 0
472 } else {
473 0 ▶ open $fh, ">:utf8", $filename or die "no such file " . $filename . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:utf8", $filename or die 'no such file ' . $filename . "\n" 0 0 0
474 }
475 }
476 0 print $fh $self->getRelationList;
477 0 ▶ if ($filename ne "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename ne "-") 0 0 0
478 0 close $fh;
479 }
480 }
481  
482 2 ▶sub getRelationList {
Subroutine getRelationList: called CC=2 SCAR=6.9
Pod documented
483 2 my ($self, $addmode) = @_;
484 2 my $id;
485 2 my $relationList = "";
486
487 2 foreach $id (keys %{$self->_relations}) {
488 23 $relationList .= $self->_getTermFromId($self->_getRelationFromId($id)->{'arg1'})->{'str'} . " : " . $self->_getTermFromId($self->_getRelationFromId($id)->{'arg2'})->{'str'} . " : " . $self->_getRelationFromId($id)->{'type'} . "\n";
489 }
490 2 return($relationList);
491 }
492  
493  
494 4 ▶sub getRelations {
Subroutine getRelations: called CC=2 SCAR=6.9
Pod documented
495 4 my ($self, $addmode) = @_;
496 4 my $id;
497 4 my $relationList = "";
498 4 my @relations;
499
500 4 foreach $id (keys %{$self->_relations}) {
501 push @relations, {'str1' => $self->_getTermFromId($self->_getRelationFromId($id)->{'arg1'})->{'str'},
502 'str2' => $self->_getTermFromId($self->_getRelationFromId($id)->{'arg2'})->{'str'},
503 23 'type' => $self->_getRelationFromId($id)->{'type'}};
504 # $relationList .= $self->_getTermFromId($self->_getRelationFromId($id)->{'arg1'})->{'str'} . " : " . $self->_getTermFromId($self->_getRelationFromId($id)->{'arg2'})->{'str'} . " : " . $self->_getRelationFromId($id)->{'type'} . "\n";
505 }
506 4 return(\@relations);
507 }
508  
509  
510 4 ▶sub getAnnotationList {
Subroutine getAnnotationList: called CC=5 SCAR=16.1
Pod documented
511 4 my ($self) = @_;
512 4 my $id;
513 my $attrId;
514 4 my $elt;
515 4 my $attr;
516 4 my $annotations = "";
517 4 my $i;
518  
519 4 foreach $id (sort {&_sortId($a,$b)} keys %{$self->_terms}) {
520 223 $elt = $self->_getTermFromId($id);
521 223 $annotations .= $elt->{'id'} . "\t";
522 223 $annotations .= $elt->{'type'} . " ";
523 223 ▶ for($i=0; $i < scalar(@{$elt->{'start'}}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$elt->{"start"};}) 226 223 449
524 226 $annotations .= $elt->{'start'}->[$i] . " " . $elt->{'end'}->[$i] . ";";
525 }
526 223 chop $annotations;
527
528 223 $annotations .= "\t" . $elt->{'str'} . "\n";
529 0 foreach $attrId (sort {&_sortId($a,$b)} @{$elt->{'attrlst'}}) {
530 5 $attr = $self->_getAttributeFromId($attrId);
531 5 $annotations .= $attr->{'id'} . "\t";
532 5 $annotations .= $attr->{'type'} . " " . $attr->{'termId'} . " " . $attr->{'value'} . "\n";
533 }
534 }
535 4 foreach $id (sort {&_sortId($a,$b)} keys %{$self->_relations}) {
536 35 $elt = $self->_getRelationFromId($id);
537 35 $annotations .= $elt->{'id'} . "\t";
538 35 $annotations .= $elt->{'type'} . " Arg1:" . $elt->{'arg1'} . " Arg2:" . $elt->{'arg2'} . "\n";
539 }
540 4 return($annotations);
541 }
542  
543 1153 ▶sub _sortId {
Subroutine _sortId: called CC=1 SCAR=0.0
544 1153 my ($A, $B) = @_;
545  
546 1153 my $idA = $a;
547 1153 $idA =~ s/^[TAR]//;
548 1153 my $idB = $b;
549 1153 $idB =~ s/^[TAR]//;
550 1153 return($idA <=> $idB);
551 }
552  
553 0 ▶sub print {
Subroutine print: not called CC=14 SCAR=53.5
Pod documented
554 0 my ($self, $filename, $addmode) = @_;
555 0 my $fh;
556 my $line;
557 0 my $file;
558 0 my $annotationFilename = $filename;
559 0 $annotationFilename =~ s/\.txt$/.ann/;
560 0 my $textFilename = $filename;
561 0 $textFilename =~ s/\.ann$/.txt/;
562  
563 # print/copy text
564 0 ▶ if ($filename eq "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
565 0 $fh = \*STDOUT;
566 } else {
567 0 ▶ if (defined $addmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addmode) { } 0 0 0
568 0 ▶ open $fh, ">>:utf8", $textFilename or die "no such file $textFilename\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>:utf8", $textFilename or die "no such file $textFilename\n" 0 0 0
569 } else {
570 0 ▶ open $fh, ">:utf8", $textFilename or die "no such file $textFilename\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:utf8", $textFilename or die "no such file $textFilename\n" 0 0 0
571 }
572 }
573 0 foreach $file (@{$self->_textFilename}) {
574 0 ▶ open FILE, $file or die "no such file " . $file . "\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die 'no such file ' . $file . "\n" 0 0 0
575 0 ▶ while($line = <FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 0 0 0
576 0 print $fh $line;
577 }
578 0 close FILE;
579 }
580 0 ▶ if ($filename ne "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename ne "-") 0 0 0
581 0 close $fh;
582 }
583  
584 # print annotations
585 0 ▶ if ($filename eq "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
586 0 $fh = \*STDOUT;
587 } else {
588 0 ▶ if (defined $addmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addmode) { } 0 0 0
589 0 ▶ open $fh, ">>:utf8", $annotationFilename or die "no such file $annotationFilename\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>:utf8", $annotationFilename or die "no such file $annotationFilename\n" 0 0 0
590 } else {
591 0 ▶ open $fh, ">:utf8", $annotationFilename or die "no such file $annotationFilename\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:utf8", $annotationFilename or die "no such file $annotationFilename\n" 0 0 0
592 }
593 }
594 0 print $fh $self->getAnnotationList;
595 0 ▶ if ($filename ne "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename ne "-") 0 0 0
596 0 close $fh;
597 }
598 }
599  
600 9 1;
601  
602 __END__
« lib/Brat/Handler.pm ↑ Summary script/brat2relationlist.pl »