script/text2sval.pl untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 170 CC 1 SCAR 6.9
CC
1
Cov
0%
showminimal
2.0
showhelp
2.0
showversion
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 text2sval.pl - Convert a plain text file with one context per line into Senseval-2 format
6  
7 =head1 SYNOPSIS
8  
9 Create a Senseval-2 format file from a plain text input (where target
10 words are marked) and a given key file:
11  
12 cat small.txt
13
14 Output =>
15  
16 he played on the offensive <head>line</head> in college
17 i think the phone <head>line</head> is down
18  
19 cat key.txt
20  
21 Output =>
22  
23 <instance id="0"/> <sense id="formation"/>
24 <instance id="1"/> <sense id="cable"/>
25  
26 text2sval.pl small.txt --lexelt line --key key.txt
27  
28 Output =>
29  
30 <corpus lang="english">
31 <lexelt item="line">
32 <instance id="0">
33 <answer instance="0" senseid="formation"/>
34 <context>
35 he played on the offensive <head>line</head> in college
36 </context>
37 </instance>
38 <instance id="1">
39 <answer instance="1" senseid="cable"/>
40 <context>
41 i think the phone <head>line</head> is down
42 </context>
43 </instance>
44 </lexelt>
45 </corpus>
46  
47 Type C<text2sval.pl> for a quick summary of options.
48  
49 =head1 DESCRIPTION
50  
51 Converts a plain text instance data file into a Senseval-2 formatted XML
52 file. Sense tags, instances ids, and lexelt tags can be inserted into
53 the Senseval-2 file.
54  
55 =head1 INPUT
56  
57 =head2 Required Arguments:
58  
59 =head3 TEXT
60  
61 Should be a plain text data file containing context of a single instance on each
62 line. In other words, contexts of different instances should be separated by
63 a newline character and there should not be any newline characters within the
64 context of a single instance. For example, the following shows 3
65 instances with context of each instance on each line.
66  
67 market capitalization draws a <head>line</head> between big and small stocks
68  
69 volunteers using a dozen telephone <head>lines</head> at the group's washington headquarters this week will be urging members in alabama arizona
70  
71 he proceeded briskly through a reception <head>line</head> of party officials and old friends
72  
73 =head2 Optional Arguments:
74  
75 =head4 --lexelt LEX
76  
77 Specifies the value of the <lexelt> item tag to be used in the output
78 Senseval-2 file.
79  
80 =head4 --key KEYFILE
81  
82 Displays the instance ids and optional sense tags of the instances in the TEXT
83 file. These will be used as the values of the <instance> and <sense> tags in
84 the output Senseval-2 file.
85  
86 Each line in KEYFILE should show the instance id and optional sense tags of
87 the instance displayed on the corresponding line in the TEXT file, in the
88 format :
89  
90 <instance id=\"IID\"\/> [<sense id=\"SID\"\/>]*
91  
92 where an <instance> tag is followed by zero or more <sense> tags.
93  
94 =head3 Other Options :
95  
96 =head4 --help
97  
98 Displays this message.
99  
100 =head4 --version
101  
102 Displays the version information.
103  
104 =head1 OUTPUT
105  
106 Given TEXT input file is converted to a Senseval-2 formatted XML file that
107 is displayed on stdout.
108  
109 Sample Outputs
110  
111 =over
112  
113 =item 1 No options specified
114  
115 Input TEXT file => input.text
116  
117 the maiden seated herself upon the golden chair and offered the silver one to her companion they were <head>served</head> by maidens dressed in white whose feet made no sound as they moved about and not a word was spoken during the meal
118  
119 why leftover beef should ever be a problem i cannot understand there is nothing better than cold roast sliced paper thin and <head>served</head> with mustard chutney or pickled walnuts these can be found in almost any food specialty shop meat to be served cold should be removed from the refrigerator an hour or so before eating to allow it to return to room temperature
120  
121 continue cooking for hours remove the ribs to a hot platter and <head>serve</head> the pan juices separately
122  
123 an agency spokesman al heier said it granted the exceptions because these crops are grown by few farmers in small areas that can be closely monitored dinoseb is a herbicide that also <head>serves</head> as a fungicide and an insecticide
124  
125 Command => text2sval.pl input.text
126  
127 STDOUT will display =>
128  
129 <corpus lang="english">
130 <lexelt item="LEXELT">
131 <instance id="0">
132 <answer instance="0" senseid="NOTAG"/>
133 <context>
134 the maiden seated herself upon the golden chair and offered the silver one to her companion they were <head>served</head> by maidens dressed in white whose feet made no sound as they moved about and not a word was spoken during the meal
135 </context>
136 </instance>
137 <instance id="1">
138 <answer instance="1" senseid="NOTAG"/>
139 <context>
140 why leftover beef should ever be a problem i cannot understand there is nothing better than cold roast sliced paper thin and <head>served</head> with mustard chutney or pickled walnuts these can be found in almost any food specialty shop meat to be served cold should be removed from the refrigerator an hour or so before eating to allow it to return to room temperature
141 </context>
142 </instance>
143 <instance id="2">
144 <answer instance="2" senseid="NOTAG"/>
145 <context>
146 continue cooking for hours remove the ribs to a hot platter and <head>serve</head> the pan juices separately
147 </context>
148 </instance>
149 <instance id="3">
150 <answer instance="3" senseid="NOTAG"/>
151 <context>
152 an agency spokesman al heier said it granted the exceptions because these crops are grown by few farmers in small areas that can be closely monitored dinoseb is a herbicide that also <head>serves</head> as a fungicide and an insecticide
153 </context>
154 </instance>
155 </lexelt>
156 </corpus>
157  
158 Notice that -
159  
160 1. Since the instance ids are not provided (via --key KEYFILE), text2sval uses
161 ordinal numbers
162 0,1,2 ... etc as the instance ids for the instances in the same order
163 i.e. Instance id assigned to instance at position i in the TEXT file is (i-1)
164  
165 2. Since the sense tags are not provided, all instances are assigned tag 'NOTAG'
166  
167 3. Since --lexelt is not provided, value of <lexelt> tag shows LEXELT i.e. as
168 <lexelt item=\"LEXELT\">
169  
170 =item 2 --key KEY is provided and KEY shows only the instance ids
171  
172 In this case, text2sval uses instance ids from KEY file as the values of
173 instance ids in <instance> and <answer> tags while sense ids will have
174 values NOTAG
175  
176 For TEXT file in example (1),
177  
178 if the KEY file is => serve.key
179  
180 <instance id="serve-v.aphb_34700303_2142"/>
181 <instance id="serve-v.aphb_51903174_3841"/>
182 <instance id="serve-v.aphb_51903399_3856"/>
183 <instance id="serve-v.w7_022806_525"/>
184  
185 Command => text2sval.pl --key serve.key --lexelt serve-v input.text
186  
187 will display on stdout =>
188  
189 <corpus lang="english">
190 <lexelt item="LEXELT">
191 <instance id="serve-v.aphb_34700303_2142">
192 <answer instance="serve-v.aphb_34700303_2142" senseid="NOTAG"/>
193 <context>
194 the maiden seated herself upon the golden chair and offered the silver one to her companion they were <head>served</head> by maidens dressed in white whose feet made no sound as they moved about and not a word was spoken during the meal
195 </context>
196 </instance>
197 <instance id="serve-v.aphb_51903174_3841">
198 <answer instance="serve-v.aphb_51903174_3841" senseid="NOTAG"/>
199 <context>
200 why leftover beef should ever be a problem i cannot understand there is nothing better than cold roast sliced paper thin and <head>served</head> with mustard chutney or pickled walnuts these can be found in almost any food specialty shop meat to be served cold should be removed from the refrigerator an hour or so before eating to allow it to return to room temperature
201 </context>
202 </instance>
203 <instance id="serve-v.aphb_51903399_3856">
204 <answer instance="serve-v.aphb_51903399_3856" senseid="NOTAG"/>
205 <context>
206 continue cooking for hours remove the ribs to a hot platter and <head>serve</head> the pan juices separately
207 </context>
208 </instance>
209 <instance id="serve-v.w7_022806_525">
210 <answer instance="serve-v.w7_022806_525" senseid="NOTAG"/>
211 <context>
212 an agency spokesman al heier said it granted the exceptions because these crops are grown by few farmers in small areas that can be closely monitored dinoseb is a herbicide that also <head>serves</head> as a fungicide and an insecticide
213 </context>
214 </instance>
215 </lexelt>
216 </corpus>
217  
218 Note that the instance ids are taken from the KEY file while sense ids have
219 NOTAGs.
220  
221 =item 3 KEY file contains both the instance and sense tags
222  
223 For TEXT file in example (1),
224  
225 if the KEY file is => serve.key
226  
227 <instance id="serve-v.aphb_34700303_2142"/> <sense id="SERVE10"/>
228 <instance id="serve-v.aphb_51903174_3841"/> <sense id="SERVE10"/>
229 <instance id="serve-v.aphb_51903399_3856"/> <sense id="SERVE10"/>
230 <instance id="serve-v.w7_022806_525"/> <sense id="SERVE2"/>
231  
232 Command => text2sval.pl --key serve.key --lexelt serve-v input.text
233  
234 will display on STDOUT =>
235  
236 <corpus lang="english">
237 <lexelt item="LEXELT">
238 <instance id="serve-v.aphb_34700303_2142">
239 <answer instance="serve-v.aphb_34700303_2142" senseid="SERVE10"/>
240 <context>
241 the maiden seated herself upon the golden chair and offered the silver one to her companion they were <head>served</head> by maidens dressed in white whose feet made no sound as they moved about and not a word was spoken during the meal
242 </context>
243 </instance>
244 <instance id="serve-v.aphb_51903174_3841">
245 <answer instance="serve-v.aphb_51903174_3841" senseid="SERVE10"/>
246 <context>
247 why leftover beef should ever be a problem i cannot understand there is nothing better than cold roast sliced paper thin and <head>served</head> with mustard chutney or pickled walnuts these can be found in almost any food specialty shop meat to be served cold should be removed from the refrigerator an hour or so before eating to allow it to return to room temperature
248 </context>
249 </instance>
250 <instance id="serve-v.aphb_51903399_3856">
251 <answer instance="serve-v.aphb_51903399_3856" senseid="SERVE10"/>
252 <context>
253 continue cooking for hours remove the ribs to a hot platter and <head>serve</head> the pan juices separately
254 </context>
255 </instance>
256 <instance id="serve-v.w7_022806_525">
257 <answer instance="serve-v.w7_022806_525" senseid="SERVE2"/>
258 <context>
259 an agency spokesman al heier said it granted the exceptions because these crops are grown by few farmers in small areas that can be closely monitored dinoseb is a herbicide that also <head>serves</head> as a fungicide and an insecticide
260 </context>
261 </instance>
262 </lexelt>
263 </corpus>
264  
265 where instance ids and sense tags are both extracted from the KEY file.
266  
267 =back
268  
269 =head1 AUTHORS
270  
271 Amruta Purandare, University of Pittsburgh
272  
273 Ted Pedersen, University of Minnesota, Duluth
274 tpederse at d.umn.edu
275  
276 =head1 COPYRIGHT
277  
278 Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen
279  
280 This program is free software; you can redistribute it and/or modify it under
281 the terms of the GNU General Public License as published by the Free Software
282 Foundation; either version 2 of the License, or (at your option) any later
283 version.
284  
285 This program is distributed in the hope that it will be useful, but WITHOUT
286 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
287 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
288  
289 You should have received a copy of the GNU General Public License along with
290 this program; if not, write to
291  
292 The Free Software Foundation, Inc.,
293 59 Temple Place - Suite 330,
294 Boston, MA 02111-1307, USA.
295  
296 =cut
297  
298 ###############################################################################
299  
300 # THE CODE STARTS HERE
301  
302 #$0 contains the program name along with
303 #the complete path. Extract just the program
304 #name and use in error messages
305 $0=~s/.*\/(.+)/$1/;
306  
307 ###############################################################################
308  
309 # ================================
310 # COMMAND LINE OPTIONS AND USAGE
311 # ================================
312  
313 # command line options
314 use Getopt::Long;
315 GetOptions ("help","version","lexelt=s","key=s");
316 # show help option
317 if(defined $opt_help)
318 {
319 $opt_help=1;
320 &showhelp();
321 exit;
322 }
323  
324 # show version information
325 if(defined $opt_version)
326 {
327 $opt_version=1;
328 &showversion();
329 exit;
330 }
331  
332 # show minimal usage message if no arguments
333 if($#ARGV<0)
334 {
335 &showminimal();
336 exit;
337 }
338  
339 # lexelt item
340 if(!defined $opt_lexelt)
341 {
342 $opt_lexelt="LEXELT";
343 }
344  
345 # =======================
346 # KEY handling
347 # =======================
348  
349 if(defined $opt_key)
350 {
351 $keyfile=$opt_key;
352  
353 if(!-e $keyfile)
354 {
355 print STDERR "ERROR($0):
356 KEY file <$keyfile> doesn't exist.\n";
357 exit;
358 }
359
360 open(KEY,$keyfile) || die "Error($0):
361 Error(code=$!) in opening KEY file <$keyfile>.\n";
362  
363 $key_num=0;
364 #read the keyfile to construct a keytable
365 while(<KEY>)
366 {
367 $key_num++;
368 chomp;
369 #blank line
370 if(/^\s*$/)
371 {
372 print STDERR "ERROR($0):
373 Blank line encountered in KEY file <$keyfile> at line <$key_num>.\n";
374 exit;
375 }
376 #get the instance id from the key file
377 if(/<instance id=\"([^\"]+)\"\/>/)
378 {
379 $instance=$1;
380 $_=$';
381 if(defined $instance_hash{$instance})
382 {
383 print STDERR "ERROR($0):
384 Instance-Id <$instance> is repeated in the KEY file <$keyfile>.\n";
385 exit;
386 }
387 $instance_hash{$instance}=1;
388 push @instances,$instance;
389 }
390 else
391 {
392 print STDERR "ERROR($0):
393 Line <$key_num> in the KEY file <$keyfile> should show an <instance>
394 tag.\n";
395 exit;
396 }
397  
398 #get the sense tags
399 while(/<sense id=\"([^\"]+)\"\/>/)
400 {
401 $sense=$1;
402 $_=$';
403 if(defined $key_tab{$instance}{$sense})
404 {
405 print "ERROR($0):
406 The Instance-Id Sense-Tag pair <$instance , $sense> is repeated in
407 the KEY file <$keyfile>.\n";
408 exit;
409 }
410 #making an entry for the instance in the keytab
411 $key_tab{$instance}{$sense}=1;
412 }
413 if($_ !~ /^\s*$/)
414 {
415 print STDERR "ERROR($0):
416 Invalid tag found on line <$key_num> in KEY file <$keyfile> after
417 matching <instance> and <sense> tags.\n";
418 exit;
419 }
420 }
421 }
422 #############################################################################
423  
424 # ================================
425 # INITIALIZATION AND INPUT
426 # ================================
427  
428 if(!defined $ARGV[0])
429 {
430 print STDERR "ERROR($0):
431 Please specify the input TEXT file ...\n";
432 exit;
433 }
434 #accept the input file name
435 $infile=$ARGV[0];
436 if(!-e $infile)
437 {
438 print STDERR "ERROR($0):
439 TEXT file <$infile> doesn't exist...\n";
440 exit;
441 }
442 open(IN,$infile) || die "Error($0):
443 Error(code=$!) in opening <$infile> file.\n";
444  
445 ##############################################################################
446  
447 # ===========================
448 # ACTUAL CONVERSION
449 # ===========================
450  
451 # output is written into a temporary file
452 # if program completes without error,
453 # this temp file is displayed on stdout
454 # and is deleted
455 # otherwise temp file is preserved
456 # and will contain partial program output
457  
458 $tempfile="tempfile" . time() . ".text2sval";
459 if(-e $tempfile)
460 {
461 print STDERR "ERROR($0):
462 Temporary file <$tempfile> already exists.\n";
463 exit;
464 }
465  
466 open(TEMP,">$tempfile") || die "ERROR($0):
467 Error(code=$!) in opening temporary file <$tempfile>.\n";
468  
469 print TEMP "<corpus lang=\"english\">\n";
470 print TEMP "<lexelt item=\"$opt_lexelt\">\n";
471  
472 $line_num=0;
473 while(<IN>)
474 {
475 $line_num++;
476 # trimming extra spaces
477 chomp;
478 # handling blank lines
479 if(/^\s*$/)
480 {
481 print STDERR "ERROR($0):
482 Blank line encountered at line <$line_num> in TEXT file <$infile>.\n";
483 exit;
484 }
485 $data=$_;
486 if(defined $keyfile)
487 {
488 if(defined $instances[$line_num-1])
489 {
490 $instance=$instances[$line_num-1];
491 }
492 else
493 {
494 print STDERR "ERROR($0):
495 No instance id found at line <$line_num> in KEY file <$keyfile>.\n";
496 exit;
497 }
498 }
499 # use the ordinal position of instance as the instance id
500 else
501 {
502 $instance=$line_num-1;
503 }
504 print TEMP "<instance id=\"$instance\">\n";
505 if(defined $keyfile)
506 {
507 if(defined $key_tab{$instance})
508 {
509 foreach $sense (sort keys %{$key_tab{$instance}})
510 {
511 print TEMP "<answer instance=\"$instance\" senseid=\"$sense\"\/>\n";
512 }
513 }
514 else
515 {
516 print TEMP "<answer instance=\"$instance\" senseid=\"NOTAG\"\/>\n";
517 }
518 }
519 else
520 {
521 print TEMP "<answer instance=\"$instance\" senseid=\"NOTAG\"\/>\n";
522 }
523 print TEMP "<context>\n";
524 print TEMP "$data\n";
525 print TEMP "<\/context>\n";
526 print TEMP "<\/instance>\n";
527 }
528  
529 print TEMP "<\/lexelt>\n";
530 print TEMP "<\/corpus>\n";
531  
532 if(defined $keyfile)
533 {
534 if(scalar(@instances) != $line_num)
535 {
536 print STDERR "ERROR($0):
537 Number of entries in the KEY file <" . scalar(@instances). "> do not match the number of instances <$line_num> in the TEXT file.\n";
538 exit;
539 }
540 }
541  
542 # --------------
543 # printing TEMP
544 # --------------
545 close TEMP;
546 open(TEMP,$tempfile) || die "ERROR($0):
547 Error(code=$!) in opening temporary file <$tempfile>.\n";
548  
549 while(<TEMP>)
550 {
551 print;
552 }
553 close TEMP;
554 unlink "$tempfile";
555  
556 ##############################################################################
557  
558 # ==========================
559 # SUBROUTINE SECTION
560 # ==========================
561  
562 #-----------------------------------------------------------------------------
563 #show minimal usage message
564 sub showminimal()
565 {
566 print "Usage: text2sval.pl [OPTIONS] TEXT";
567 print "\nTYPE text2sval.pl --help for help\n";
568 }
569  
570 #-----------------------------------------------------------------------------
571 #show help
572 sub showhelp()
573 {
574 print "Usage: text2sval.pl [OPTIONS] TEXT
575  
576 Converts a plain TEXT instance file into the Senseval-2 formatted XML file.
577  
578 TEXT
579 Plain TEXT instance file showing context of each instance of a target
580 word on a separate line.
581  
582 OPTIONS:
583  
584 --lexelt LEX
585 Specifies the value of the <lexelt> tag to be used in the Senseval-2
586 file. Default is LEXELT.
587  
588 --key KEY
589 Specifies instance ids and optional sense tags of the instances in the
590 TEXT file.
591 Each line in KEY file should show the instance id followed by zero or
592 more sense tags of the instance on the corresponding line in the TEXT
593 file, in the format -
594 <instance id=\"IID\"\/> [<sense id=\"SID\"\/>]*
595  
596 --help
597 Displays this message.
598  
599 --version
600 Displays the version information.
601  
602 Type 'perldoc text2sval.pl' to view detailed documentation of text2sval.\n";
603 }
604  
605 #------------------------------------------------------------------------------
606 #version information
607 sub showversion()
608 {
609 # print "text2sval.pl - Version 0.01\n";
610 print '$Id: text2sval.pl,v 1.9 2008/03/29 23:37:25 tpederse Exp $';
611 print "\nConverts a plain text file into a Senseval-2 formatted XML file.\n";
612 # print "Copyright (c) 2002-2005, Amruta Purandare & Ted Pedersen.\n";
613 # print "Date of Last Update: 12/02/2003\n";
614 }
615  
616 #############################################################################
617