| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # THIS PROGRAM ORIGINALLY BELONGS TO THE SenseTools PACKAGE | ||
| 4 | # (http://www.d.umn.edu/~tpederse/sensetools.html) DEVELOPED | ||
| 5 | # BY SATANJEEV BANERJEE AND TED PEDERSEN. IT HAS BEEN | ||
| 6 | # INCLUDED IN SenseClusters DISTRIBUTION AS A CONVENIENCE. | ||
| 7 | |||
| 8 | # BELOW IS THE DOCUMENTATION THAT IS COMPILED FROM THE README | ||
| 9 | # FROM THE SenseTools PACKAGE | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | preprocess.pl - Split Senseval-2 data file into one file per lexical | ||
| 14 | item (lexelt), and carry out various tokenization and formatting tasks | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | preprocess.pl [OPTIONS] SOURCE | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | Takes an xml file in SENSEVAL-2 lexical-sample format and splits it | ||
| 23 | apart into as many files as there are lexical elements in the original | ||
| 24 | file. Each lexical element usually corresponds with a word used in a | ||
| 25 | particular part of speech. It also does other sundry preprocessing | ||
| 26 | tasks with the data such as splitting it into training and test | ||
| 27 | portions, tokenizing, and providing various formatting options. It can | ||
| 28 | also create plain text versions of the xml files, which can be useful | ||
| 29 | when needed as training data. | ||
| 30 | |||
| 31 | =head1 INPUT | ||
| 32 | |||
| 33 | =head2 Required Arguments: | ||
| 34 | |||
| 35 | =head3 SOURCE | ||
| 36 | |||
| 37 | Senseval2 formatted input file. | ||
| 38 | |||
| 39 | =head2 Optional Arguments | ||
| 40 | |||
| 41 | =head4 --token FILE | ||
| 42 | |||
| 43 | Reads tokens from FILE. The context of each instance | ||
| 44 | is broken up into tokens and each pair of consecutive tokens | ||
| 45 | are separated by white space. Non-white space characters | ||
| 46 | which do not belong to any token are put between angular | ||
| 47 | brackets. If this option is not used, the default token | ||
| 48 | definitions of count.pl are assumed. | ||
| 49 | |||
| 50 | =head4 --removeNotToken | ||
| 51 | |||
| 52 | Removes strings that do not match token file. If not\n"; | ||
| 53 | specified, the non-matching strings are put within angular\n"; | ||
| 54 | brackets, ie, <> | ||
| 55 | |||
| 56 | =head4 --nontoken FILE | ||
| 57 | |||
| 58 | Removes all characters sequences that match Perl | ||
| 59 | regular expressions specified in FILE. | ||
| 60 | |||
| 61 | =head4 --noxml | ||
| 62 | |||
| 63 | Does not output an xml file. | ||
| 64 | |||
| 65 | =head4 --xml FILE | ||
| 66 | |||
| 67 | Outputs the changed xml file to FILE. If this option nor | ||
| 68 | the option --noxml is provided, the file name is derived | ||
| 69 | by concatenating the word in the <lexelt> tag with \".xml\". | ||
| 70 | Note: if this option is used, separate lexelt items will | ||
| 71 | not be split into separate files. | ||
| 72 | |||
| 73 | =head4 --nocount | ||
| 74 | |||
| 75 | Does not output a NSP-ready file. | ||
| 76 | |||
| 77 | =head4 --count FILE | ||
| 78 | |||
| 79 | Outputs just the part between <context> </context> (after | ||
| 80 | modification) to FILE. FILE can then be used directly with | ||
| 81 | NSP. If this option nor the option --nocount is provided, | ||
| 82 | the file name is derived as in xml above, with a .count | ||
| 83 | extension. | ||
| 84 | Note: if this option is used, separate lexelt items will | ||
| 85 | not be split into separate files. | ||
| 86 | |||
| 87 | =head4 --useLexelt | ||
| 88 | |||
| 89 | Includes a tag <lexelt=WORD/> within the <head></head> | ||
| 90 | tags, where WORD is the word in the immediately preceeding | ||
| 91 | <lexelt> tag. | ||
| 92 | |||
| 93 | =head4 --useSenseid | ||
| 94 | |||
| 95 | Includes a tag <senseid=XXXXX/> within the <head></head> | ||
| 96 | tags, where XXXXX is the number in the immediately | ||
| 97 | preceeding <answer> tag. | ||
| 98 | |||
| 99 | =head4 --split N | ||
| 100 | |||
| 101 | Shuffles the instances in SOURCE and then splits them into | ||
| 102 | two files, a training file and a test file, approximately | ||
| 103 | in the ratio N:(100-N). | ||
| 104 | |||
| 105 | =head4 --seed N | ||
| 106 | |||
| 107 | Sets the seed for the random number generator used during | ||
| 108 | shuffling. If not used, no seeding is done (except for | ||
| 109 | that provided automatically by perl) | ||
| 110 | |||
| 111 | =head4 --putSentenceTags | ||
| 112 | |||
| 113 | Puts separate lines within the <context> </context> region | ||
| 114 | within <s> </s> pairs of tags. If separate sentences are on | ||
| 115 | seperate lines, these tags effectively denote the start and | ||
| 116 | end of sentences. | ||
| 117 | |||
| 118 | =head4 --version | ||
| 119 | |||
| 120 | Prints the version number. | ||
| 121 | |||
| 122 | =head4 --help | ||
| 123 | |||
| 124 | Prints this help message. | ||
| 125 | |||
| 126 | =head4 --verbose | ||
| 127 | |||
| 128 | Turns on verbose mode. Silent by default. | ||
| 129 | |||
| 130 | =head2 OUTPUT | ||
| 131 | |||
| 132 | 1. The modified/processed Input SENSEVAL-2 (*.xml) file, if --noxml option is not specified. | ||
| 133 | |||
| 134 | 2. The Ngram Statistics Package (NSP) ready (*.count) file, if --nocount is not specified. | ||
| 135 | |||
| 136 | 3. The *-test and *-training files if the --split option is used. | ||
| 137 | |||
| 138 | =head1 An Example SENSEVAL-2 File | ||
| 139 | |||
| 140 | The following is an example SENSEVAL-2 file that we will refer to | ||
| 141 | later in as example.xml | ||
| 142 | |||
| 143 | <corpus lang='english'> | ||
| 144 | <lexelt item="art.n"> | ||
| 145 | <instance id="art.40001"> | ||
| 146 | <answer instance="art.40001" senseid="art~1:06:00::"/> | ||
| 147 | <answer instance="art.40001" senseid="fine_art%1:06:00::"/> | ||
| 148 | <context> | ||
| 149 | <head>Art</head> you can dance to from the creative group | ||
| 150 | called Halo. | ||
| 151 | </context> | ||
| 152 | </instance> | ||
| 153 | <instance id="art.40002"> | ||
| 154 | <answer instance="art.40002" senseid="art_gallery~1:06:00::"/> | ||
| 155 | <context> | ||
| 156 | There's always one to be heard somewhere during the summer in | ||
| 157 | the piazza in front of the <head>art</head>gallery and town | ||
| 158 | hall or in a park. | ||
| 159 | </context> | ||
| 160 | </instance> | ||
| 161 | <instance id="art.40005" docsrc="bnc_ckv_938"> | ||
| 162 | <answer instance="art.40005" senseid="art~1:04:00::"/> | ||
| 163 | <context> | ||
| 164 | Paintings, drawings and sculpture from every period of | ||
| 165 | <head>art</head> during the last 350 years will be on display. | ||
| 166 | </context> | ||
| 167 | </instance> | ||
| 168 | </lexelt> | ||
| 169 | <lexelt item="authority.n"> | ||
| 170 | <instance id="authority.40001"> | ||
| 171 | <answer instance="authority.40001" senseid="authority~1:14:00::"/> | ||
| 172 | <context> | ||
| 173 | Not only is it allowing certain health | ||
| 174 | <head>authorities</head>to waste millions of pounds on | ||
| 175 | computer systems that dont work, it also allowed the London | ||
| 176 | ambulance service to put lives at risk with a system that had | ||
| 177 | not been fully proven in practice. | ||
| 178 | </context> | ||
| 179 | </instance> | ||
| 180 | </lexelt> | ||
| 181 | </corpus> | ||
| 182 | |||
| 183 | Here we have two lexelts, "art.n" and "authority.n", where "n" denotes | ||
| 184 | that these are noun senses of the words. We have three instances of | ||
| 185 | art with instance id's art.40001, art.40002 and art.40007 | ||
| 186 | respectively, and one instance of authority with instance id | ||
| 187 | authority.40001. The first instance has two answers, while the others | ||
| 188 | have one each. | ||
| 189 | |||
| 190 | =head1 Detailed Description | ||
| 191 | |||
| 192 | =head2 Tokenization of Text | ||
| 193 | |||
| 194 | preprocess.pl accepts regular expressions from the user and | ||
| 195 | then "tokenizes" the text between the <context> </context> tags. This | ||
| 196 | is done to simplify the construction of regular expressions in program | ||
| 197 | nsp2regex.pl and to achieve optimum regular expression matching in | ||
| 198 | xml2arff.pl. Following is a description of the tokenization process. | ||
| 199 | |||
| 200 | The text within the <context> </context> tags is considered as one | ||
| 201 | string, the "input" string. This algorithm takes this input string and | ||
| 202 | creates an "output" string where tokens are identified and separated | ||
| 203 | from each other by a SINGLE space. Regex's provided by the user are | ||
| 204 | checked against the input string to see if a sequence of characters | ||
| 205 | starting with the first character of the string match against any of | ||
| 206 | these regex's. As soon as a we find a regular expression that does | ||
| 207 | match, this checking is halted, the matched sequence of characters is | ||
| 208 | removed from the string and appended to an "output" string with | ||
| 209 | exactly one space to its left and right. If none of the regex's match | ||
| 210 | against the starting characters of the input string, the first | ||
| 211 | character is considered a "non-token". By default this non token is | ||
| 212 | placed in angular brackets (<>) and then put into the output string | ||
| 213 | with one space to its left and right. This process is continued until | ||
| 214 | the input string becomes empty. This process is restarted for the next | ||
| 215 | instance. | ||
| 216 | |||
| 217 | For example, assume we provide the following regular expressions to | ||
| 218 | preprocess.pl: | ||
| 219 | |||
| 220 | <head>\w+</head> | ||
| 221 | \w+ | ||
| 222 | |||
| 223 | The first regular expression says that a sequence of characters | ||
| 224 | starting with "<head>", having an unbroken sequence of alphanumeric | ||
| 225 | characters and finally ending with a "</head>" is a valid token. Also, | ||
| 226 | an unbroken sequence of alphanum characters makes a token. | ||
| 227 | |||
| 228 | Then, assuming that the following text occurs within the <context> | ||
| 229 | </context> tags of an instance: | ||
| 230 | |||
| 231 | No, he has no <head>authority</head> on this! | ||
| 232 | |||
| 233 | preprocess.pl would then convert this text to: | ||
| 234 | |||
| 235 | No <,> he has no <head>authority</head> on this <!> | ||
| 236 | |||
| 237 | Observe that "No", "he", "has", "no", "<head>authority</head>", etc | ||
| 238 | are all the tokens, while "," and "!" arent tokens and so have been | ||
| 239 | put into angular brackets. Further observe that each token has exactly | ||
| 240 | one space to its left and right. | ||
| 241 | |||
| 242 | One can provide a file containing regular expressions to preprocess.pl | ||
| 243 | using the switch --token. In this file, each regular expression should | ||
| 244 | be on a line of its own and should be preceeded and succeeded with '/' | ||
| 245 | signs. Further these should be perl regular expressions. | ||
| 246 | |||
| 247 | Thus our regular expressions above would look like so: | ||
| 248 | |||
| 249 | /<head>\w+<\/head>/ | ||
| 250 | /\w+/ | ||
| 251 | |||
| 252 | We shall call the file these regular expressions lie in | ||
| 253 | "token.txt". Then, we would run preprocess.pl on example.xml with this | ||
| 254 | token file like so: | ||
| 255 | |||
| 256 | preprocess.pl example.xml --token token.txt | ||
| 257 | |||
| 258 | =head2 Various Issues of Tokenization wrt preprocess.pl | ||
| 259 | |||
| 260 | =head2 Default Regular Expressions: | ||
| 261 | |||
| 262 | Although | ||
| 263 | tokenization is best controlled via a user specified tokenization file | ||
| 264 | designated via the --token option, there is also a default definition | ||
| 265 | of tokens that is used in the absence of a tokenization file, which | ||
| 266 | consists of the following: | ||
| 267 | |||
| 268 | /w+/ | ||
| 269 | /[\.,;:\?!]/ | ||
| 270 | |||
| 271 | According to this definition, a token is either a single punctuation | ||
| 272 | mark from the specified class, or it is a string of alpha-numeric | ||
| 273 | characters. Note that this default definition is generally not a good | ||
| 274 | choice for XML data since it does not treat XML tags as tokens and | ||
| 275 | will result in them "breaking apart" during pre-processing. For | ||
| 276 | example, given this default definition, the string : | ||
| 277 | |||
| 278 | <head>art</head> | ||
| 279 | |||
| 280 | will be represented by preprocess.pl as | ||
| 281 | |||
| 282 | <<> head <>> art <<> </> head <>> | ||
| 283 | |||
| 284 | which suggests that "<", ">", and "/" are non-tokens, while "art" and | ||
| 285 | "head" are. This is unlikely to provide useful information. | ||
| 286 | |||
| 287 | These defaults correspond to those in NSP, which is geared towards | ||
| 288 | plain text. These are provided as a convenience, but in general we | ||
| 289 | recommend against relying upon them when processing XML data. | ||
| 290 | |||
| 291 | =head2 Regular Expression /\S+/: | ||
| 292 | |||
| 293 | Assume that the only regular expression in our token file token.txt is | ||
| 294 | /\S+/. This regular expression says that any sequence of | ||
| 295 | non-white-space characters is a token. Now, if we run the program like | ||
| 296 | so: | ||
| 297 | |||
| 298 | preprocess.pl example.xml --token token.txt | ||
| 299 | |||
| 300 | (where example.xml is the example xml file described in the previous section | ||
| 301 | and token.txt is the file that contains the above regular | ||
| 302 | expressions /\S+/). | ||
| 303 | |||
| 304 | We would get all the four files, art.n.xml, art.n.count, | ||
| 305 | authority.n.xml and authority.n.count. From here on we shall show only | ||
| 306 | the "authority" files to save space; it is understood that the art | ||
| 307 | files are also created. | ||
| 308 | |||
| 309 | File authority.n.xml: | ||
| 310 | |||
| 311 | <corpus lang='english'> | ||
| 312 | <lexelt item="authority.n"> | ||
| 313 | <instance id="authority.40001"> | ||
| 314 | <answer instance="authority.40001" senseid="authority~1:14:00::"/> | ||
| 315 | <context> | ||
| 316 | |||
| 317 | Not only is it allowing certain health <head>authorities</head>to waste millions of pounds on computer systems that dont work, it also allowed the London ambulance service to put lives at risk with a system that had not been fully proven in practice. | ||
| 318 | </context> | ||
| 319 | </instance> | ||
| 320 | </lexelt> | ||
| 321 | </corpus> | ||
| 322 | |||
| 323 | File authority.n.count: | ||
| 324 | |||
| 325 | Not only is it allowing certain health <head>authorities</head> | ||
| 326 | to waste millions of pounds on computer systems that dont work, | ||
| 327 | it also allowed the London ambulance service to put lives at | ||
| 328 | risk with a system that had not been fully proven in practice. | ||
| 329 | |||
| 330 | Note that every character is a part of some sequence of | ||
| 331 | non-white-space characters, and is therefore part of some token. Hence | ||
| 332 | no character is put into <> brackets. Also, each | ||
| 333 | non-white-space-character-sequence, that is each token, is placed in | ||
| 334 | the output with exactly one space character to its left and right. | ||
| 335 | |||
| 336 | |||
| 337 | =head2 Regular Expression /\w+/: | ||
| 338 | |||
| 339 | On the other hand if our token file token.txt were to contain the | ||
| 340 | following regex which treats every sequence of alpha numeric | ||
| 341 | characters as a token: | ||
| 342 | |||
| 343 | /\w+/ | ||
| 344 | |||
| 345 | ... and we were to run the program like so: | ||
| 346 | |||
| 347 | preprocess.pl example.xml --token token.txt | ||
| 348 | |||
| 349 | ... then our authority files would like like so: | ||
| 350 | |||
| 351 | File authority.n.xml: | ||
| 352 | |||
| 353 | <corpus lang='english'> | ||
| 354 | <lexelt item="authority.n"> | ||
| 355 | <instance id="authority.40001"> | ||
| 356 | <answer instance="authority.40001" senseid="authority~1:14:00::"/> | ||
| 357 | <context> | ||
| 358 | Not only is it allowing certain health <<> head <>> authorities | ||
| 359 | <</> head <>> to waste millions of pounds on computer systems that dont | ||
| 360 | work <,> it also allowed the London ambulance service to put lives at | ||
| 361 | risk with a system that had not been fully proven in practice <.> | ||
| 362 | </context> | ||
| 363 | </instance> | ||
| 364 | </lexelt> | ||
| 365 | </corpus> | ||
| 366 | |||
| 367 | File authority.n.count: | ||
| 368 | |||
| 369 | Not only is it allowing certain health <<> head <>> authorities | ||
| 370 | <</> head <>> to waste millions of pounds on computer systems that | ||
| 371 | dont work <,> it also allowed the London ambulance service to put | ||
| 372 | lives at risk with a system that had not been fully proven in practice <.> | ||
| 373 | |||
| 374 | Note again that since the '<' and '>' of the head tags are not | ||
| 375 | alpha-numeric characters they are considered as "non-token" | ||
| 376 | characters, and are put within the <> tags. Further note that if there | ||
| 377 | are more than one such non-token characters one after another, they | ||
| 378 | get put into one pair of diamond brackets '<' and '>'. As mentioned | ||
| 379 | before, the user should include regular expressions that | ||
| 380 | preserve the tags. Thus for the above example, a regular expression | ||
| 381 | like /<head>\w+<\/head>/ would work admirably. | ||
| 382 | |||
| 383 | =head2 Other Useful Regular Expressions in the Token File: | ||
| 384 | |||
| 385 | Besides the regular expressions <head>\w+</head> and \w+, we have | ||
| 386 | found the following regular expressions useful too. | ||
| 387 | |||
| 388 | /[\.,;:\?!]/ - This states that a single occurrence of one of the | ||
| 389 | puncutation marks in the list is a token. This helps | ||
| 390 | us specify that a puncutation mark is indeed a token | ||
| 391 | and should not be ignored! Further, this allows us to | ||
| 392 | create features consisting of punctuation marks using | ||
| 393 | SenseClusters. | ||
| 394 | |||
| 395 | /&([^;]+;)+/ - The XML format forces us to replace certain meta | ||
| 396 | symbols in the text by their standard formats. For | ||
| 397 | example, if the '<' symbol occurs in the text, it is | ||
| 398 | replaced with "<". Similarly, '-' is replaced with | ||
| 399 | "‐". This regular expression recognizes these | ||
| 400 | constructs as tokens instead of breaking them up! | ||
| 401 | |||
| 402 | =head2 Order of Regular Expressions Is Important: | ||
| 403 | |||
| 404 | Recall that at every point of the "input string", the matching | ||
| 405 | mechanism marches down the regular expressions in the order they are | ||
| 406 | provided in the input regular expression file, and stops at the FIRST | ||
| 407 | regular expression that matches. Thus the order of the regular | ||
| 408 | expression makes a difference. For example, say our regular expression | ||
| 409 | file has the following regular expressions in this order: | ||
| 410 | |||
| 411 | /he/ | ||
| 412 | /hear/ | ||
| 413 | /\w+/ | ||
| 414 | |||
| 415 | and our input text is "hear me" | ||
| 416 | |||
| 417 | Then our output text is " he ar me " | ||
| 418 | |||
| 419 | On the other hand, if we reverse the first two regular expressions | ||
| 420 | |||
| 421 | /hear/ | ||
| 422 | /he/ | ||
| 423 | /\w+/ | ||
| 424 | |||
| 425 | we get as output " hear me " | ||
| 426 | |||
| 427 | Thus as expected, the order of the regular expressions define how the | ||
| 428 | output will look. | ||
| 429 | |||
| 430 | =head2 Redundant Regular Expressions: | ||
| 431 | |||
| 432 | Consider the following regular expressions: | ||
| 433 | |||
| 434 | /\S+/ | ||
| 435 | /\w+/ | ||
| 436 | |||
| 437 | As should be obvious, every token that matches the second regular | ||
| 438 | expression matches the first one too. We say that the first regular | ||
| 439 | expression "subsumes" the second one, and the second regular | ||
| 440 | expression is redundant. This is because the matching mechanism will | ||
| 441 | always stop at the first regular expression, and never get an | ||
| 442 | opportunity to exercise the second one. Note of course that this does | ||
| 443 | not adversely affect anything. | ||
| 444 | |||
| 445 | =head2 Ignoring Non-Tokens using --removeNotToken: | ||
| 446 | |||
| 447 | Recall that characters in the input string that do not match any regular | ||
| 448 | expression as defined in token are put into angular (<>) brackets. You | ||
| 449 | may, if you wish, remove these "non tokens", that is not have them appear | ||
| 450 | in the output xml and count files, by using the switch --removeNotToken. | ||
| 451 | |||
| 452 | Thus, for the following text: | ||
| 453 | |||
| 454 | No, he has no <head>authority</head> on me! | ||
| 455 | |||
| 456 | and with regular expressions | ||
| 457 | |||
| 458 | <head>\w+</head> | ||
| 459 | \w+ | ||
| 460 | |||
| 461 | and if we were to run the program with the switch --removeNotToken, | ||
| 462 | preprocess.pl would convert the text into: | ||
| 463 | |||
| 464 | No he has no <head>authority</head> on me | ||
| 465 | |||
| 466 | =head2 Ignoring Non-Tokens using --nontoken: | ||
| 467 | |||
| 468 | The --nontoken option allows a user to specify a list of regular | ||
| 469 | expressions. Any strings in the input file that match this list | ||
| 470 | are removed from the file prior to tokenization. | ||
| 471 | |||
| 472 | It's important to note the order in which tokenization occurs. | ||
| 473 | First, those strings that match the regexes defined in nontoken | ||
| 474 | are removed. Then the strings that match the regexes defined in | ||
| 475 | token are matched. Those tokens that do not match the token | ||
| 476 | regexes are then removed. Thus, the "order" of precedence during | ||
| 477 | tokenization is: | ||
| 478 | |||
| 479 | -nontoken | ||
| 480 | -token | ||
| 481 | -removeNotToken | ||
| 482 | |||
| 483 | =head2 XML output: | ||
| 484 | |||
| 485 | By default, for each lexical element "word" in the training or test | ||
| 486 | file (in the lexical sample of SENSEVAL-2), preprocess.pl creates a | ||
| 487 | file of the name "word".xml. For example for the file example.xml, | ||
| 488 | preprocess.pl will create files art.n.xml and authority.n.xml if it | ||
| 489 | is run as follows: | ||
| 490 | |||
| 491 | preprocess.pl example.xml --token token.txt | ||
| 492 | |||
| 493 | File art.n.xml: | ||
| 494 | |||
| 495 | <corpus lang='english'> | ||
| 496 | <lexelt item="art.n"> | ||
| 497 | <instance id="art.40001"> | ||
| 498 | <answer instance="art.40001" senseid="art~1:06:00::"/> | ||
| 499 | <answer instance="art.40001" senseid="fine_art%1:06:00::"/> | ||
| 500 | <context> | ||
| 501 | <head>Art</head> you can dance to from the creative group called Halo <.> | ||
| 502 | </context> | ||
| 503 | </instance> | ||
| 504 | <instance id="art.40002"> | ||
| 505 | <answer instance="art.40002" senseid="art_gallery~1:06:00::"/> | ||
| 506 | <context> | ||
| 507 | There <'> s always one to be heard somewhere during the summer in the piazza in front of the <head>art</head> gallery and town hall or in a park <.> | ||
| 508 | </context> | ||
| 509 | </instance> | ||
| 510 | <instance id="art.40005" docsrc="bnc_ckv_938"> | ||
| 511 | <answer instance="art.40005" senseid="art~1:04:00::"/> | ||
| 512 | <context> | ||
| 513 | Paintings <,> drawings and sculpture from every period of <head>art</head> during the last 350 years will be on display <.> | ||
| 514 | </context> | ||
| 515 | </instance> | ||
| 516 | </lexelt> | ||
| 517 | </corpus> | ||
| 518 | |||
| 519 | File authority.n.xml: | ||
| 520 | |||
| 521 | <corpus lang='english'> | ||
| 522 | <lexelt item="authority.n"> | ||
| 523 | <instance id="authority.40001"> | ||
| 524 | <answer instance="authority.40001" senseid="authority~1:14:00::"/> | ||
| 525 | <context> | ||
| 526 | Not only is it allowing certain health <head>authorities</head> to waste millions of pounds on computer systems that dont work <,> it also allowed the London ambulance service to put lives at risk with a system that had not been fully proven in practice <.> | ||
| 527 | </context> | ||
| 528 | </instance> | ||
| 529 | </lexelt> | ||
| 530 | </corpus> | ||
| 531 | |||
| 532 | Observe of course that the text within the <context> </context> region | ||
| 533 | has been tokenized as described previously according to the regular | ||
| 534 | expressions in file token.txt. | ||
| 535 | |||
| 536 | This default behavior can be stopped either by using the switch --xml | ||
| 537 | FILE, by which only one FILE is created, or by using the switch | ||
| 538 | --noxml, by which no xml file is created. | ||
| 539 | |||
| 540 | =head2 Count output: | ||
| 541 | |||
| 542 | Besides creating xml output, this program also creates output that can | ||
| 543 | be used directly with the program count.pl (from the Ngram Statistics | ||
| 544 | Package). After tokenizing the region within the <context> | ||
| 545 | </context> tags of each instance, the program puts together ONLY these | ||
| 546 | pieces of text to create "count.pl ready" output. This is because | ||
| 547 | count.pl assumes that all tokens in the input file needs to be | ||
| 548 | "counted" and generally we are only interested in the "contextual" | ||
| 549 | material provided in each instance, and not the tags that occur | ||
| 550 | outside the <context> </context> region of text. | ||
| 551 | |||
| 552 | By default, for each lexical element "word", this program creates a | ||
| 553 | file of the name word.count. For example, for the file example.xml, we | ||
| 554 | would get the files art.n.count and authority.n.count. | ||
| 555 | |||
| 556 | File art.n.count: | ||
| 557 | |||
| 558 | <head>Art</head> you can dance to from the creative group called Halo <.> | ||
| 559 | There <'> s always one to be heard somewhere during the summer in the piazza in front of the <head>art</head> gallery and town hall or in a park <.> | ||
| 560 | Paintings <,> drawings and sculpture from every period of <head>art</head> during the last 350 years will be on display <.> | ||
| 561 | |||
| 562 | File authority.n.count: | ||
| 563 | |||
| 564 | Not only is it allowing certain health <head>authorities</head> to waste millions of pounds on computer systems that dont work <,> it also allowed the London ambulance service to put lives at risk with a system that had not been fully proven in practice <.> | ||
| 565 | |||
| 566 | This default behavior can be stopped either by using the switch | ||
| 567 | --count FILE, by which only one FILE is created, or by using the | ||
| 568 | switch --nocount, by which no count file is created. | ||
| 569 | |||
| 570 | Note that the --xml/--noxml switches and the --count/--nocount | ||
| 571 | switches are independant of each other. Thus, although providing --xml | ||
| 572 | FILE or --noxml switchs produces a single xml FILE or no xml file at | ||
| 573 | all, you will still get all the count files, unless you also give the | ||
| 574 | --count FILE or --nocount switches. Similarly, providing the --count | ||
| 575 | FILE or --nocount switches does not affect the production of the xml | ||
| 576 | files. | ||
| 577 | |||
| 578 | |||
| 579 | =head2 Information Insertion | ||
| 580 | |||
| 581 | =head3 Inserting lexelt and senseId Information: | ||
| 582 | |||
| 583 | The lexelt information and the senseId information are outside the | ||
| 584 | <context> </context> region. This program gives you the | ||
| 585 | capability to bring these pieces of information inside the context. | ||
| 586 | |||
| 587 | Switch --useLexelt puts the tag <lexelt=WORD/> within the | ||
| 588 | <head></head> tags, where WORD is the word in the immediately | ||
| 589 | preceding <lexelt> tag. | ||
| 590 | |||
| 591 | Switch --useSenseid puts the tag <senseid=XXXXX/> within the | ||
| 592 | <head></head> tags, where XXXXX is the number in the immediately | ||
| 593 | preceding <answer> tag. | ||
| 594 | |||
| 595 | For example, running the program like so: | ||
| 596 | |||
| 597 | preprocess.pl example.xml --useLexelt --useSenseid --token token.txt | ||
| 598 | |||
| 599 | produces this for authority.n.xml: | ||
| 600 | |||
| 601 | <corpus lang='english'> | ||
| 602 | <lexelt item="authority.n"> | ||
| 603 | <instance id="authority.40001"> | ||
| 604 | <answer instance="authority.40001" senseid="authority~1:14:00::"/> | ||
| 605 | <context> | ||
| 606 | Not only is it allowing certain health <head> authorities <lexelt=authority.n/><senseid=authority~1:14:00::/></head> to waste millions of pounds on computer systems that dont work , it also allowed the London ambulance service to put lives at risk with a system that had not been fully proven in practice . | ||
| 607 | </context> | ||
| 608 | </instance> | ||
| 609 | </lexelt> | ||
| 610 | </corpus> | ||
| 611 | |||
| 612 | Note that the extra information is put inside the <head> </head> | ||
| 613 | region. Hence the user has to provide a token file that will preserve | ||
| 614 | these <head> </head> tags. For instance, as shown in the previous | ||
| 615 | section, if one were to rely on the default regex's, these tags would | ||
| 616 | not be preserved (the '<' and '>' would be considered non-token | ||
| 617 | symbols) and the lexelt and senseid information would not be included | ||
| 618 | within the tags. | ||
| 619 | |||
| 620 | So for example, the following regular expression file is adequate: | ||
| 621 | |||
| 622 | <head>\w+</head> | ||
| 623 | \w+ | ||
| 624 | |||
| 625 | =head3 Inserting Sentence-Boundary Tags | ||
| 626 | |||
| 627 | The english lexical sample data available from SENSEVAL-2 is such that | ||
| 628 | each sentence within the <context> </context> tags is on a line of its | ||
| 629 | own. This human-detected sentence boundary information is usually lost | ||
| 630 | in preprocess.pl, but can be preserved using the switch | ||
| 631 | --putSentenceTags. This puts each line within <s> and </s> | ||
| 632 | tags. Assuming that each sentence was originally on a line of its own, | ||
| 633 | then <s> marks the start of a sentence and </s> marks its end. Note | ||
| 634 | that no sentence boundary detection is done: if the end of line | ||
| 635 | character (\n) does not match the end of a sentence, then the <s> </s> | ||
| 636 | tags will not be indicative of a sentence boundary either. | ||
| 637 | |||
| 638 | For example, assume the following is our source xml file, source.xml: | ||
| 639 | |||
| 640 | <corpus lang='english'> | ||
| 641 | <lexelt item="word"> | ||
| 642 | <instance id="word.1"> | ||
| 643 | <answer instance="word.1" senseid="1"/> | ||
| 644 | <context> | ||
| 645 | This is the first line | ||
| 646 | This is the second line | ||
| 647 | This is the last line for <head>word</head> | ||
| 648 | </context> | ||
| 649 | </instance> | ||
| 650 | </lexelt> | ||
| 651 | </corpus> | ||
| 652 | |||
| 653 | Further assume our token file is this: | ||
| 654 | |||
| 655 | /<head>\w+</head>/ | ||
| 656 | /<s>/ | ||
| 657 | /<\/s>/ | ||
| 658 | /\w+/ | ||
| 659 | |||
| 660 | Running preprocess.pl like so: | ||
| 661 | |||
| 662 | preprocess.pl --token token.txt source.xml | ||
| 663 | |||
| 664 | Produces the following word.xml file: | ||
| 665 | |||
| 666 | <corpus lang='english'> | ||
| 667 | <lexelt item="word"> | ||
| 668 | <instance id="word.1"> | ||
| 669 | <answer instance="word.1" senseid="1"/> | ||
| 670 | <context> | ||
| 671 | This is the first line This is the second line This is the last line for <head>word</head> | ||
| 672 | </context> | ||
| 673 | </instance> | ||
| 674 | </lexelt> | ||
| 675 | </corpus> | ||
| 676 | |||
| 677 | and the following word.count file: | ||
| 678 | |||
| 679 | This is the first line This is the second line This is the last line for <head>word</head> | ||
| 680 | |||
| 681 | However, running preprocess.pl like so: | ||
| 682 | |||
| 683 | preprocess.pl --token token.txt --putSentenceTags source.xml | ||
| 684 | |||
| 685 | Produces the following word.xml file: | ||
| 686 | |||
| 687 | <corpus lang='english'> | ||
| 688 | <lexelt item="word"> | ||
| 689 | <instance id="word.1"> | ||
| 690 | <answer instance="word.1" senseid="1"/> | ||
| 691 | <context> | ||
| 692 | <s> This is the first line </s> <s> This is the second line </s> <s> This is the last line for <head>word</head> </s> | ||
| 693 | </context> | ||
| 694 | </instance> | ||
| 695 | </lexelt> | ||
| 696 | </corpus> | ||
| 697 | |||
| 698 | and the following word.count file: | ||
| 699 | |||
| 700 | <s> This is the first line </s> <s> This is the second line </s> <s> This is the last line for <head>word</head> </s> | ||
| 701 | |||
| 702 | Note that the <s> and </s> tags are placed into the data BEFORE the | ||
| 703 | tokenization process. Hence a token regular expression that preserves | ||
| 704 | these tags is required! The token file shown above is adequate for | ||
| 705 | this. | ||
| 706 | |||
| 707 | =head2 Splitting Input Lexical Files | ||
| 708 | |||
| 709 | Besides splitting the lexical elements into separate files, | ||
| 710 | preprocess.pl also allows you to split the instances of a single | ||
| 711 | lexical element into separate "training" and "test" files. | ||
| 712 | |||
| 713 | If one has a corpus of sense-tagged text, it is | ||
| 714 | often desirable to divide that sense tagged text into training and | ||
| 715 | test portions in order to develop or tune a methodology. This is the | ||
| 716 | intention of the --split option. | ||
| 717 | |||
| 718 | The --split option of preprocess.pl allows you to specify an integer | ||
| 719 | N... the instances of each lexical element in the input XML SOURCE | ||
| 720 | file are split into two files approximately in the ratio N:(100-N). | ||
| 721 | |||
| 722 | If an output XML file "foo" is specified through the switch --xml then | ||
| 723 | two files, foo-training.xml and foo-test.xml are created. | ||
| 724 | |||
| 725 | If an output count file "foo" is specified through the switch --count | ||
| 726 | then two files, foo-training.count and foo-test.count are created. | ||
| 727 | |||
| 728 | Creation of Xml and count output files can be suppressed by using the | ||
| 729 | --noxml and --nocount switches respectively. | ||
| 730 | |||
| 731 | If neither --noxml nor --xml switches are used, then files of the type | ||
| 732 | word-training.xml, word-test.xml are created. | ||
| 733 | |||
| 734 | If neither --nocount nor --count switches are used, then files of the | ||
| 735 | type word-training.count, word-test.count are created. | ||
| 736 | |||
| 737 | The instances are shuffled before being put into training and test | ||
| 738 | files. Perl automatically seeds the randomizing process... but you can | ||
| 739 | specify your own seed using the switch --seed. | ||
| 740 | |||
| 741 | =head1 AUTHORS | ||
| 742 | |||
| 743 | Satanjeev Banerjee, Carnegie-Mellon University | ||
| 744 | |||
| 745 | Ted Pedersen, University of Minnesota, Duluth | ||
| 746 | tpederse at d.umn.edu | ||
| 747 | |||
| 748 | =head1 COPYRIGHT | ||
| 749 | |||
| 750 | Copyright (c) 2001-2008, Satanjeev Banerjee and Ted Pedersen | ||
| 751 | |||
| 752 | This program is free software; you can redistribute it and/or modify it under | ||
| 753 | the terms of the GNU General Public License as published by the Free Software | ||
| 754 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 755 | version. | ||
| 756 | |||
| 757 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 758 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 759 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 760 | |||
| 761 | You should have received a copy of the GNU General Public License along with | ||
| 762 | this program; if not, write to | ||
| 763 | |||
| 764 | The Free Software Foundation, Inc., | ||
| 765 | 59 Temple Place - Suite 330, | ||
| 766 | Boston, MA 02111-1307, USA. | ||
| 767 | |||
| 768 | =cut | ||
| 769 | |||
| 770 | # preprocess.pl version 0.3 | ||
| 771 | # | ||
| 772 | # Program to take a Senseval training data file and convert it into the format | ||
| 773 | # required by xml2arff and, optionally, by NSP | ||
| 774 | # | ||
| 775 | # | ||
| 776 | ############################################################################### | ||
| 777 | # | ||
| 778 | # ------- CHANGELOG --------- | ||
| 779 | # | ||
| 780 | # version date programmer List of changes change-id | ||
| 781 | # | ||
| 782 | # 0.3 01/18/2003 Amruta Introduced | ||
| 783 | # --nontoken option ADP.3.1 | ||
| 784 | # 0.3 05/10/2003 Ted split loop fix TDP.3.1 | ||
| 785 | # | ||
| 786 | ############################################################################### | ||
| 787 | # | ||
| 788 | # Start of program | ||
| 789 | #----------------------------------------------------------------------------- | ||
| 790 | |||
| 791 | # we have to use commandline options, so use the necessary package! | ||
| 792 | use Getopt::Long; | ||
| 793 | |||
| 794 | # first check if no commandline options have been provided... in which case | ||
| 795 | # print out the usage notes! | ||
| 796 | if ( $#ARGV == -1 ) | ||
| 797 | { | ||
| 798 | &minimalUsageNotes(); | ||
| 799 | exit; | ||
| 800 | } | ||
| 801 | |||
| 802 | # now get the options! | ||
| 803 | GetOptions("version", "help", "verbose", "useLexelt", "useSenseid", "count=s", | ||
| 804 | "xml=s", "noxml", "nocount", "split=i", "token=s", "seed=i", "removeNotToken", | ||
| 805 | "putSentenceTags","nontoken=s"); | ||
| 806 | |||
| 807 | # if help has been requested, print out help! | ||
| 808 | if ( defined $opt_help ) | ||
| 809 | { | ||
| 810 | $opt_help = $opt_help; | ||
| 811 | &showHelp(); | ||
| 812 | exit; | ||
| 813 | } | ||
| 814 | |||
| 815 | # if version has been requested, show version! | ||
| 816 | if ( defined $opt_version ) | ||
| 817 | { | ||
| 818 | $opt_version = $opt_version; | ||
| 819 | &showVersion(); | ||
| 820 | exit; | ||
| 821 | } | ||
| 822 | |||
| 823 | $opt_removeNotToken = 1 if ( defined $opt_removeNotToken); | ||
| 824 | |||
| 825 | # seed if defined | ||
| 826 | if ( defined $opt_seed) | ||
| 827 | { | ||
| 828 | srand($opt_seed); | ||
| 829 | } | ||
| 830 | |||
| 831 | # create count-style tokenizer regex from -token file, if supplied, or by using | ||
| 832 | # the defaults that count uses. | ||
| 833 | |||
| 834 | if ( defined $opt_token ) | ||
| 835 | { | ||
| 836 | if ( !( -e $opt_token)) | ||
| 837 | { | ||
| 838 | print "Cant find token definition file $opt_token.\n"; | ||
| 839 | askHelp(); | ||
| 840 | exit; | ||
| 841 | } | ||
| 842 | |||
| 843 | open TOKEN, $opt_token || die "Couldnt open $opt_token\n"; | ||
| 844 | |||
| 845 | while(<TOKEN>) | ||
| 846 | { | ||
| 847 | chomp; | ||
| 848 | s/^\s*//g; | ||
| 849 | s/\s*$//g; | ||
| 850 | if (length($_) <= 0) { next; } | ||
| 851 | if (!(/^\//) || !(/\/$/)) | ||
| 852 | { | ||
| 853 | print STDERR "Ignoring regex with no delimiters: $_\n"; | ||
| 854 | next; | ||
| 855 | } | ||
| 856 | s/^\///; | ||
| 857 | s/\/$//; | ||
| 858 | push @tokenRegex, $_; | ||
| 859 | } | ||
| 860 | close TOKEN; | ||
| 861 | } | ||
| 862 | else | ||
| 863 | { | ||
| 864 | push @tokenRegex, "\\w+"; | ||
| 865 | push @tokenRegex, "[\.,;:\?!]"; | ||
| 866 | } | ||
| 867 | |||
| 868 | # create the complete token regex | ||
| 869 | $tokenizerRegex = ""; | ||
| 870 | |||
| 871 | foreach $token (@tokenRegex) | ||
| 872 | { | ||
| 873 | if ( length($tokenizerRegex) > 0 ) | ||
| 874 | { | ||
| 875 | $tokenizerRegex .= "|"; | ||
| 876 | } | ||
| 877 | $tokenizerRegex .= "("; | ||
| 878 | $tokenizerRegex .= $token; | ||
| 879 | $tokenizerRegex .= ")"; | ||
| 880 | } | ||
| 881 | |||
| 882 | # --------------- | ||
| 883 | # ADP.3.1 start | ||
| 884 | # --------------- | ||
| 885 | # Introducing --nontoken option to specify what is not a valid token. | ||
| 886 | # The designated file contains regular expressions that define strings | ||
| 887 | # that should not be considered tokens. Any string that matches these | ||
| 888 | # will be removed. Note that nontoken take precedence over token. | ||
| 889 | |||
| 890 | if(defined $opt_nontoken) | ||
| 891 | { | ||
| 892 | #check if the file exists | ||
| 893 | if(-e $opt_nontoken) | ||
| 894 | { | ||
| 895 | #open the non token file | ||
| 896 | open(NOTOK,"$opt_nontoken") || die "Couldn't open Nontoken file $opt_nontoken.\n"; | ||
| 897 | while(<NOTOK>) | ||
| 898 | { | ||
| 899 | chomp; | ||
| 900 | s/^\s+//; | ||
| 901 | s/\s+$//; | ||
| 902 | #handling a blank lines | ||
| 903 | if(/^\s*$/) | ||
| 904 | { | ||
| 905 | next; | ||
| 906 | } | ||
| 907 | |||
| 908 | if(!(/^\//)) | ||
| 909 | { | ||
| 910 | print STDERR "Nontoken regular expression $_ should start with '/'\n"; | ||
| 911 | exit; | ||
| 912 | } | ||
| 913 | if(!(/\/$/)) | ||
| 914 | { | ||
| 915 | print STDERR "Nontoken regular expression $_ should end with '/'\n"; | ||
| 916 | exit; | ||
| 917 | } | ||
| 918 | #removing the / s from the beginning and the end | ||
| 919 | s/^\///; | ||
| 920 | s/\/$//; | ||
| 921 | #form a single regex | ||
| 922 | $non_token_regex.="(".$_.")|"; | ||
| 923 | } | ||
| 924 | # if no valid regexs are found in Nontoken file | ||
| 925 | if(length($non_token_regex)<=0) | ||
| 926 | { | ||
| 927 | print STDERR "No valid Perl Regular Experssion found in Nontoken file $opt_nontoken.\n"; | ||
| 928 | exit; | ||
| 929 | } | ||
| 930 | chop $non_token_regex; | ||
| 931 | } | ||
| 932 | else | ||
| 933 | { | ||
| 934 | print STDERR "Nontoken file $opt_nontoken doesn't exist.\n"; | ||
| 935 | exit; | ||
| 936 | } | ||
| 937 | } | ||
| 938 | # End of --nontoken option functionality | ||
| 939 | # ------------- | ||
| 940 | # ADP.3.1 end | ||
| 941 | # ------------- | ||
| 942 | |||
| 943 | # if count file has been specified, try to open it for writing! | ||
| 944 | if ( defined $opt_count ) | ||
| 945 | { | ||
| 946 | # if split not defined, open normally... | ||
| 947 | if ( !defined $opt_split ) | ||
| 948 | { | ||
| 949 | if ( -e $opt_count ) | ||
| 950 | { | ||
| 951 | print "File $opt_count exists! Overwrite (Y/N)? "; | ||
| 952 | my $reply = <STDIN>; | ||
| 953 | chomp $reply; | ||
| 954 | $reply = uc $reply; | ||
| 955 | exit 0 if ($reply ne "Y"); | ||
| 956 | } | ||
| 957 | open ( COUNT, ">$opt_count" ) || die "Couldnt open $opt_count\n"; | ||
| 958 | } | ||
| 959 | # else open two separate file: training and test! | ||
| 960 | else | ||
| 961 | { | ||
| 962 | my $training = $opt_count . "-training"; | ||
| 963 | if ( -e $training ) | ||
| 964 | { | ||
| 965 | print "File $training exists! Overwrite (Y/N)? "; | ||
| 966 | my $reply = <STDIN>; | ||
| 967 | chomp $reply; | ||
| 968 | $reply = uc $reply; | ||
| 969 | exit 0 if ( $reply ne "Y" ); | ||
| 970 | } | ||
| 971 | open ( COUNT_TRAIN, ">$training" ) || die "Couldnt open $training\n"; | ||
| 972 | |||
| 973 | my $test = $opt_count . "-test"; | ||
| 974 | if ( -e $test ) | ||
| 975 | { | ||
| 976 | print "File $test exists! Overwrite (Y/N)? "; | ||
| 977 | my $reply = <STDIN>; | ||
| 978 | chomp $reply; | ||
| 979 | $reply = uc $reply; | ||
| 980 | exit 0 if ( $reply ne "Y" ); | ||
| 981 | } | ||
| 982 | open ( COUNT_TEST, ">$test" ) || die "Couldnt open $test\n"; | ||
| 983 | } | ||
| 984 | } | ||
| 985 | |||
| 986 | # if xml file has been specified, try to open it for writing! | ||
| 987 | if ( defined $opt_xml ) | ||
| 988 | { | ||
| 989 | if ( !defined $opt_split ) | ||
| 990 | { | ||
| 991 | if ( -e $opt_xml ) | ||
| 992 | { | ||
| 993 | print "File $opt_xml exists! Overwrite (Y/N)? "; | ||
| 994 | $reply = <STDIN>; | ||
| 995 | chomp $reply; | ||
| 996 | $reply = uc $reply; | ||
| 997 | exit 0 if ($reply ne "Y"); | ||
| 998 | } | ||
| 999 | open ( XML, ">$opt_xml" ) || die "Couldnt open $opt_xml\n"; | ||
| 1000 | } | ||
| 1001 | # else open two separate file: training and test! | ||
| 1002 | else | ||
| 1003 | { | ||
| 1004 | my $training = $opt_xml . "-training"; | ||
| 1005 | if ( -e $training ) | ||
| 1006 | { | ||
| 1007 | print "File $training exists! Overwrite (Y/N)? "; | ||
| 1008 | my $reply = <STDIN>; | ||
| 1009 | chomp $reply; | ||
| 1010 | $reply = uc $reply; | ||
| 1011 | exit 0 if ( $reply ne "Y" ); | ||
| 1012 | } | ||
| 1013 | open ( XML_TRAIN, ">$training" ) || die "Couldnt open $training\n"; | ||
| 1014 | |||
| 1015 | my $test = $opt_xml . "-test"; | ||
| 1016 | if ( -e $test ) | ||
| 1017 | { | ||
| 1018 | print "File $test exists! Overwrite (Y/N)? "; | ||
| 1019 | my $reply = <STDIN>; | ||
| 1020 | chomp $reply; | ||
| 1021 | $reply = uc $reply; | ||
| 1022 | exit 0 if ( $reply ne "Y" ); | ||
| 1023 | } | ||
| 1024 | open ( XML_TEST, ">$test" ) || die "Couldnt open $test\n"; | ||
| 1025 | } | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | if ( defined $opt_noxml && defined $opt_nocount ) | ||
| 1029 | { | ||
| 1030 | print "Cannot simultaneously specify both -noxml and -nocount.\n"; | ||
| 1031 | askHelp(); | ||
| 1032 | exit; | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | # now starts the real fun! | ||
| 1036 | # first get the header that precedes <corpus.. | ||
| 1037 | |||
| 1038 | # get the file name | ||
| 1039 | $sourceXMLFile = shift; | ||
| 1040 | |||
| 1041 | if (!defined $sourceXMLFile) | ||
| 1042 | { | ||
| 1043 | print "Source file not provided.\n"; | ||
| 1044 | askHelp(); | ||
| 1045 | exit; | ||
| 1046 | } | ||
| 1047 | |||
| 1048 | open(SRC, $sourceXMLFile) || die("Couldnt open file $sourceXMLFile"); | ||
| 1049 | |||
| 1050 | @header = (); | ||
| 1051 | $gotHeader = 0; | ||
| 1052 | |||
| 1053 | $lineFromSource = <SRC>; | ||
| 1054 | |||
| 1055 | while ($lineFromSource && ($gotHeader == 0)) | ||
| 1056 | |||
| 1057 | { | ||
| 1058 | |||
| 1059 | # since this line could have multiple tags, we will split the line | ||
| 1060 | # such that each line has at most one tag. note, in the split | ||
| 1061 | # function below, the brackets return the tags we are splitting | ||
| 1062 | # on, besides the stuff within the tags | ||
| 1063 | |||
| 1064 | ## TDP 3.1 START | ||
| 1065 | ## Statement (S1) causes split loop error on linux, but not Solaris! | ||
| 1066 | ## | ||
| 1067 | ## the regex is based on the following definition of a tag - any string | ||
| 1068 | ## that starts with a <, ends with a > and includes any characters except | ||
| 1069 | ## >. Thus, <<<> is considered a tag. The string <<>> is split into | ||
| 1070 | ## <<> and >. The motivation for this is to make sure that each line | ||
| 1071 | ## only has one tag. Curiously enough, the regex does not seem to | ||
| 1072 | ## be the problem. The split loop error does not occur on linux when | ||
| 1073 | ## the input to the split statement is in $_. This doesn't make sense | ||
| 1074 | ## to me, but I have hacked things such that split reads from $_. | ||
| 1075 | ## | ||
| 1076 | ## @separatedLines = split/(<[^>]*>)/, $lineFromSource; (S1) | ||
| 1077 | ## | ||
| 1078 | |||
| 1079 | $_ = $lineFromSource; ## TDP.3.1 | ||
| 1080 | |||
| 1081 | @separatedLines = split/(<[^>]+>)/; | ||
| 1082 | |||
| 1083 | while (defined ($thisLine = shift @separatedLines)) | ||
| 1084 | { | ||
| 1085 | chomp $thisLine; | ||
| 1086 | if ($thisLine eq "") { next; } | ||
| 1087 | |||
| 1088 | if ( defined $opt_xml ) | ||
| 1089 | { | ||
| 1090 | if ( defined $opt_split ) | ||
| 1091 | { | ||
| 1092 | print XML_TRAIN "$thisLine\n"; | ||
| 1093 | print XML_TEST "$thisLine\n"; | ||
| 1094 | } | ||
| 1095 | else { print XML "$thisLine\n"; } | ||
| 1096 | } | ||
| 1097 | if ( $thisLine =~ /<corpus / ) | ||
| 1098 | { | ||
| 1099 | $gotHeader = 1; | ||
| 1100 | $corpus = "$thisLine\n"; | ||
| 1101 | last; | ||
| 1102 | } | ||
| 1103 | push @header, "$thisLine\n"; | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | $lineFromSource = <SRC> if (!$gotHeader); | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | if (!defined $corpus) | ||
| 1110 | { | ||
| 1111 | print "Could not find <corpus> in input file!\n"; | ||
| 1112 | exit; | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | # now to get the lexical elts till the end of the file | ||
| 1116 | while (defined ($lineFromSource = <SRC>) || defined ($separatedLines[0])) | ||
| 1117 | { | ||
| 1118 | |||
| 1119 | $_ = $lineFromSource; ## TDP.3.1 | ||
| 1120 | |||
| 1121 | ## push @separatedLines, split/(<[^>]*>)/, $lineFromSource; ## TDP.3.1 | ||
| 1122 | |||
| 1123 | push @separatedLines, split/(<[^>]*>)/ ## TDP.3.1 | ||
| 1124 | if (defined $lineFromSource); | ||
| 1125 | |||
| 1126 | my $foundLexelt = 0; | ||
| 1127 | |||
| 1128 | while ( defined ($thisLine = shift @separatedLines)) | ||
| 1129 | { | ||
| 1130 | if ($thisLine =~ /^<lexelt / ) | ||
| 1131 | { | ||
| 1132 | $foundLexelt = 1; | ||
| 1133 | last; | ||
| 1134 | } | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | next if ($foundLexelt == 0); | ||
| 1138 | |||
| 1139 | # right, so we've got <lexelt>. | ||
| 1140 | # first get the lexelt word | ||
| 1141 | |||
| 1142 | $thisLine =~ /<lexelt item="([^\"]*)">/; | ||
| 1143 | $lexeltWord = $1; | ||
| 1144 | |||
| 1145 | # if xml option has not been taken, create the xml filename using the lexelt word | ||
| 1146 | if ( !(defined $opt_xml) && !(defined $opt_noxml) ) | ||
| 1147 | { | ||
| 1148 | if ( !defined $opt_split ) | ||
| 1149 | { | ||
| 1150 | my $filename = $lexeltWord . ".xml"; | ||
| 1151 | |||
| 1152 | if ( $filename eq $sourceXMLFile ) | ||
| 1153 | { | ||
| 1154 | print "Default output XML filename $filename clashes with source XML filename.\n"; | ||
| 1155 | print "Create output in file ${filename}1? (Y = Yes / N = abort) "; | ||
| 1156 | $reply = <STDIN>; | ||
| 1157 | chomp $reply; | ||
| 1158 | $reply = uc $reply; | ||
| 1159 | exit 0 if ($reply ne "Y"); | ||
| 1160 | $filename .= "1"; | ||
| 1161 | } | ||
| 1162 | if ( -e $filename ) | ||
| 1163 | { | ||
| 1164 | print "File $filename exists! Overwrite (Y/N)? "; | ||
| 1165 | $reply = <STDIN>; | ||
| 1166 | chomp $reply; | ||
| 1167 | $reply = uc $reply; | ||
| 1168 | exit 0 if ($reply ne "Y"); | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | open XML, ">$filename" || die "Couldnt open file $filename\n"; | ||
| 1172 | |||
| 1173 | print XML @header; | ||
| 1174 | print XML $corpus; | ||
| 1175 | } | ||
| 1176 | else | ||
| 1177 | { | ||
| 1178 | my $training = $lexeltWord . "-training.xml"; | ||
| 1179 | |||
| 1180 | if ( -e $training ) | ||
| 1181 | { | ||
| 1182 | print "File $training exists! Overwrite (Y/N)? "; | ||
| 1183 | my $reply = <STDIN>; | ||
| 1184 | chomp $reply; | ||
| 1185 | $reply = uc $reply; | ||
| 1186 | exit 0 if ( $reply ne "Y" ); | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | open XML_TRAIN, ">$training" || die "Couldnt open file $training\n"; | ||
| 1190 | |||
| 1191 | print XML_TRAIN @header; | ||
| 1192 | print XML_TRAIN $corpus; | ||
| 1193 | |||
| 1194 | my $test = $lexeltWord . "-test.xml"; | ||
| 1195 | |||
| 1196 | if ( -e $test ) | ||
| 1197 | { | ||
| 1198 | print "File $test exists! Overwrite (Y/N)? "; | ||
| 1199 | my $reply = <STDIN>; | ||
| 1200 | chomp $reply; | ||
| 1201 | $reply = uc $reply; | ||
| 1202 | exit 0 if ( $reply ne "Y" ); | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | open XML_TEST, ">$test" || die "Couldnt open file $test\n"; | ||
| 1206 | |||
| 1207 | print XML_TEST @header; | ||
| 1208 | print XML_TEST $corpus; | ||
| 1209 | } | ||
| 1210 | } | ||
| 1211 | |||
| 1212 | # print out the lexelt tag | ||
| 1213 | if ( !defined ($opt_noxml) ) | ||
| 1214 | { | ||
| 1215 | if ( defined $opt_split ) | ||
| 1216 | { | ||
| 1217 | print XML_TRAIN "$thisLine\n"; | ||
| 1218 | print XML_TEST "$thisLine\n"; | ||
| 1219 | } | ||
| 1220 | else { print XML "$thisLine\n"; } | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | # also create the count file, if not already opened | ||
| 1224 | if ( !( defined $opt_count ) && !(defined $opt_nocount) ) | ||
| 1225 | { | ||
| 1226 | if ( !defined $opt_split ) | ||
| 1227 | { | ||
| 1228 | my $filename = $lexeltWord . ".count"; | ||
| 1229 | |||
| 1230 | if ( $filename eq $sourceXMLFile ) | ||
| 1231 | { | ||
| 1232 | print "Default output count filename $filename clashes with source XML filename.\n"; | ||
| 1233 | print "Create output in file ${filename}1? (Y = Yes / N = abort) "; | ||
| 1234 | $reply = <STDIN>; | ||
| 1235 | chomp $reply; | ||
| 1236 | $reply = uc $reply; | ||
| 1237 | exit 0 if ($reply ne "Y"); | ||
| 1238 | $filename .= "1"; | ||
| 1239 | } | ||
| 1240 | if ( -e $filename ) | ||
| 1241 | { | ||
| 1242 | print "File $filename exists! Overwrite (Y/N)? "; | ||
| 1243 | $reply = <STDIN>; | ||
| 1244 | chomp $reply; | ||
| 1245 | $reply = uc $reply; | ||
| 1246 | exit 0 if ($reply ne "Y"); | ||
| 1247 | } | ||
| 1248 | |||
| 1249 | open COUNT, ">$filename" || die "Couldnt open file $filename\n"; | ||
| 1250 | } | ||
| 1251 | else | ||
| 1252 | { | ||
| 1253 | my $training = $lexeltWord . "-training.count"; | ||
| 1254 | |||
| 1255 | if ( -e $training ) | ||
| 1256 | { | ||
| 1257 | print "File $training exists! Overwrite (Y/N)? "; | ||
| 1258 | my $reply = <STDIN>; | ||
| 1259 | chomp $reply; | ||
| 1260 | $reply = uc $reply; | ||
| 1261 | exit 0 if ( $reply ne "Y" ); | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | open COUNT_TRAIN, ">$training" || die "Couldnt open file $training\n"; | ||
| 1265 | |||
| 1266 | my $test = $lexeltWord . "-test.count"; | ||
| 1267 | |||
| 1268 | if ( -e $test ) | ||
| 1269 | { | ||
| 1270 | print "File $test exists! Overwrite (Y/N)? "; | ||
| 1271 | my $reply = <STDIN>; | ||
| 1272 | chomp $reply; | ||
| 1273 | $reply = uc $reply; | ||
| 1274 | exit 0 if ( $reply ne "Y" ); | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | open COUNT_TEST, ">$test" || die "Couldnt open file $test\n"; | ||
| 1278 | } | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | # now do the following until we get to </lexelt> | ||
| 1282 | # removed by tdp oct 3, 2015 due to perl deprecation of defined @array | ||
| 1283 | # if ( defined @instanceArrayXml ) { undef @instanceArrayXml; } | ||
| 1284 | # if ( defined @instanceArrayCount ) { undef @instanceArrayCount; } | ||
| 1285 | |||
| 1286 | $instanceStringXml = ""; | ||
| 1287 | $instanceStringCount = ""; | ||
| 1288 | |||
| 1289 | $foundLexelt = 0; | ||
| 1290 | $insideContext = 0; | ||
| 1291 | |||
| 1292 | while ( !$foundLexelt ) | ||
| 1293 | { | ||
| 1294 | while (!(defined ($separatedLines[0]))) | ||
| 1295 | { | ||
| 1296 | my $lineFromSource = <SRC>; | ||
| 1297 | if ( !defined $lineFromSource ) | ||
| 1298 | { | ||
| 1299 | print STDERR "ERROR!! Premature end to source file!\n"; | ||
| 1300 | exit; | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | $_ = $lineFromSource; ## TDP.3.1 | ||
| 1304 | @separatedLines = split/(<[^>]*>)/; ## TDP.3.1 | ||
| 1305 | |||
| 1306 | ## @separatedLines = split/(<[^>]*>)/, $lineFromSource; TDP.3.1 | ||
| 1307 | |||
| 1308 | } | ||
| 1309 | |||
| 1310 | while (defined ($thisLine = shift @separatedLines)) | ||
| 1311 | { | ||
| 1312 | # chomp $thisLine; | ||
| 1313 | |||
| 1314 | # if outside the context... | ||
| 1315 | if (!$insideContext) | ||
| 1316 | { | ||
| 1317 | # then it better be a tag! | ||
| 1318 | chomp $thisLine; | ||
| 1319 | |||
| 1320 | if ( $thisLine !~ /<[^>]*>/ ) { next; } | ||
| 1321 | |||
| 1322 | if ( !defined $opt_noxml ) | ||
| 1323 | { | ||
| 1324 | if ( defined $opt_split ) | ||
| 1325 | { | ||
| 1326 | if ( !( $thisLine =~ /<\/instance>/) ) | ||
| 1327 | { | ||
| 1328 | $instanceStringXml .= $thisLine . "\n"; | ||
| 1329 | } | ||
| 1330 | } | ||
| 1331 | else { print XML "$thisLine\n"; } | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | # if its an answer tag, put the senseid into the array! | ||
| 1335 | if ( $thisLine =~ /<answer instance=\"[^\"]*\" senseid=\"([^\"]*)\"/ ) | ||
| 1336 | { | ||
| 1337 | my $senseid = $1; | ||
| 1338 | push @answers, $senseid; | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | if ( $thisLine =~ /<context/ ) | ||
| 1342 | { | ||
| 1343 | $contextLine = ""; | ||
| 1344 | $insideContext = 1; | ||
| 1345 | } | ||
| 1346 | |||
| 1347 | # check if this is the lexelt! | ||
| 1348 | if ( $thisLine =~ /<\/lexelt>/ ) | ||
| 1349 | { | ||
| 1350 | $foundLexelt = 1; | ||
| 1351 | last; | ||
| 1352 | } | ||
| 1353 | |||
| 1354 | } | ||
| 1355 | else | ||
| 1356 | { | ||
| 1357 | if ( $thisLine !~ /<\/context/ ) | ||
| 1358 | { | ||
| 1359 | $contextLine .= $thisLine; | ||
| 1360 | next; | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | # right so we are at the END of the context now. | ||
| 1364 | $insideContext = 0; | ||
| 1365 | |||
| 1366 | # remove all new lines from $line | ||
| 1367 | # $contextLine =~ s/\s+/ /g; | ||
| 1368 | |||
| 1369 | # create tag according to if -uselexelt has been chosen | ||
| 1370 | $newTag = ""; | ||
| 1371 | if ( defined $opt_useLexelt ) | ||
| 1372 | { | ||
| 1373 | $opt_useLexelt *= 1; | ||
| 1374 | $newTag = "<lexelt=$lexeltWord/>"; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | # create tag according to if -useSenseid has been chosen | ||
| 1378 | if ( defined $opt_useSenseid ) | ||
| 1379 | { | ||
| 1380 | $opt_useSenseid *= 1; | ||
| 1381 | foreach $answer (@answers) | ||
| 1382 | { | ||
| 1383 | $newTag .= "<senseid=$answer/>"; | ||
| 1384 | } | ||
| 1385 | undef @answers; | ||
| 1386 | } | ||
| 1387 | |||
| 1388 | # note that at this point, new line characters have | ||
| 1389 | # not been removed. if --putSentenceTags is forced, put | ||
| 1390 | # in the sentence tags, and then put everything on one | ||
| 1391 | # line. | ||
| 1392 | |||
| 1393 | if (defined $opt_putSentenceTags) | ||
| 1394 | { | ||
| 1395 | $opt_putSentenceTags = 1; | ||
| 1396 | |||
| 1397 | $contextLine = $contextLine; | ||
| 1398 | $contextLine =~ s/^\s+//; | ||
| 1399 | $contextLine =~ s/\s+$//; | ||
| 1400 | $contextLine =~ s/\n+/\n/g; | ||
| 1401 | $contextLine =~ s/\n+/ <\/s> <s> /g; | ||
| 1402 | $contextLine = "<s> " . $contextLine . " </s>"; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | # now fix the line so that everything count cant see goes into tags | ||
| 1406 | $temp = $contextLine; | ||
| 1407 | |||
| 1408 | # ------------------------------ | ||
| 1409 | # ADP.3.1 adding nontoken option | ||
| 1410 | # ------------------------------ | ||
| 1411 | if(defined $non_token_regex) | ||
| 1412 | { | ||
| 1413 | $temp=~s/$non_token_regex/ /g; | ||
| 1414 | $temp=~s/\s+/ /g; | ||
| 1415 | } | ||
| 1416 | $contextLine = ""; | ||
| 1417 | while ( $temp =~ /($tokenizerRegex)/ ) | ||
| 1418 | { | ||
| 1419 | if (!(defined $opt_removeNotToken) && !( $` eq "" )) | ||
| 1420 | { | ||
| 1421 | my $notToken = $`; | ||
| 1422 | $notToken =~ s/^\s+//; | ||
| 1423 | $notToken =~ s/\s+$//; | ||
| 1424 | $contextLine .= " <" . $notToken . ">" if ($notToken ne ""); | ||
| 1425 | } | ||
| 1426 | |||
| 1427 | my $currentToken = $1; | ||
| 1428 | $temp = $'; | ||
| 1429 | |||
| 1430 | $contextLine .= " $currentToken"; | ||
| 1431 | } | ||
| 1432 | |||
| 1433 | $temp =~ s/^\s*//; | ||
| 1434 | $temp =~ s/\s*$//; | ||
| 1435 | if (!(defined $opt_removeNotToken) && !( $temp eq "" ) && !($temp =~ /^\s+$/)) | ||
| 1436 | { | ||
| 1437 | $contextLine .= " <" . $temp . ">"; | ||
| 1438 | } | ||
| 1439 | |||
| 1440 | # put a space in the end so that every token has at least one space on each side | ||
| 1441 | $contextLine .= " "; | ||
| 1442 | |||
| 1443 | # write out the context to the count file, if defined | ||
| 1444 | if ( !(defined $opt_nocount) ) | ||
| 1445 | { | ||
| 1446 | if ( !defined $opt_split ) | ||
| 1447 | { | ||
| 1448 | $temp = $contextLine; | ||
| 1449 | while ( $temp =~ /<\/head>/ ) | ||
| 1450 | { | ||
| 1451 | print COUNT $`; | ||
| 1452 | print COUNT $newTag; | ||
| 1453 | print COUNT $&; | ||
| 1454 | $temp = $'; | ||
| 1455 | } | ||
| 1456 | print COUNT "$temp\n"; | ||
| 1457 | } | ||
| 1458 | else | ||
| 1459 | { | ||
| 1460 | $temp = $contextLine; | ||
| 1461 | while ( $temp =~ /<\/head>/ ) | ||
| 1462 | { | ||
| 1463 | $instanceStringCount .= $`; | ||
| 1464 | $instanceStringCount .= $newTag; | ||
| 1465 | $instanceStringCount .= $&; | ||
| 1466 | $temp = $'; | ||
| 1467 | } | ||
| 1468 | $instanceStringCount .= $temp . "\n"; | ||
| 1469 | push @instanceArrayCount, $instanceStringCount; | ||
| 1470 | $instanceStringCount = ""; | ||
| 1471 | } | ||
| 1472 | } | ||
| 1473 | |||
| 1474 | # now write out to the xml file | ||
| 1475 | if ( !( defined $opt_noxml ) ) | ||
| 1476 | { | ||
| 1477 | if ( !defined $opt_split ) | ||
| 1478 | { | ||
| 1479 | $temp = $contextLine; | ||
| 1480 | while ( $temp =~ /<\/head>/ ) | ||
| 1481 | { | ||
| 1482 | print XML $`; | ||
| 1483 | print XML $newTag; | ||
| 1484 | print XML $&; | ||
| 1485 | $temp = $'; | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | print XML "$temp\n"; | ||
| 1489 | |||
| 1490 | # finally write out the /context tag | ||
| 1491 | print XML "</context>\n"; | ||
| 1492 | } | ||
| 1493 | else | ||
| 1494 | { | ||
| 1495 | $temp = $contextLine; | ||
| 1496 | while ( $temp =~ /<\/head>/ ) | ||
| 1497 | { | ||
| 1498 | $instanceStringXml .= $`; | ||
| 1499 | $instanceStringXml .= $newTag; | ||
| 1500 | $instanceStringXml .= $&; | ||
| 1501 | $temp = $'; | ||
| 1502 | } | ||
| 1503 | $instanceStringXml .= $temp . "\n</context>\n</instance>\n"; | ||
| 1504 | push @instanceArrayXml, $instanceStringXml; | ||
| 1505 | $instanceStringXml = ""; | ||
| 1506 | } | ||
| 1507 | } | ||
| 1508 | } | ||
| 1509 | } | ||
| 1510 | } | ||
| 1511 | |||
| 1512 | # if split defined, do the shuffling, and then writing out to the files! | ||
| 1513 | if ( defined $opt_split ) | ||
| 1514 | { | ||
| 1515 | if ((!defined $opt_noxml) && (!defined $opt_nocount)) | ||
| 1516 | { | ||
| 1517 | $splitPoint = sprintf("%d", $opt_split / 100 * $#instanceArrayXml); | ||
| 1518 | |||
| 1519 | for ( $i = 0; $i < $splitPoint; $i++ ) | ||
| 1520 | { | ||
| 1521 | my $index = int rand ($#instanceArrayXml+1); | ||
| 1522 | while ( $instanceArrayXml[$index] eq "" ) | ||
| 1523 | { | ||
| 1524 | $index = int rand ($#instanceArrayXml+1); | ||
| 1525 | } | ||
| 1526 | print XML_TRAIN $instanceArrayXml[$index]; | ||
| 1527 | print COUNT_TRAIN $instanceArrayCount[$index]; | ||
| 1528 | $instanceArrayXml[$index] = ""; | ||
| 1529 | } | ||
| 1530 | print XML_TRAIN "</lexelt>\n"; | ||
| 1531 | |||
| 1532 | for ( $i = $splitPoint; $i <= $#instanceArrayXml; $i++ ) | ||
| 1533 | { | ||
| 1534 | my $index = int rand ($#instanceArrayXml+1); | ||
| 1535 | while ( $instanceArrayXml[$index] eq "" ) | ||
| 1536 | { | ||
| 1537 | $index = int rand ($#instanceArrayXml+1); | ||
| 1538 | } | ||
| 1539 | print XML_TEST $instanceArrayXml[$index]; | ||
| 1540 | print COUNT_TEST $instanceArrayCount[$index]; | ||
| 1541 | $instanceArrayXml[$index] = ""; | ||
| 1542 | } | ||
| 1543 | print XML_TEST "</lexelt>\n"; | ||
| 1544 | } | ||
| 1545 | |||
| 1546 | elsif (defined $opt_noxml) | ||
| 1547 | { | ||
| 1548 | |||
| 1549 | $splitPoint = sprintf("%d", $opt_split / 100 * $#instanceArrayCount); | ||
| 1550 | |||
| 1551 | for ( $i = 0; $i < $splitPoint; $i++ ) | ||
| 1552 | { | ||
| 1553 | my $index = int rand ($#instanceArrayCount+1); | ||
| 1554 | while ( $instanceArrayCount[$index] eq "" ) | ||
| 1555 | { | ||
| 1556 | $index = int rand ($#instanceArrayCount+1); | ||
| 1557 | } | ||
| 1558 | print COUNT_TRAIN $instanceArrayCount[$index]; | ||
| 1559 | $instanceArrayCount[$index] = ""; | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | for ( $i = $splitPoint; $i <= $#instanceArrayCount; $i++ ) | ||
| 1563 | { | ||
| 1564 | my $index = int rand ($#instanceArrayCount+1); | ||
| 1565 | while ( $instanceArrayCount[$index] eq "" ) | ||
| 1566 | { | ||
| 1567 | $index = int rand ($#instanceArrayCount+1); | ||
| 1568 | } | ||
| 1569 | print COUNT_TEST $instanceArrayCount[$index]; | ||
| 1570 | $instanceArrayCount[$index] = ""; | ||
| 1571 | } | ||
| 1572 | } | ||
| 1573 | |||
| 1574 | else # defined $opt_nocount | ||
| 1575 | { | ||
| 1576 | $splitPoint = sprintf("%d", $opt_split / 100 * $#instanceArrayXml); | ||
| 1577 | |||
| 1578 | for ( $i = 0; $i < $splitPoint; $i++ ) | ||
| 1579 | { | ||
| 1580 | my $index = int rand ($#instanceArrayXml+1); | ||
| 1581 | while ( $instanceArrayXml[$index] eq "" ) | ||
| 1582 | { | ||
| 1583 | $index = int rand ($#instanceArrayXml+1); | ||
| 1584 | } | ||
| 1585 | print XML_TRAIN $instanceArrayXml[$index]; | ||
| 1586 | $instanceArrayXml[$index] = ""; | ||
| 1587 | } | ||
| 1588 | print XML_TRAIN "</lexelt>\n"; | ||
| 1589 | |||
| 1590 | for ( $i = $splitPoint; $i <= $#instanceArrayXml; $i++ ) | ||
| 1591 | { | ||
| 1592 | my $index = int rand ($#instanceArrayXml+1); | ||
| 1593 | while ( $instanceArrayXml[$index] eq "" ) | ||
| 1594 | { | ||
| 1595 | $index = int rand ($#instanceArrayXml+1); | ||
| 1596 | } | ||
| 1597 | print XML_TEST $instanceArrayXml[$index]; | ||
| 1598 | $instanceArrayXml[$index] = ""; | ||
| 1599 | } | ||
| 1600 | print XML_TEST "</lexelt>\n"; | ||
| 1601 | } | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | # that is the end of the <lexelt>. if opt_xml not defined, | ||
| 1605 | # close the corpus and close the file. | ||
| 1606 | |||
| 1607 | if ( !( defined $opt_xml) && !(defined $opt_noxml) ) | ||
| 1608 | { | ||
| 1609 | if ( defined $opt_split ) | ||
| 1610 | { | ||
| 1611 | print XML_TRAIN "</corpus>\n"; | ||
| 1612 | close XML_TRAIN; | ||
| 1613 | print XML_TEST "</corpus>\n"; | ||
| 1614 | close XML_TEST; | ||
| 1615 | } | ||
| 1616 | else | ||
| 1617 | { | ||
| 1618 | print XML "</corpus>\n"; | ||
| 1619 | close XML; | ||
| 1620 | } | ||
| 1621 | } | ||
| 1622 | if ( !(defined $opt_count) && !(defined $opt_nocount) ) | ||
| 1623 | { | ||
| 1624 | if ( defined $opt_split ) | ||
| 1625 | { | ||
| 1626 | close COUNT_TRAIN; | ||
| 1627 | close COUNT_TEST; | ||
| 1628 | } | ||
| 1629 | else { close COUNT; } | ||
| 1630 | } | ||
| 1631 | } | ||
| 1632 | |||
| 1633 | if ( defined $opt_xml ) | ||
| 1634 | { | ||
| 1635 | if ( defined $opt_split ) | ||
| 1636 | { | ||
| 1637 | print XML_TRAIN "</corpus>\n"; | ||
| 1638 | close XML_TRAIN; | ||
| 1639 | print XML_TEST "</corpus>\n"; | ||
| 1640 | close XML_TEST; | ||
| 1641 | } | ||
| 1642 | else | ||
| 1643 | { | ||
| 1644 | print XML "</corpus>\n"; | ||
| 1645 | close XML; | ||
| 1646 | } | ||
| 1647 | } | ||
| 1648 | if ( defined $opt_count ) | ||
| 1649 | { | ||
| 1650 | if ( defined $opt_split ) | ||
| 1651 | { | ||
| 1652 | close COUNT_TRAIN; | ||
| 1653 | close COUNT_TEST; | ||
| 1654 | } | ||
| 1655 | else { close COUNT; } | ||
| 1656 | } | ||
| 1657 | |||
| 1658 | # thats it! | ||
| 1659 | |||
| 1660 | # function to output a minimal usage note when the user has not provided any | ||
| 1661 | # commandline options | ||
| 1662 | sub minimalUsageNotes | ||
| 1663 | { | ||
| 1664 | print "Usage: preprocess.pl [OPTIONS] SOURCE\n"; | ||
| 1665 | askHelp(); | ||
| 1666 | } | ||
| 1667 | |||
| 1668 | # function to output help messages for this program | ||
| 1669 | sub showHelp | ||
| 1670 | { | ||
| 1671 | print "Usage: preprocess.pl [OPTIONS] SOURCE\n\n"; | ||
| 1672 | |||
| 1673 | print "Preprocesses Senseval lexical samples files. Outputs modified xml file(s) and\n"; | ||
| 1674 | print "also a NSP-ready file (see options below). By default separates different\n"; | ||
| 1675 | print "lexical elements into separate .xml and .count files.\n\n"; | ||
| 1676 | |||
| 1677 | print "OPTIONS:\n\n"; | ||
| 1678 | |||
| 1679 | print " --token FILE Reads tokens from FILE. The context of each instance\n"; | ||
| 1680 | print " is broken up into tokens and each pair of consecutive tokens\n"; | ||
| 1681 | print " are separated by white space. Non-white space characters\n"; | ||
| 1682 | print " which do not belong to any token are put between angular\n"; | ||
| 1683 | print " brackets. If this option is not used, the default token\n"; | ||
| 1684 | print " definitions of count.pl are assumed.\n\n"; | ||
| 1685 | |||
| 1686 | print " --removeNotToken Removes strings that do not match token file. If not\n"; | ||
| 1687 | print " specified, the non-matching strings are put within angular\n"; | ||
| 1688 | print " brackets, ie, <>\n\n"; | ||
| 1689 | |||
| 1690 | |||
| 1691 | print " --nontoken FILE Removes all characters sequences that match Perl\n"; | ||
| 1692 | print " regular expressions specified in FILE.\n\n"; | ||
| 1693 | |||
| 1694 | print " --noxml Does not output an xml file.\n\n"; | ||
| 1695 | |||
| 1696 | print " --xml FILE Outputs the changed xml file to FILE. If this option nor\n"; | ||
| 1697 | print " the option --noxml is provided, the file name is derived\n"; | ||
| 1698 | print " by concatenating the word in the <lexelt> tag with \".xml\".\n"; | ||
| 1699 | print " Note: if this option is used, separate lexelt items will\n"; | ||
| 1700 | print " not be split into separate files.\n\n"; | ||
| 1701 | |||
| 1702 | print " --nocount Does not output a NSP-ready file.\n\n"; | ||
| 1703 | |||
| 1704 | print " --count FILE Outputs just the part between <context> </context> (after\n"; | ||
| 1705 | print " modification) to FILE. FILE can then be used directly with\n"; | ||
| 1706 | print " NSP. If this option nor the option --nocount is provided,\n"; | ||
| 1707 | print " the file name is derived as in xml above, with a .count\n"; | ||
| 1708 | print " extension.\n"; | ||
| 1709 | print " Note: if this option is used, separate lexelt items will\n"; | ||
| 1710 | print " not be split into separate files.\n\n"; | ||
| 1711 | |||
| 1712 | print " --useLexelt Includes a tag <lexelt=WORD/> within the <head></head>\n"; | ||
| 1713 | print " tags, where WORD is the word in the immediately preceeding\n"; | ||
| 1714 | print " <lexelt> tag.\n\n"; | ||
| 1715 | |||
| 1716 | print " --useSenseid Includes a tag <senseid=XXXXX/> within the <head></head>\n"; | ||
| 1717 | print " tags, where XXXXX is the number in the immediately\n"; | ||
| 1718 | print " preceeding <answer> tag.\n\n"; | ||
| 1719 | |||
| 1720 | print " --split N Shuffles the instances in SOURCE and then splits them into\n"; | ||
| 1721 | print " two files, a training file and a test file, approximately\n"; | ||
| 1722 | print " in the ratio N:(100-N).\n\n"; | ||
| 1723 | |||
| 1724 | print " --seed N Sets the seed for the random number generator used during\n"; | ||
| 1725 | print " shuffling. If not used, no seeding is done (except for\n"; | ||
| 1726 | print " that provided automatically by perl)\n\n"; | ||
| 1727 | |||
| 1728 | print " --putSentenceTags Puts separate lines within the <context> </context> region\n"; | ||
| 1729 | print " within <s> </s> pairs of tags. If separate sentences are on\n"; | ||
| 1730 | print " seperate lines, these tags effectively denote the start and\n"; | ||
| 1731 | print " end of sentences.\n\n"; | ||
| 1732 | |||
| 1733 | print " --version Prints the version number.\n\n"; | ||
| 1734 | |||
| 1735 | print " --help Prints this help message.\n\n"; | ||
| 1736 | |||
| 1737 | print " --verbose Turns on verbose mode. Silent by default.\n\n"; | ||
| 1738 | } | ||
| 1739 | |||
| 1740 | # function to output the version number | ||
| 1741 | sub showVersion | ||
| 1742 | { | ||
| 1743 | # print STDERR "preprocess.pl - Version 0.3\n"; | ||
| 1744 | print '$Id: preprocess.pl,v 1.9 2015/10/03 14:05:57 tpederse Exp $'; | ||
| 1745 | print "\nFormat and clean a Senseval-2 data file\n"; | ||
| 1746 | # print STDERR "Copyright (C) 2001-2003, Ted Pedersen & Satanjeev Banerjee\n"; | ||
| 1747 | # print STDERR "Date of Last Update: May 10, 2003 by TDP\n"; | ||
| 1748 | } | ||
| 1749 | |||
| 1750 | # function to output "ask for help" message when the user's goofed up! | ||
| 1751 | sub askHelp | ||
| 1752 | { | ||
| 1753 | print STDERR "Type preprocess.pl --help for help.\n"; | ||
| 1754 | } |