script/maketarget.pl untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 93 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 maketarget.pl - Create target.regex file for a given Senseval-2 data
6 file that shows all the forms of the target word
7  
8 =head1 SYNOPSIS
9  
10 maketarget.pl -head begin.v-test.xml
11  
12 This creates a file called target.regex with the following contents:
13  
14 /<head>\s*(began)|(begin)|(beginning)|(begins)|(begun)\s*</head>/
15  
16 maketarget.pl begin.v-test.xml
17  
18 This creates a file called target.regex with the following contents:
19  
20 /(\bbegan\b)|(\bbegin\b)|(\bbeginning\b)|(\bbegins\b)|(\bbegun\b)/
21
22 These are regular expressions that show all the forms of "begin" that
23 appear in the given Senseval-2 data file with and without a surrounding
24 head tag.
25  
26 You can find begin.v-test.xml at samples/Data
27  
28 Type C<maketarget.pl> for a quick list of options
29  
30 =head1 DESCRIPTION
31  
32 This program creates a Perl regex for the TARGET word by detecting its
33 various forms from the given SVAL2 file.
34  
35 This program will create a regular expression file called target.regex
36 that can be used to match target words via the --target option in many
37 SenseClusters programs. The target.regex file can be of two forms:
38  
39 /<head>\s*(target1|target2)\s*</head>/
40  
41 or
42  
43 /(\btarget1\b)|(\btarget2\b)/
44  
45 The first form is appropriate when the corpus already has the target word
46 marked with head tags, while the second should be used when the corpus is
47 plain unannotated text. The second form is the default, while the first is
48 available with the --head option. Note that in the first form the <head>
49 tag acts as a delimiter on word boundaries, while in the second form the
50 \b character class is used for that purpose.
51  
52 =head1 INPUT
53  
54 =head2 Required Arguments:
55  
56 =head3 SVAL2
57  
58 Should be a file in Senseval-2 format from which various possible forms of the
59 TARGET word are to be detected.
60  
61 =head2 Optional Arguments:
62  
63 =head3 --head
64  
65 Create target word regex in the form: <head>\s*(target1|target2)\s*</head>
66  
67 =head3 --help
68  
69 Displays the summary of command line options.
70  
71 =head3 --version
72  
73 Displays the version information.
74  
75 =head1 OUTPUT
76  
77 maketarget.pl automatically creates the file with name 'target.regex' that
78 shows the Perl regex for the TARGET word. The regex is a OR of various
79 forms of the word detected placed within a single regex, optionally
80 surrounded by <head> and </head> tags.
81  
82 For example: Contents of a sample <target.regex> file:
83  
84 /(\bLine\b)|(\bLines\b)|(\bline\b)|(\blined\b)|(\blines\b)/ (default)
85  
86 /<head>\s*(Line)|(Lines)|(line)|(lined)|(lines)\s*</head>/ (with --head)
87  
88 =head1 BUGS
89  
90 This program does not recognize target words of the form:
91  
92 <head> Bill Clinton </head>
93  
94 It is restricted to target words that are a single string, such as
95  
96 <head> Bill_Clinton </head>
97  
98 =head1 AUTHORS
99  
100 Ted Pedersen, University of Minnesota, Duluth
101 tpederse at d.umn.edu
102  
103 Amruta Purandare, University of Pittsburgh
104  
105 Anagha Kulkarni, Carnegie-Mellon University
106  
107 =head1 COPYRIGHT
108  
109 Copyright (c) 2002-2008, Ted Pedersen, Amurta Purandare, Anagha Kulkarni
110  
111 This program is free software; you can redistribute it and/or modify it under
112 the terms of the GNU General Public License as published by the Free Software
113 Foundation; either version 2 of the License, or (at your option) any later
114 version.
115  
116 This program is distributed in the hope that it will be useful, but WITHOUT
117 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
118 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
119  
120 You should have received a copy of the GNU General Public License along with
121 this program; if not, write to
122  
123 The Free Software Foundation, Inc.,
124 59 Temple Place - Suite 330,
125 Boston, MA 02111-1307, USA.
126  
127 =cut
128  
129 ###############################################################################
130  
131 # THE CODE STARTS HERE
132  
133 #$0 contains the program name along with
134 #the complete path. Extract just the program
135 #name and use in error messages
136 $0=~s/.*\/(.+)/$1/;
137  
138 ###############################################################################
139  
140 # ================================
141 # COMMAND LINE OPTIONS AND USAGE
142 # ================================
143  
144 # command line options
145 use Getopt::Long;
146 GetOptions ("head","help","version");
147  
148 # show help option
149 if(defined $opt_help)
150 {
151 $opt_help=1;
152 &showhelp();
153 exit;
154 }
155  
156 # show version information
157 if(defined $opt_version)
158 {
159 $opt_version=1;
160 &showversion();
161 exit;
162 }
163  
164 # use head tags if --head is given
165  
166 if(defined $opt_head)
167 {
168 $opt_head=1;
169 }
170  
171 # show minimal usage message if no arguments
172 if($#ARGV<0)
173 {
174 &showminimal();
175 exit;
176 }
177  
178 #############################################################################
179  
180 # ================================
181 # INITIALIZATION AND INPUT
182 # ================================
183  
184 #accept the SVAL2 file name
185 $infile=$ARGV[0];
186 if(!-e $infile)
187 {
188 print STDERR "ERROR($0):
189 Source file <$infile> doesn't exist...\n";
190 exit;
191 }
192 open(IN,$infile) || die "Error($0):
193 Error(code=$!) in opening source file <$infile>.\n";
194  
195 ##############################################################################
196  
197 while(<IN>)
198 {
199  
200 # modified by tdp on august 5, 2006 to handle \s* that
201 # may appear inside the head tags
202  
203 # first match the head tags, and then find what is inside
204 # them. Match those to a regex that defines a single
205 # string that is (optionally) preceded and/or followed
206 # by spaces. If that matches, then we have a legal head tag.
207
208 if(/<head>(.*)<\/head>/)
209 {
210 $match = $1;
211 if( $match =~ /\s*(\w+)\s*/)
212 {
213 $target_form{$1}=1;
214 }
215 else
216 {
217 print STDERR "ERROR($0):
218 Target word <$match> doesn't look like a valid word.\n";
219 exit 1;
220 }
221 }
222 }
223  
224 if(%target_form)
225 {
226 $targetfile="target.regex";
227 if(-e $targetfile)
228 {
229 print STDERR "Warning($0):
230 Target File <$targetfile> already exists, overwrite (y/n)? ";
231 $ans=<STDIN>;
232 if($ans !~ /^y|Y/)
233 {
234 exit;
235 }
236 }
237 open(TARGET,">$targetfile") || die "Error($0):
238 Error(code=$!) in opening file <$targetfile>.\n";
239  
240 # modified by TDP August 1, 2006 to use head option
241 # if using head tag, word boundaries delimited by <head>
242 # if not, then should use \b to avoid substring matches
243  
244 if ($opt_head) {
245  
246 # Modified by AKK July 30, 2006.
247 # Added the surrounding head tags to the regex of
248 # target forms.
249
250 # tdp modified $target assignments to single quotes
251 # to avoid interpolation of regex, which seemed to
252 # be causing problem with passed through \s errors
253  
254 $target= '/<head>\s*';
255 foreach $form (sort keys %target_form)
256 {
257 $target.="($form)|";
258 }
259 chop $target;
260 $target.= '\s*</head>/';
261 print TARGET $target;
262 print TARGET "\n";
263 }
264 else {
265 $target="/";
266 foreach $form (sort keys %target_form)
267 {
268 $target.="(\\b$form\\b)|";
269 }
270 chop $target;
271 $target.="/";
272 print TARGET $target;
273 print TARGET "\n";
274 }
275 }
276  
277  
278 ##############################################################################
279  
280 # ==========================
281 # SUBROUTINE SECTION
282 # ==========================
283  
284 #-----------------------------------------------------------------------------
285 #show minimal usage message
286 sub showminimal()
287 {
288 print "Usage: maketarget.pl [OPTIONS] SVAL2";
289 print "\nTYPE maketarget.pl --help for help\n";
290 }
291  
292 #-----------------------------------------------------------------------------
293 #show help
294 sub showhelp()
295 {
296 print "Usage: maketarget.pl [OPTIONS] SVAL2
297  
298 Creates a Perl regex for the TARGET word by detecting its various
299 forms from the given SVAL2 file.
300  
301 SVAL2
302 File in Senseval-2 format from which different forms of the target
303 word are to be searched.
304  
305 OPTIONS:
306 --head
307 Create target word regex as follows: <head>\\s*target\\s*</head>
308 --help
309 Displays this message.
310 --version
311 Displays the version information.
312 Type 'perldoc maketarget.pl' to view detailed documentation of maketarget.\n";
313 }
314  
315 #------------------------------------------------------------------------------
316 #version information
317 sub showversion()
318 {
319 print '$Id: maketarget.pl,v 1.18 2013/06/22 20:28:29 tpederse Exp $';
320 ## print "\nCopyright (c) 2002-2006, Ted Pedersen, Amruta Purandare, & Anagha Kulkarni\n";
321 ## print "maketarget.pl - Version 0.01\n";
322 print "\nCreate a Perl regex for a target word in a Senseval-2 file\n";
323 # print "Date of Last Update: 07/30/2006\n";
324 }
325  
326 #############################################################################
327