blib/lib/Brat/Handler.pm

filter: Statementstmt 99.3% 144 / 145 Branchbran 80.0% 16 / 20 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 97.4% 192 / 197 SCAR 30.0
CC
20
Cov
97%
_scanDir
7.0
_copyTermsWithOffsetShift
4.0
loadList
3.0
CRAP 20.0
« blib/lib/Brat/Handler/File.pm ↑ Summary Build »
1 package Brat::Handler;
2  
3  
4 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use open qw(:utf8 :std);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Brat::Handler::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION='0.1';
12  
13 sub new {
14  
15 4 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16  
17 4 my $bratHandler = {
18 'inputDir' => undef,
19 # 'outputFile' => undef,
20 'inputFiles' => [],
21 'bratAnnotations' => [],
22 };
23 4 bless($bratHandler, $class);
24  
25 4 return($bratHandler);
26 }
27  
28 # sub _inputDir {
29 # my $self = shift;
30  
31 # if (@_) {
32 # $self->{'inputDir'} = shift;
33 # }
34 # return($self->{'inputDir'});
35 # }
36  
37 sub _scanDir {
38 2 ▶ my $self = shift;
Subroutine _scanDir: called CC=7 SCAR=19.5
39 2 my $inputDir = shift;
40 2 my $file;
41 2 ▶ if (defined ($inputDir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inputDir) 2 0 2
42 2 ▶ opendir DIR, $inputDir or die "no such dir $inputDir\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $inputDir or die "no such dir $inputDir\n" 0 2 2
43 2 ▶ while($file = readdir DIR) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($file = readdir DIR)) 18 2 20
44 18 ▶ if (($file ne ".") && ($file ne "..") && ($file =~ /\.ann$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file ne "." and $file ne ".." and $file =~ /\.ann$/) 6 12 18
Condition100%6/6
$file ne "." and $file ne ".." and $file =~ /\.ann$/
!ll&&!rl&&r
486
$file ne "." and $file ne ".."
!ll&&!rl&&r
2214
MC/DC100%3/3
$file ne "." and $file ne ".." and $file =~ /\.ann$/
$file ne "." $file ne ".." $file =~ /\.ann$/
Truth table100%4/4
A and B and C
A= $file ne "."B= $file ne ".."C= $file =~ /\.ann$/
ABCresult
0XX0
10X0
1100
1111
45 6 $self->_inputFiles($inputDir . "/$file");
46 }
47 }
48 2 closedir DIR;
49 }
50 }
51  
52  
53 # sub _outputFile {
54 # my $self = shift;
55  
56 # if (@_) {
57 # $self->{'outputFile'} = shift;
58 # }
59 # return($self->{'outputFile'});
60 # }
61  
62  
63 sub _inputFiles {
64 12 ▶ my $self = shift;
Subroutine _inputFiles: called CC=3 SCAR=11.0
65  
66 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 5 12
67 7 my $fileList = shift;
68 7 ▶ if (ref($fileList) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fileList eq 'ARRAY') { } 1 6 7
69 1 push @{$self->{'inputFiles'}}, @$fileList;
70 } else {
71 6 push @{$self->{'inputFiles'}}, $fileList;
72 }
73 }
74 12 return($self->{'inputFiles'});
75 }
76  
77 sub _bratAnnotations {
78 15 ▶ my $self = shift;
Subroutine _bratAnnotations: called CC=2 SCAR=6.9
79  
80 15 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 6 15
81 9 my $bratAnn = shift;
82 9 push @{$self->{'bratAnnotations'}}, $bratAnn;
83 }
84 15 return($self->{'bratAnnotations'});
85 }
86  
87 sub _bratAnnotationSize {
88 4 ▶ my $self = shift;
Subroutine _bratAnnotationSize: called CC=1 SCAR=0.0
89  
90 4 return(scalar(@{$self->_bratAnnotations}));
91 }
92  
93 sub _getBratAnnotationsFromId {
94 3 ▶ my $self = shift;
Subroutine _getBratAnnotationsFromId: called CC=2 SCAR=7.4
95
96 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
97 3 my $id = shift;
98 3 return $self->{'bratAnnotations'}->[$id];
99 }
100 0 return(undef);
101 }
102  
103 sub loadDir {
104 2 ▶ my ($self, $inputDir) = @_;
Subroutine loadDir: called CC=2 SCAR=6.9
Pod documented
105 2 my $file;
106  
107 2 $self->_scanDir($inputDir);
108 2 foreach $file (@{$self->_inputFiles}) {
109 6 $self->_bratAnnotations($self->loadFile($file));
110 }
111 }
112  
113 sub loadList {
114 1 ▶ my ($self, $list) = @_;
Subroutine loadList: called CC=3 SCAR=11.0
Pod documented
115 1 my $file;
116 my @files;
117 1 ▶ open LIST, $list or die "no such file $list\n";
Branch: 1 branch
BranchTrueFalseTotal
open LIST, $list or die "no such file $list\n" 0 1 1
118 1 @files = <LIST>;
119 1 map {chomp;} @files;
120 1 close LIST;
121 1 $self->_inputFiles(\@files);
122 1 foreach $file (@{$self->_inputFiles}) {
123 3 $self->_bratAnnotations($self->loadFile($file));
124 }
125 }
126  
127 sub loadFile {
128 9 ▶ my ($self, $file) = @_;
Subroutine loadFile: called CC=1 SCAR=0.0
Pod documented
129
130 9 my $ann = Brat::Handler::File->new($file);
131 9 return($ann);
132 }
133  
134 sub concat {
135 1 ▶ my ($self) = @_;
Subroutine concat: called CC=2 SCAR=6.9
Pod documented
136 1 my $i;
137 1 my $offset = 0;
138 1 my $termIdOffset = 0;
139 1 my $relationIdOffset = 0;
140 1 my $attributeIdOffset = 0;
141 1 my $currentBratAnnotations;
142 1 my $concatAnn = Brat::Handler::File->new();
143 1 my %Term2newTerm;
144 1 ▶ for($i=0; $i < $self->_bratAnnotationSize; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->_bratAnnotationSize) 3 1 4
145 3 %Term2newTerm = ();
146 3 $currentBratAnnotations = $self->_getBratAnnotationsFromId($i);
147 # warn "Read file: " . $currentBratAnnotations->_textFilename . "\n";
148 3 $self->_copyTermsWithOffsetShift($currentBratAnnotations, $concatAnn, $termIdOffset, $offset, \%Term2newTerm);
149 3 $self->_copyAttributesWithOffsetShift($currentBratAnnotations, $concatAnn, $attributeIdOffset, $offset, \%Term2newTerm);
150 3 $self->_copyRelationsWithOffsetShift($currentBratAnnotations, $concatAnn, $relationIdOffset, $offset, \%Term2newTerm);
151 3 $offset += $currentBratAnnotations->_textSize;
152 3 $termIdOffset += $currentBratAnnotations->_maxTermId;
153 3 $attributeIdOffset += $currentBratAnnotations->_maxAttributeId;
154 3 $relationIdOffset += $currentBratAnnotations->_maxRelationId;
155 3 $concatAnn->_textFilename(@{$currentBratAnnotations->_textFilename});
156 3 $concatAnn->_annotationFilename(@{$currentBratAnnotations->_annotationFilename});
157 }
158 1 $concatAnn->_textSize($offset);
159
160  
161 1 return($concatAnn);
162 }
163  
164 sub _copyTermsWithOffsetShift {
165 3 ▶ my ($self, $ann, $concatAnn, $termIdOffset, $offset, $Term2newTerm) = @_;
Subroutine _copyTermsWithOffsetShift: called CC=4 SCAR=13.9
166 3 my $elt;
167 my $id;
168 3 my $newNumId;
169 3 my @starts;
170 3 my @ends;
171 3 my @newStarts;
172 3 my @newEnds;
173 3 my $s;
174 3 my $e;
175 3 my $i = 0;
176  
177 3 foreach $id (keys %{$ann->_terms}) {
178 83 @newStarts = ();
179 83 @newEnds = ();
180 83 $elt = $ann->_getTermFromId($id);
181 83 $newNumId = $elt->{'numId'} + $termIdOffset;
182 83 $Term2newTerm->{$elt->{'id'}} = "T$newNumId";
183 83 foreach $s (@{$elt->{'start'}}) {
184 84 push @newStarts, ($s+$offset);
185 }
186 83 foreach $e (@{$elt->{'end'}}) {
187 84 push @newEnds, ($e+$offset);
188 }
189  
190 $concatAnn->_addTerm("T$newNumId", {
191 'id' => "T$newNumId",
192 'numId' => $newNumId,
193 'type' => $elt->{'type'},
194 'start' => [@newStarts],
195 'end' => [@newEnds],
196 83 'str' => $elt->{'str'},
197 'attrlst' => [], # TODO
198 });
199 83 $i++;
200 }
201 3 return($i);
202 }
203  
204 sub _copyAttributesWithOffsetShift {
205 3 ▶ my ($self, $ann, $concatAnn, $attributeIdOffset, $offset, $Term2newTerm) = @_;
Subroutine _copyAttributesWithOffsetShift: called CC=2 SCAR=6.9
206 3 my $attr;
207 my $id;
208 3 my $newNumId;
209 3 my $newTermId;
210  
211 3 foreach $id (keys %{$ann->_attributes}) {
212 2 $attr = $ann->_getAttributeFromId($id);
213 2 $newNumId = $attr->{'numId'} + $attributeIdOffset;
214 $concatAnn->_addAttribute("A$newNumId", {
215 'id' => "A$newNumId",
216 'numId' => $newNumId,
217 'type' => $attr->{'type'},
218 'termId' => $Term2newTerm->{$attr->{'termId'}},
219 2 'value' => $attr->{'value'},
220 });
221 # warn "termId: $start\n";
222 2 push @{$concatAnn->_getTermFromId($Term2newTerm->{$attr->{'termId'}})->{'attrlst'}}, "A$newNumId";
223 }
224 }
225  
226 sub _copyRelationsWithOffsetShift {
227 3 ▶ my ($self, $ann, $concatAnn, $relationIdOffset, $offset, $Term2newTerm) = @_;
Subroutine _copyRelationsWithOffsetShift: called CC=2 SCAR=6.9
228 3 my $relation;
229 my $id;
230 3 my $newNumId;
231 3 my $newTermId1;
232 3 my $newTermId2;
233  
234 3 foreach $id (keys %{$ann->_relations}) {
235 12 $relation = $ann->_getRelationFromId($id);
236 12 $newNumId = $relation->{'numId'} + $relationIdOffset;
237 $concatAnn->_addRelation("R$newNumId", {
238 'id' => "R$newNumId",
239 'numId' => $newNumId,
240 'type' => $relation->{'type'},
241 'arg1' => $Term2newTerm->{$relation->{'arg1'}},
242 12 'arg2' => $Term2newTerm->{$relation->{'arg2'}},
243 });
244 }
245
246 }
247  
248  
249 6 1;
250  
251 __END__
« blib/lib/Brat/Handler/File.pm ↑ Summary Build »