line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package NLP::GATE::Annotation; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
22
|
use warnings; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
106
|
|
4
|
4
|
|
|
4
|
|
19
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
107
|
|
5
|
4
|
|
|
4
|
|
21
|
use Carp; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
4881
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
NLP::GATE::Annotation - A class for representing GATE-like annotations |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 0.6 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=cut |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.6'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use NLP::GATE::Annotation; |
22
|
|
|
|
|
|
|
my $ann = NLP::GATE::Annotation->new($type,$fromOffset,$toOffset); |
23
|
|
|
|
|
|
|
my $copy = $ann->clone(); |
24
|
|
|
|
|
|
|
$ann->setFromTo($fromOffset,$toOffset); |
25
|
|
|
|
|
|
|
$ann->setType($type); |
26
|
|
|
|
|
|
|
$ann->setFeature($name,$value); |
27
|
|
|
|
|
|
|
$ann->setFeatureType($name,$type); |
28
|
|
|
|
|
|
|
$value = $ann->getFeature($name); |
29
|
|
|
|
|
|
|
$type = $ann->getFeatureType($name); |
30
|
|
|
|
|
|
|
$from = $ann->getFrom(); |
31
|
|
|
|
|
|
|
$to = $ann->getTo(); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 DESCRIPTION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This is a simple class representing a GATE-like annotation for |
36
|
|
|
|
|
|
|
document text. The annotation knows about the text offsets from |
37
|
|
|
|
|
|
|
start (first character has offset 0) to end (the offset of the |
38
|
|
|
|
|
|
|
byte following the annotation). |
39
|
|
|
|
|
|
|
Zero length annotations should be avoided as gate cannot handle |
40
|
|
|
|
|
|
|
them properly. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
All functions that set values return the original |
43
|
|
|
|
|
|
|
annotation object. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
This library does not attempt to mirror the Java API or the way how |
46
|
|
|
|
|
|
|
annotations are represented and handled in the original GATE API. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
A major difference to the GATE API is that annotations can live |
49
|
|
|
|
|
|
|
completely independent from annotation sets or documents and have their |
50
|
|
|
|
|
|
|
own constructor. An annotation is simply a range that is assiciated |
51
|
|
|
|
|
|
|
with a type and a map of features. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Another difference to the GATE API is that feature maps are NOT |
54
|
|
|
|
|
|
|
seperately modeled. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 METHODS |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 new() |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Create a new annotation. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub new { |
66
|
4
|
|
|
4
|
1
|
1279
|
my $class = shift; |
67
|
4
|
|
|
|
|
9
|
my $type = shift; |
68
|
4
|
50
|
|
|
|
13
|
if(!defined($type)) { |
69
|
0
|
|
|
|
|
0
|
croak "Must specify a type when creating an annotation"; |
70
|
|
|
|
|
|
|
} |
71
|
4
|
|
|
|
|
7
|
my $fromOffset = shift; |
72
|
4
|
50
|
|
|
|
12
|
if(!defined($fromOffset)) { |
73
|
0
|
|
|
|
|
0
|
croak "Must specify from offset when creating an annotation"; |
74
|
|
|
|
|
|
|
} |
75
|
4
|
|
|
|
|
7
|
my $toOffset = shift; |
76
|
4
|
50
|
|
|
|
13
|
if(!defined($toOffset)) { |
77
|
0
|
|
|
|
|
0
|
croak "Must specify to offset when creating an annotation"; |
78
|
|
|
|
|
|
|
} |
79
|
4
|
|
|
|
|
24
|
my $featurehashref = shift; |
80
|
4
|
|
33
|
|
|
43
|
my $self = bless { |
81
|
|
|
|
|
|
|
type => $type, |
82
|
|
|
|
|
|
|
fromOffset => $fromOffset, |
83
|
|
|
|
|
|
|
toOffset => $toOffset, |
84
|
|
|
|
|
|
|
features => {}, |
85
|
|
|
|
|
|
|
featuretypes => {}, |
86
|
|
|
|
|
|
|
}, ref($class) || $class; |
87
|
4
|
|
|
|
|
18
|
foreach my $key ( keys %$featurehashref) { |
88
|
0
|
|
|
|
|
0
|
$self->setFeature($key,$featurehashref->{$key}); |
89
|
|
|
|
|
|
|
} |
90
|
4
|
|
|
|
|
24
|
return $self; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 clone() |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Create a deep copy of an annotation, creating a new annotation object |
97
|
|
|
|
|
|
|
that has the same values as the old one, but contains no references to |
98
|
|
|
|
|
|
|
data contained in the old one. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub clone { |
103
|
2
|
|
|
2
|
1
|
6
|
my $self = shift; |
104
|
2
|
|
|
|
|
10
|
my $new = NLP::GATE::Annotation->new($self->getType(),$self->getFrom(),$self->getTo()); |
105
|
2
|
|
|
|
|
4
|
foreach my $name ( keys %{$self->{features}} ) { |
|
2
|
|
|
|
|
7
|
|
106
|
2
|
|
|
|
|
7
|
$new->setFeature($name,$self->{features}->{$name}); |
107
|
2
|
|
50
|
|
|
21
|
$new->setFeatureType($name,$self->{featuretypes}->{$name} || "java.lang.String"); |
108
|
|
|
|
|
|
|
} |
109
|
2
|
|
|
|
|
39
|
return $new; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 setType($type) |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Set a new annotation type. Must be a string and a valid type name. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub setType { |
119
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
120
|
0
|
|
|
|
|
0
|
my $type = shift; |
121
|
0
|
|
|
|
|
0
|
$self->{type} = $type; |
122
|
0
|
|
|
|
|
0
|
return $self; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 getType() |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Return the current annotation type. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub getType { |
132
|
8
|
|
|
8
|
1
|
478
|
my $self = shift; |
133
|
8
|
|
|
|
|
46
|
return $self->{type}; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 setFromTo($fromOffset,$toOffset) |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Set the text range of the annotation. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub setFromTo { |
143
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
144
|
0
|
|
|
|
|
0
|
my $from= shift; |
145
|
0
|
|
|
|
|
0
|
my $to = shift; |
146
|
0
|
|
|
|
|
0
|
$self->{fromOffset} = $from; |
147
|
0
|
|
|
|
|
0
|
$self->{toOffset} = $to; |
148
|
0
|
|
|
|
|
0
|
return $self; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 setFeature($name,$value) |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Add or replace the feature of the given name with the new |
156
|
|
|
|
|
|
|
value. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub setFeature { |
161
|
4
|
|
|
4
|
1
|
10
|
my $self = shift; |
162
|
4
|
|
|
|
|
8
|
my $name = shift; |
163
|
|
|
|
|
|
|
# TODO: check that name is a valid string: what is a valid name string? |
164
|
4
|
|
|
|
|
6
|
my $value = shift; |
165
|
4
|
50
|
|
|
|
30
|
my $new = 1 if(!defined($self->{features}->{$name})); |
166
|
4
|
|
|
|
|
11
|
$self->{features}->{$name} = $value; |
167
|
4
|
50
|
|
|
|
17
|
$self->setFeatureType($name,"java.lang.String") if($new); |
168
|
4
|
|
|
|
|
7
|
return $self; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 setFeatureType($name,$type) |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Add or replace the Java type associated with the feature. |
175
|
|
|
|
|
|
|
If this method is never used, the default for a feature is java.lang.String. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=cut |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub setFeatureType { |
180
|
7
|
|
|
7
|
1
|
10
|
my $self = shift; |
181
|
7
|
|
|
|
|
9
|
my $name = shift; |
182
|
7
|
|
|
|
|
7
|
my $type = shift; |
183
|
7
|
50
|
|
|
|
21
|
croak "Cannot set a type for a nonexisting feature" if(!defined($self->{features}->{$name})); |
184
|
7
|
|
|
|
|
17
|
$self->{featuretypes}->{$name} = $type; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 getFeatureType($name) |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Return the Java type associated with the feature. |
191
|
|
|
|
|
|
|
If the type was never set, "java.lang.String" is returned. |
192
|
|
|
|
|
|
|
If no value is set for the feature, undef is returned. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub getFeatureType { |
197
|
3
|
|
|
3
|
1
|
5
|
my $self = shift; |
198
|
3
|
|
|
|
|
5
|
my $name = shift; |
199
|
3
|
100
|
|
|
|
12
|
return undef unless $self->{features}->{$name}; |
200
|
2
|
|
50
|
|
|
11
|
return $self->{featuretypes}->{$name} || "java.lang.String"; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 getFeatureNames() |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Return an array of feature names for this annotation. |
207
|
|
|
|
|
|
|
The order of feature names in the array is random and may change between |
208
|
|
|
|
|
|
|
successive calls. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
sub getFeatureNames() { |
212
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
213
|
0
|
|
|
|
|
0
|
return keys %{$self->{features}}; |
|
0
|
|
|
|
|
0
|
|
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 setFeatures($hashref) |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Add or replace all the features with the keys in the hash reference with |
220
|
|
|
|
|
|
|
the corresponding values in the hash reference. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=cut |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub setFeatures { |
225
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
226
|
0
|
|
|
|
|
0
|
my $features = shift; |
227
|
0
|
|
|
|
|
0
|
foreach my $k (keys %$features) { |
228
|
0
|
|
|
|
|
0
|
$self->setFeature($k,$features->{$k}); |
229
|
|
|
|
|
|
|
} |
230
|
0
|
|
|
|
|
0
|
return $self; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 getFeature($name) |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Returns the value of the feature. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub getFeature { |
241
|
5
|
|
|
5
|
1
|
10
|
my $self = shift; |
242
|
5
|
|
|
|
|
8
|
my $name = shift; |
243
|
5
|
|
|
|
|
57
|
return $self->{features}->{$name}; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head2 getFrom() |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Returns the from offset. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=cut |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub getFrom { |
253
|
3
|
|
|
3
|
1
|
6
|
my $self = shift; |
254
|
3
|
|
|
|
|
12
|
return $self->{fromOffset}; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head2 getTo() |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Returns the to offset. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub getTo { |
264
|
3
|
|
|
3
|
1
|
6
|
my $self = shift; |
265
|
3
|
|
|
|
|
15
|
return $self->{toOffset}; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
sub _getFeatures { |
271
|
0
|
|
|
0
|
|
|
my $self = shift; |
272
|
0
|
|
|
|
|
|
return $self->{features}; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
sub _getFeatureTypes { |
276
|
0
|
|
|
0
|
|
|
my $self = shift; |
277
|
0
|
|
|
|
|
|
return $self->{featuretypes}; |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub _getFeaturesString { |
281
|
0
|
|
|
0
|
|
|
my $self = shift; |
282
|
0
|
|
|
|
|
|
my $f = $self->_getFeatures(); |
283
|
0
|
|
|
|
|
|
my @tmp = map { $_."=".$f->{$_} } keys %{$f}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
284
|
0
|
|
|
|
|
|
return join(", ",@tmp); |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=head1 AUTHOR |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
Johann Petrak, C<< >> |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 BUGS |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
294
|
|
|
|
|
|
|
C, or through the web interface at |
295
|
|
|
|
|
|
|
L. |
296
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
297
|
|
|
|
|
|
|
your bug as I make changes. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=head1 SUPPORT |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
perldoc NLP::GATE |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
You can also look for information at: |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=over 4 |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
L |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=item * CPAN Ratings |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
L |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
L |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=item * Search CPAN |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
L |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=back |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=cut |
329
|
|
|
|
|
|
|
1; # End of NLP::GATE::Annotation |