lib/WordNet/SenseRelate/Reader/Senseval2.pm untested

filter: Statementstmt 0.0% 0 / 262 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 117 MC/DCmcdc 0.0% 0 / 66 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 572 CC 50 SCAR 40.4
CC
50
Cov
0%
_getInstance
182.0
_processFile
42.0
_processCorpus
42.0
CRAP 56.9
1 # WordNet::SenseRelate::Reader::Senseval2 v0.09
2 # (Last updated $Id: Senseval2.pm,v 1.10 2006/12/24 12:18:47 sidz1979 Exp $)
3  
4 package WordNet::SenseRelate::Reader::Senseval2;
5  
6 use strict;
7 use XML::Parser;
8 use WordNet::SenseRelate::Tools;
9 use WordNet::SenseRelate::Word;
10 use vars qw($VERSION @ISA);
11  
12 @ISA = qw(Exporter);
13 $VERSION = '0.09';
14  
15 # Constructor for this module
16 # Instance Fields:
17 # (a) text (array)
18 # (b) words (array)
19 # (c) head (scalar:int)
20 # (d) target (scalar:int)
21 # (e) wordobjects (array)
22 # (f) lexelt (scalar:"art.n")
23 # (g) id (scalar:string)
24 # (h) answer (scalar:string, optional)
25 # (i) targetpos (scalar:string, "n/v/a/r")
26 sub new
27 {
28 my $class = shift;
29 my $self = {};
30  
31 # Create the Senseval2-reader object
32 $class = ref $class || $class;
33 bless($self, $class);
34  
35 # Read in the Senseval-2 data
36 $self->{instances} = [];
37 return undef if (!($self->_processFile(shift)));
38  
39 # Initialize WordNet tools
40 my $wntools = shift;
41 if ( !defined $wntools
42 || !ref $wntools
43 || ref($wntools) ne "WordNet::SenseRelate::Tools")
44 {
45 $wntools = WordNet::SenseRelate::Tools->new($wntools);
46 return undef if (!defined $wntools);
47 }
48 $self->{wntools} = $wntools;
49  
50 return $self;
51 }
52  
53 # Read in another Senseval-2 file
54 sub read
55 {
56 my $self = shift;
57 my $fname = shift;
58  
59 # Basic checks on input
60 return undef if (!ref $self);
61 return undef if (!defined $fname);
62  
63 return undef if (!($self->_processFile($fname)));
64 return 1;
65 }
66  
67 # Return the instance count
68 sub instanceCount
69 {
70 my $self = shift;
71 return 0 if (!defined $self || !ref $self);
72 return scalar(@{$self->{instances}});
73 }
74  
75 # Return the ith instance
76 sub instance
77 {
78 my $self = shift;
79 my $index = shift;
80 return undef if (!defined $self || !ref $self);
81 return undef
82 if ( !defined $index
83 || $index !~ /[0-9]+/
84 || $index < 0
85 || $index > $#{$self->{instances}});
86 return $self->{instances}->[$index];
87 }
88  
89 # Process a single XML file
90 sub _processFile
91 {
92 my $self = shift;
93 my $fname = shift;
94  
95 # Perform some basic checks on input data
96 return 0 if (!defined $self || !defined $fname || !ref $self);
97  
98 # Create a parser object, read the file
99 my $xmlparser = XML::Parser->new("Style" => "Tree");
100 my $parseTree;
101 eval { $parseTree = $xmlparser->parsefile($fname); };
102  
103 # Paring error
104 if ($@)
105 {
106 undef $parseTree;
107 return 0;
108 }
109  
110 # Traverse the parse tree, create our instances
111 while (scalar(@{$parseTree}) > 1)
112 {
113  
114 # Looking for a "corpus" node
115 my $key = shift(@{$parseTree});
116 my $val = shift(@{$parseTree});
117 next if ($self->_isTextNode($key, $val) || $key ne "corpus");
118  
119 # Check for english language
120 my $attribHash = shift(@{$val});
121 my $language = "english";
122 $language = lc($attribHash->{"lang"})
123 if (defined $attribHash && defined $attribHash->{"lang"});
124  
125 # Get the instances from the corpus
126 push(@{$self->{instances}}, $self->_processCorpus($val))
127 if ($language =~ /^en/);
128 }
129  
130 return 1;
131 }
132  
133 # Check if a node is a text node
134 sub _isTextNode
135 {
136 my $self = shift;
137 my $label = shift;
138 my $data = shift;
139 return 0 if (!defined $self);
140 return 0 if (!defined $label || !defined $data);
141 return 1 if ($label eq "0" && !ref($data));
142 return 0;
143 }
144  
145 # Process a senseval corpus
146 sub _processCorpus
147 {
148 my $self = shift;
149 my $aRef = shift;
150 return () if (!defined $self);
151 return () if (!defined $aRef || !ref $aRef);
152  
153 # Traversing a corpus tree
154 my $lexelt = "";
155 my @instances = ();
156 while (scalar(@{$aRef}) > 1)
157 {
158  
159 # Get one node
160 my $wonKey = shift(@{$aRef});
161 my $wonVal = shift(@{$aRef});
162 next if ($self->_isTextNode($wonKey, $wonVal));
163  
164 # Check for lexelt nodes
165 if ($wonKey eq "lexelt" && ref($wonVal))
166 {
167 my $attr = shift(@{$wonVal});
168 $lexelt = $attr->{"item"}
169 if (defined $attr && defined $attr->{"item"});
170 $lexelt =~ s/^\s+//;
171 $lexelt =~ s/\s+$//;
172 push(@instances, $self->_processLexelt($lexelt, $wonVal));
173 }
174 }
175  
176 return @instances;
177 }
178  
179 # Process a lexelt
180 sub _processLexelt
181 {
182 my $self = shift;
183 my $lexelt = shift;
184 my $aRef = shift;
185  
186 # Perform basic checks on input
187 return () if (!defined $self);
188 return ()
189 if ( !defined $lexelt
190 || !defined $aRef
191 || $lexelt eq ""
192 || !ref $aRef);
193  
194 # Traverse the lexelt node
195 my @instances = ();
196 my $id = "";
197 while (scalar(@{$aRef}) > 1)
198 {
199  
200 # Get the node data
201 my $wonKey = shift(@{$aRef});
202 my $wonVal = shift(@{$aRef});
203  
204 # Follow down to the instance node
205 if ($wonKey eq "instance" && ref($wonVal))
206 {
207 my $attr = shift(@{$wonVal});
208 $id = $attr->{"id"} if (defined $attr && defined $attr->{"id"});
209 $id =~ s/^\s+//;
210 $id =~ s/\s+$//;
211 my $inst = $self->_getInstance($lexelt, $id, $wonVal);
212 push(@instances, $inst) if (defined $inst);
213 }
214 }
215  
216 return @instances;
217 }
218  
219 # Get one instance/context
220 sub _getInstance
221 {
222 my $self = shift;
223 my $lexelt = shift;
224 my $id = shift;
225 my $aRef = shift;
226 my $wntools = $self->{wntools};
227  
228 # Perform basic input checks
229 return undef if (!defined $self);
230 return undef
231 if ( !defined $aRef
232 || !defined $lexelt
233 || !defined $id
234 || !ref($aRef)
235 || $lexelt eq ""
236 || $id eq "");
237  
238 # Create an empty instance
239 my $retRef = {};
240 $retRef->{text} = [];
241 $retRef->{words} = [];
242 $retRef->{head} = -1;
243 $retRef->{target} = -1;
244 $retRef->{wordobjects} = [];
245  
246 # Traverse the node
247 my ($aid, $sid) = ("", "");
248 while (scalar(@{$aRef}) > 1)
249 {
250  
251 # Get the node data
252 my $wonKey = shift(@{$aRef});
253 my $wonVal = shift(@{$aRef});
254  
255 # Get the answer, if specified
256 if ($wonKey eq "answer" && ref($wonVal))
257 {
258 my $attr = shift(@{$wonVal});
259 $aid = $attr->{"instance"}
260 if (defined $attr && defined $attr->{"instance"});
261 $aid =~ s/^\s+//;
262 $aid =~ s/\s+$//;
263 $sid = $attr->{"senseid"}
264 if ( defined $attr
265 && defined $attr->{"senseid"}
266 && $aid eq $id);
267 $aid =~ s/^\s+//;
268 $aid =~ s/\s+$//;
269 }
270  
271 # Get the context info
272 if ($wonKey eq "context" && ref($wonVal))
273 {
274  
275 # Ignore attribs hash
276 shift(@{$wonVal});
277  
278 # Traverse the context node
279 while (scalar(@{$wonVal}) > 1)
280 {
281  
282 # Get context data
283 my $textKey = shift(@{$wonVal});
284 my $textVal = shift(@{$wonVal});
285  
286 # Concatenate all text data
287 if ($self->_isTextNode($textKey, $textVal))
288 {
289 push(@{$retRef->{text}}, $textVal);
290 my $segment = lc($textVal);
291 $segment =~ s/[\n\r\f]+/ /g;
292 $segment =~ s/^\s+//;
293 $segment =~ s/\s+$//;
294 $segment =~ s/\[[^\]]*\]//g;
295 $segment =~ s/\&([a-z]*\;)+//g;
296 $segment =~ s/\'//g; #'
297 $segment =~ s/[^a-z0-9]+/ /g;
298 while ($segment =~ s/([0-9]+)\s+([0-9]+)/$1$2/g) { }
299 $segment = $wntools->compoundify($segment)
300 if (defined($wntools));
301 $segment =~ s/^\s+//;
302 $segment =~ s/\s+$//;
303 my @tmpArr = split(/\s+/, $segment);
304 push(@{$retRef->{words}}, @tmpArr);
305  
306 foreach my $wrd (@tmpArr)
307 {
308 push(
309 @{$retRef->{wordobjects}},
310 WordNet::SenseRelate::Word->new($wrd)
311 );
312 }
313 }
314  
315 # Get text from head node
316 elsif ($textKey eq "head" && ref($textVal))
317 {
318 $retRef->{head} = scalar(@{$retRef->{text}});
319 my $attr = shift(@{$textVal});
320 my $segment = "";
321 if (ref($attr) && defined $attr->{"sats"})
322 {
323 $segment = $attr->{"sats"};
324 $segment =~ s/^\s+//;
325 $segment =~ s/\s+.*//;
326 $segment =~ s/\.[0-9\:\s]*$//;
327 push(@{$retRef->{text}}, $segment);
328 }
329 else
330 {
331 $segment = $self->_getAllText($textVal);
332 push(@{$retRef->{text}}, $segment);
333 }
334 $segment = lc($segment);
335 $segment =~ s/[\n\r\f]+/ /g;
336 $segment =~ s/^\s+//;
337 $segment =~ s/\s+$//;
338 $segment =~ s/\[[^\]]*\]//g;
339 $segment =~ s/\&([a-z]*\;)+//g;
340 $segment =~ s/\'//g; #'
341 $segment =~ s/[^a-z0-9]+/ /g;
342 while ($segment =~ s/([0-9]+)\s+([0-9]+)/$1$2/g) { }
343 $segment =~ s/^\s+//;
344 $segment =~ s/\s+$//;
345 my $tmpWrd = join("_", split(/\s+/, $segment));
346 push(@{$retRef->{words}}, $tmpWrd);
347 push(
348 @{$retRef->{wordobjects}},
349 WordNet::SenseRelate::Word->new($tmpWrd)
350 );
351 $retRef->{target} = scalar(@{$retRef->{words}}) - 1;
352 }
353 }
354 }
355 }
356  
357 # Complete the instance object data
358 $retRef->{lexelt} = $lexelt;
359 $retRef->{id} = $id;
360 $retRef->{answer} = $sid if (defined $aid && $aid ne "");
361 $retRef->{targetpos} = $1 if ($lexelt =~ /\.([nvar])$/);
362  
363 return $retRef;
364 }
365  
366 # Get all the text from inside a tag
367 sub _getAllText
368 {
369 my $self = shift;
370 my $aRef = shift;
371  
372 # Perform some basic input checks
373 return "" if (!defined $self);
374 return "" if (!defined $aRef || !ref $aRef);
375  
376 # Traverse the node
377 my $text = "";
378 while (scalar @{$aRef} > 1)
379 {
380  
381 # Get node data
382 my $first = shift(@{$aRef});
383 my $second = shift(@{$aRef});
384  
385 # Concatenate the text
386 $text .= " $second" if ($self->_isTextNode($first, $second));
387 print STDERR "In getAllText(): Not a text node. Tag=$first\n"
388 if (!$self->_isTextNode($first, $second));
389 }
390  
391 return $text;
392 }
393  
394 # Print the XML parse tree
395 sub printTree
396 {
397 my $self = shift;
398 my $fname = shift;
399 return if (!defined $self || !defined $fname);
400  
401 # Create a parser object
402 my $xmlparser = XML::Parser->new("Style" => "Tree");
403 my $parseTree;
404 eval { $parseTree = $xmlparser->parsefile($fname); };
405  
406 # Paring error
407 if ($@)
408 {
409 undef $parseTree;
410 return;
411 }
412  
413 $self->_printTree($parseTree);
414 }
415  
416 # Recursive routine to traverse tree
417 sub _printTree
418 {
419 my $self = shift;
420 my $parseTree = shift;
421 my ($attribs, $key, $val);
422 return if (!defined $self || !defined $parseTree || !ref $parseTree);
423  
424 # Traverse the tree
425 while (scalar(@{$parseTree}))
426 {
427 $key = shift(@{$parseTree});
428 $val = shift(@{$parseTree});
429 if (ref($key))
430 {
431 print STDERR "Key Error!\n";
432 return;
433 }
434 elsif ($key eq "0")
435 {
436 print STDERR "$val ";
437 }
438 elsif (!ref($val))
439 {
440 print STDERR "Val Error!\n";
441 return;
442 }
443 else
444 {
445 $attribs = shift(@{$val});
446 print STDERR "Key: ($key) ";
447 $self->_printTree($val);
448 }
449 }
450 }
451  
452 1;
453  
454 __END__