File Coverage

blib/lib/Brat/Handler.pm
Criterion Covered Total %
statement 130 143 90.9
branch 12 16 75.0
condition 6 6 100.0
subroutine 18 18 100.0
pod 5 5 100.0
total 171 188 90.9


line stmt bran cond sub pod time code
1             package Brat::Handler;
2              
3              
4 6     6   144588 use utf8;
  6         57  
  6         32  
5 6     6   147 use strict;
  6         11  
  6         130  
6 6     6   27 use warnings;
  6         12  
  6         181  
7 6     6   5403 use open qw(:utf8 :std);
  6         7117  
  6         32  
8              
9 6     6   5466 use Brat::Handler::File;
  6         21  
  6         11551  
10              
11             our $VERSION='0.1';
12              
13             sub new {
14              
15 4     4 1 50 my ($class) = @_;
16              
17 4         29 my $bratHandler = {
18             'inputDir' => undef,
19             # 'outputFile' => undef,
20             'inputFiles' => [],
21             'bratAnnotations' => [],
22             };
23 4         41 bless($bratHandler, $class);
24              
25 4         14 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     2   4 my $self = shift;
39 2         4 my $inputDir = shift;
40 2         4 my $file;
41 2 50       9 if (defined ($inputDir)) {
42 2 50       94 opendir DIR, $inputDir or die "no such dir $inputDir\n";
43 2         85 while($file = readdir DIR) {
44 18 100 100     133 if (($file ne ".") && ($file ne "..") && ($file =~ /\.ann$/)) {
      100        
45 6         24 $self->_inputFiles($inputDir . "/$file");
46             }
47             }
48 2         30 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     12   24 my $self = shift;
65              
66 12 100       30 if (@_) {
67 7         8 my $fileList = shift;
68 7 100       16 if (ref($fileList) eq 'ARRAY') {
69 1         2 push @{$self->{'inputFiles'}}, @$fileList;
  1         7  
70             } else {
71 6         8 push @{$self->{'inputFiles'}}, $fileList;
  6         20  
72             }
73             }
74 12         41 return($self->{'inputFiles'});
75             }
76              
77             sub _bratAnnotations {
78 15     15   22 my $self = shift;
79              
80 15 100       35 if (@_) {
81 9         10 my $bratAnn = shift;
82 9         12 push @{$self->{'bratAnnotations'}}, $bratAnn;
  9         20  
83             }
84 15         42 return($self->{'bratAnnotations'});
85             }
86              
87             sub _bratAnnotationSize {
88 4     4   5 my $self = shift;
89              
90 4         5 return(scalar(@{$self->_bratAnnotations}));
  4         7  
91             }
92              
93             sub _getBratAnnotationsFromId {
94 3     3   4 my $self = shift;
95            
96 3 50       6 if (@_) {
97 3         4 my $id = shift;
98 3         8 return $self->{'bratAnnotations'}->[$id];
99             }
100 0         0 return(undef);
101             }
102              
103             sub loadDir {
104 2     2 1 1108 my ($self, $inputDir) = @_;
105 2         3 my $file;
106              
107 2         9 $self->_scanDir($inputDir);
108 2         4 foreach $file (@{$self->_inputFiles}) {
  2         46  
109 6         18 $self->_bratAnnotations($self->loadFile($file));
110             }
111             }
112              
113             sub loadList {
114 1     1 1 760 my ($self, $list) = @_;
115 1         3 my $file;
116             my @files;
117 1 50       39 open LIST, $list or die "no such file $list\n";
118 1         27 @files = ;
119 1         2 map {chomp;} @files;
  3         8  
120 1         7 close LIST;
121 1         5 $self->_inputFiles(\@files);
122 1         1 foreach $file (@{$self->_inputFiles}) {
  1         5  
123 3         9 $self->_bratAnnotations($self->loadFile($file));
124             }
125             }
126              
127             sub loadFile {
128 9     9 1 14 my ($self, $file) = @_;
129            
130 9         44 my $ann = Brat::Handler::File->new($file);
131 9         31 return($ann);
132             }
133              
134             sub concat {
135 1     1 1 6 my ($self) = @_;
136 1         2 my $i;
137 1         2 my $offset = 0;
138 1         30 my $termIdOffset = 0;
139 1         2 my $relationIdOffset = 0;
140 1         1 my $attributeIdOffset = 0;
141 1         2 my $currentBratAnnotations;
142 1         5 my $concatAnn = Brat::Handler::File->new();
143 1         2 my %Term2newTerm;
144 1         6 for($i=0; $i < $self->_bratAnnotationSize; $i++) {
145 3         13 %Term2newTerm = ();
146 3         8 $currentBratAnnotations = $self->_getBratAnnotationsFromId($i);
147             # warn "Read file: " . $currentBratAnnotations->_textFilename . "\n";
148 3         10 $self->_copyTermsWithOffsetShift($currentBratAnnotations, $concatAnn, $termIdOffset, $offset, \%Term2newTerm);
149 3         8 $self->_copyAttributesWithOffsetShift($currentBratAnnotations, $concatAnn, $attributeIdOffset, $offset, \%Term2newTerm);
150 3         9 $self->_copyRelationsWithOffsetShift($currentBratAnnotations, $concatAnn, $relationIdOffset, $offset, \%Term2newTerm);
151 3         9 $offset += $currentBratAnnotations->_textSize;
152 3         8 $termIdOffset += $currentBratAnnotations->_maxTermId;
153 3         9 $attributeIdOffset += $currentBratAnnotations->_maxAttributeId;
154 3         9 $relationIdOffset += $currentBratAnnotations->_maxRelationId;
155 3         5 $concatAnn->_textFilename(@{$currentBratAnnotations->_textFilename});
  3         7  
156 3         4 $concatAnn->_annotationFilename(@{$currentBratAnnotations->_annotationFilename});
  3         11  
157             }
158 1         4 $concatAnn->_textSize($offset);
159            
160              
161 1         5 return($concatAnn);
162             }
163              
164             sub _copyTermsWithOffsetShift {
165 3     3   4 my ($self, $ann, $concatAnn, $termIdOffset, $offset, $Term2newTerm) = @_;
166 3         4 my $elt;
167             my $id;
168 0         0 my $newNumId;
169 0         0 my @starts;
170 0         0 my @ends;
171 0         0 my @newStarts;
172 0         0 my @newEnds;
173 0         0 my $s;
174 0         0 my $e;
175 3         4 my $i = 0;
176              
177 3         4 foreach $id (keys %{$ann->_terms}) {
  3         11  
178 83         95 @newStarts = ();
179 83         90 @newEnds = ();
180 83         180 $elt = $ann->_getTermFromId($id);
181 83         124 $newNumId = $elt->{'numId'} + $termIdOffset;
182 83         211 $Term2newTerm->{$elt->{'id'}} = "T$newNumId";
183 83         86 foreach $s (@{$elt->{'start'}}) {
  83         127  
184 84         160 push @newStarts, ($s+$offset);
185             }
186 83         86 foreach $e (@{$elt->{'end'}}) {
  83         121  
187 84         152 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         655 'str' => $elt->{'str'},
197             'attrlst' => [], # TODO
198             });
199 83         142 $i++;
200             }
201 3         12 return($i);
202             }
203              
204             sub _copyAttributesWithOffsetShift {
205 3     3   5 my ($self, $ann, $concatAnn, $attributeIdOffset, $offset, $Term2newTerm) = @_;
206 3         5 my $attr;
207             my $id;
208 0         0 my $newNumId;
209 0         0 my $newTermId;
210              
211 3         3 foreach $id (keys %{$ann->_attributes}) {
  3         7  
212 2         5 $attr = $ann->_getAttributeFromId($id);
213 2         3 $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         17 'value' => $attr->{'value'},
220             });
221             # warn "termId: $start\n";
222 2         3 push @{$concatAnn->_getTermFromId($Term2newTerm->{$attr->{'termId'}})->{'attrlst'}}, "A$newNumId";
  2         12  
223             }
224             }
225              
226             sub _copyRelationsWithOffsetShift {
227 3     3   7 my ($self, $ann, $concatAnn, $relationIdOffset, $offset, $Term2newTerm) = @_;
228 3         4 my $relation;
229             my $id;
230 0         0 my $newNumId;
231 0         0 my $newTermId1;
232 0         0 my $newTermId2;
233              
234 3         3 foreach $id (keys %{$ann->_relations}) {
  3         15  
235 12         27 $relation = $ann->_getRelationFromId($id);
236 12         18 $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         103 'arg2' => $Term2newTerm->{$relation->{'arg2'}},
243             });
244             }
245            
246             }
247              
248              
249             1;
250              
251             __END__