« blib/lib/Lingua/Ogmios/Annotations/Section.pm
↑ Summary
blib/lib/Lingua/Ogmios/Config/NLPTools.pm »
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use XML::Simple; | ||
| 5 | |||
| 6 | use Getopt::Long; | ||
| 7 | use Pod::Usage; | ||
| 8 | |||
| 9 | # use Data::Dumper; | ||
| 10 | |||
| 11 | # print Dumper($config); | ||
| 12 | |||
| 13 | # my $xs_obj = XML::Simple->new(); | ||
| 14 | |||
| 15 | # my $xs_obj_out = XML::Simple->new(); | ||
| 16 | |||
| 17 | # my $ref_XS_files = $xs_obj->XMLin($ARGV[0]); | ||
| 18 | |||
| 19 | my $doc_id; | ||
| 20 | my $text; | ||
| 21 | my $i; | ||
| 22 | my $title; | ||
| 23 | my $section_regex = '^\s*[^a-z\d]+($|:)'; | ||
| 24 | |||
| 25 | my $help = 0; | ||
| 26 | my $man = 0; | ||
| 27 | |||
| 28 | my $filename; | ||
| 29 | my $lang; | ||
| 30 | |||
| 31 | if (scalar(@ARGV) ==0) { | ||
| 32 | $help = 1; | ||
| 33 | } | ||
| 34 | |||
| 35 | Getopt::Long::Configure ("bundling"); | ||
| 36 | |||
| 37 | # 'verbose|v:i' => \$verbose, | ||
| 38 | |||
| 39 | GetOptions('help|?' => \$help, | ||
| 40 | 'man' => \$man, | ||
| 41 | 'file|f=s' => \$filename, | ||
| 42 | 'docId|i=s' => \$doc_id, | ||
| 43 | 'lang|l=s' => \$lang, | ||
| 44 | ); | ||
| 45 | |||
| 46 | pod2usage(1) if $help; | ||
| 47 | pod2usage(-exitstatus => 0, -verbose => 2) if $man; | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | my @doc_initial_text; | ||
| 52 | |||
| 53 | open FILE, "<:utf8", $filename; | ||
| 54 | my @text = <FILE>; | ||
| 55 | $text[0] =~ s///; | ||
| 56 | |||
| 57 | my @initial_text = @text; | ||
| 58 | |||
| 59 | binmode(stdout,":utf8"); | ||
| 60 | # binmode(stdin,":latin9"); | ||
| 61 | |||
| 62 | # map { | ||
| 63 | # $_ =~ s/&/&/go; | ||
| 64 | # $_ =~ s/"/\"/og; | ||
| 65 | # $_ =~ s/'/\'/og; | ||
| 66 | # $_ =~ s/</</og; | ||
| 67 | # $_ =~ s/>/>/og; | ||
| 68 | # $_ =~ s/\x{0009}/ /og; | ||
| 69 | # $_ =~ s/[\x{0000}-\x{001F}]//og; | ||
| 70 | # chomp; | ||
| 71 | # } @initial_text; | ||
| 72 | |||
| 73 | |||
| 74 | map { $_ = &cleanOUTPUT($_); } @initial_text; | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | my @text_with_sections; | ||
| 79 | my $line; | ||
| 80 | |||
| 81 | foreach $line (@initial_text) { | ||
| 82 | push @text_with_sections, { "content" => $line}; | ||
| 83 | } | ||
| 84 | |||
| 85 | print '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; | ||
| 86 | print '<documentCollection xmlns="http://alvis.info/enriched/" version="1.1">'. "\n"; | ||
| 87 | |||
| 88 | &print_documentRecord($doc_id, $filename, \@text_with_sections, $lang); | ||
| 89 | |||
| 90 | print '</documentCollection>' . "\n"; | ||
| 91 | |||
| 92 | exit; | ||
| 93 | |||
| 94 | my $initial_text; | ||
| 95 | |||
| 96 | # my $initial_text = join('', @text); | ||
| 97 | |||
| 98 | # $initial_text =~ s/&/&/go; | ||
| 99 | # $initial_text =~ s/"/\"/og; | ||
| 100 | # $initial_text =~ s/'/\'/og; | ||
| 101 | # $initial_text =~ s/</</og; | ||
| 102 | # $initial_text =~ s/>/>/og; | ||
| 103 | |||
| 104 | # push @doc_initial_text, $initial_text; | ||
| 105 | |||
| 106 | close FILE; | ||
| 107 | |||
| 108 | # my @text_with_sections_and_items_step2; | ||
| 109 | |||
| 110 | # my @modified_sections; | ||
| 111 | |||
| 112 | print '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; | ||
| 113 | print '<documentCollection xmlns="http://alvis.info/enriched/" version="1.1">'. "\n"; | ||
| 114 | |||
| 115 | my %titles; | ||
| 116 | |||
| 117 | my %documents; | ||
| 118 | my $section; | ||
| 119 | |||
| 120 | my $max_doc_id; | ||
| 121 | |||
| 122 | foreach $initial_text (@doc_initial_text) { | ||
| 123 | warn "Processing document \n"; | ||
| 124 | |||
| 125 | my @text_with_sections = &identify_sections($initial_text); | ||
| 126 | |||
| 127 | # exit; | ||
| 128 | |||
| 129 | my @text_with_sections_and_items; | ||
| 130 | my $section_nb = 0; | ||
| 131 | my @text_section; | ||
| 132 | |||
| 133 | foreach $section (@text_with_sections) { | ||
| 134 | # warn "****" . $section->{"content"} . "\n"; | ||
| 135 | push @text_with_sections_and_items, &identify_listitems($section->{"content"}, \$section_nb); | ||
| 136 | } | ||
| 137 | |||
| 138 | # foreach $section (@text_with_sections_and_items) { | ||
| 139 | # warn "****" . $section->{"content"} . "\n"; | ||
| 140 | # } | ||
| 141 | $documents{$doc_id} = \@text_with_sections_and_items; | ||
| 142 | |||
| 143 | $max_doc_id = $doc_id; | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | foreach $doc_id (sort {$a <=> $b} keys %documents) { | ||
| 149 | warn "Processing document $doc_id / $max_doc_id (title identification second step)\n"; | ||
| 150 | |||
| 151 | my @text_with_sections_and_items = @{$documents{$doc_id}}; | ||
| 152 | |||
| 153 | foreach $section (@text_with_sections_and_items) { | ||
| 154 | if ($title = &identify_title($section->{"content"}, \%titles)) { | ||
| 155 | # print STDERR "title=" . $title . "\n"; | ||
| 156 | $section->{"title"} = $title; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | # A décommenter pour la deuxième passe | ||
| 161 | # my @text_with_sections_and_items_step2; | ||
| 162 | # foreach $section (@text_with_sections_and_items) { | ||
| 163 | # @modified_sections = &identify_title_from_known_titles($section, \%titles); | ||
| 164 | # if (scalar(@modified_sections)) { | ||
| 165 | # push @text_with_sections_and_items_step2, @modified_sections; | ||
| 166 | # } else { | ||
| 167 | # push @text_with_sections_and_items_step2, $section; | ||
| 168 | # } | ||
| 169 | # } | ||
| 170 | # &print_documentRecord($doc_id, \@text_with_sections_and_items_step2); | ||
| 171 | |||
| 172 | # A commenter pour la deuxième passe | ||
| 173 | # &print_documentRecord($doc_id, \@text_with_sections_and_items_step); | ||
| 174 | &print_documentRecord($doc_id, $filename, \@text_with_sections_and_items, $lang); | ||
| 175 | } | ||
| 176 | |||
| 177 | # my $xml = $xs->XMLout($hashref [, <options>]); | ||
| 178 | |||
| 179 | print '</documentCollection>' . "\n"; | ||
| 180 | |||
| 181 | sub new_section { | ||
| 182 | my ($ref_section_nb, $ref_text_with_sections, $text, $sectionType) = @_; | ||
| 183 | |||
| 184 | |||
| 185 | $sectionType = "narrative" unless $sectionType; | ||
| 186 | $$ref_section_nb++; | ||
| 187 | |||
| 188 | $ref_text_with_sections->[$$ref_section_nb]->{"content"} = $text; | ||
| 189 | $ref_text_with_sections->[$$ref_section_nb]->{"sectionType"} = $sectionType; | ||
| 190 | |||
| 191 | } | ||
| 192 | |||
| 193 | sub add_in_section { | ||
| 194 | my ($section_nb, $ref_text_with_sections, $text) = @_; | ||
| 195 | # "\n" . | ||
| 196 | |||
| 197 | if (ref($ref_text_with_sections->[$section_nb]->{"content"}) eq "ARRAY") { | ||
| 198 | push @{$ref_text_with_sections->[$section_nb]->{"content"}}, $text; | ||
| 199 | } else { | ||
| 200 | $ref_text_with_sections->[$section_nb]->{"content"} .= $text; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | sub new_item { | ||
| 205 | my ($ref_item_nb, $ref_text_with_items, $text) = @_; | ||
| 206 | |||
| 207 | $$ref_item_nb++; | ||
| 208 | $ref_text_with_items->[$$ref_item_nb] = $text; | ||
| 209 | |||
| 210 | } | ||
| 211 | |||
| 212 | sub add_in_item { | ||
| 213 | my ($item_nb, $ref_text_with_items, $text) = @_; | ||
| 214 | |||
| 215 | # "\n" . | ||
| 216 | $ref_text_with_items->[$item_nb] .= $text; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub print_list { | ||
| 220 | my ($list_ref, $title) = @_; | ||
| 221 | my $item; | ||
| 222 | |||
| 223 | # warn "$list_ref\n"; | ||
| 224 | my $item_str = ""; | ||
| 225 | foreach $item (@{$list_ref}) { | ||
| 226 | if ($item ne "") { | ||
| 227 | # warn "Here $item\n"; | ||
| 228 | $item_str .= XMLout($item, "RootName" => "item", | ||
| 229 | 'NoIndent' => 1, | ||
| 230 | ); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | if ($item_str ne "") { | ||
| 234 | if (defined $title) { | ||
| 235 | print "<section title=\"$title\"><list>"; | ||
| 236 | } else { | ||
| 237 | print "<section><list>"; | ||
| 238 | } | ||
| 239 | print $item_str; | ||
| 240 | print "</list></section>\n"; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | } | ||
| 245 | |||
| 246 | sub print_documentRecord { | ||
| 247 | |||
| 248 | my ($doc_id, $path, $documentRecord_ref, $language) = @_; | ||
| 249 | |||
| 250 | my $section; | ||
| 251 | my $item; | ||
| 252 | |||
| 253 | print ' <documentRecord id="' . $doc_id . '">' . "\n"; | ||
| 254 | print ' <acquisition>' . "\n"; | ||
| 255 | print ' <acquisitionData>' . "\n"; | ||
| 256 | print ' <modifiedDate>' . "2011" . '</modifiedDate>' . "\n"; | ||
| 257 | print ' <urls>' . "\n"; | ||
| 258 | print ' <url>file://' . $path . "#$doc_id" . '</url>' . "\n"; | ||
| 259 | print ' </urls>' . "\n"; | ||
| 260 | print ' </acquisitionData>' . "\n"; | ||
| 261 | print ' <canonicalDocument>' . "\n"; | ||
| 262 | print ' <section>'; | ||
| 263 | |||
| 264 | print_sections($documentRecord_ref); | ||
| 265 | |||
| 266 | print '</section>' . "\n"; | ||
| 267 | print ' </canonicalDocument>' . "\n"; | ||
| 268 | print ' <analysis>' . "\n"; | ||
| 269 | print ' <property name="language">' . $language . '</property>' . "\n"; | ||
| 270 | print ' </analysis>' . "\n"; | ||
| 271 | print ' </acquisition>' . "\n"; | ||
| 272 | print ' </documentRecord>' . "\n"; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub print_sections { | ||
| 276 | my ($sections) = @_; | ||
| 277 | |||
| 278 | my $section; | ||
| 279 | foreach $section (@$sections) { | ||
| 280 | if ((defined $section->{"sectionType"}) && ($section->{"sectionType"} eq "list")) { | ||
| 281 | &print_list($section->{"content"}, $section->{"title"}); | ||
| 282 | } else { | ||
| 283 | if (ref ($section->{"content"}) eq "ARRAY") { | ||
| 284 | if (defined $section->{"title"}) { | ||
| 285 | print "<section title=\"" . $section->{"title"} . "\">"; | ||
| 286 | } else { | ||
| 287 | print "<section>"; | ||
| 288 | } | ||
| 289 | &print_sections($section->{"content"}); | ||
| 290 | print "</section>\n"; | ||
| 291 | } else { | ||
| 292 | &print_section($section); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | sub print_section { | ||
| 299 | my ($section) = @_; | ||
| 300 | if ($section->{"content"} !~ /^[\s\n]*$/o) { | ||
| 301 | if (exists $section->{"title"}) { | ||
| 302 | print XMLout($section, 'RootName' => "section", | ||
| 303 | 'NoIndent' => 1, | ||
| 304 | ) ; | ||
| 305 | print "\n"; | ||
| 306 | } else { | ||
| 307 | print XMLout($section, 'RootName' => "section", | ||
| 308 | 'NoIndent' => 1, | ||
| 309 | ); | ||
| 310 | print "\n"; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | sub identify_sections { | ||
| 316 | my ($text) = @_; | ||
| 317 | |||
| 318 | my @text = split /\n/, $text; | ||
| 319 | |||
| 320 | # warn "Size: " . scalar(@text); | ||
| 321 | |||
| 322 | map {$_ .= "\n"} @text; | ||
| 323 | |||
| 324 | # for($i=0;$i < scalar(@text); $i++) { | ||
| 325 | # $text[$i] .= "\n"; | ||
| 326 | # } | ||
| 327 | |||
| 328 | my @text_with_sections; | ||
| 329 | my $section_nb = 0; | ||
| 330 | $i = 0; | ||
| 331 | |||
| 332 | # warn ">" . $text[$i]; | ||
| 333 | # do { | ||
| 334 | # if ($text[$i] !~ /^\s*$/o) { | ||
| 335 | &new_section(\$section_nb, \@text_with_sections, $text[$i]); | ||
| 336 | # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 337 | # } | ||
| 338 | $i++; | ||
| 339 | do { | ||
| 340 | # warn "->" . $text[$i]; | ||
| 341 | &add_in_section($section_nb, \@text_with_sections, $text[$i]); | ||
| 342 | # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 343 | $i++; | ||
| 344 | } while($text[$i] =~ /^\s*\n$/); | ||
| 345 | # $i--; | ||
| 346 | $section_nb++; | ||
| 347 | for(;$i <= $#text; $i++) { | ||
| 348 | # warn "i: $i\n"; | ||
| 349 | # warn "-->" . $text[$i]; | ||
| 350 | |||
| 351 | if (($text[$i] =~ /^\s*[^a-z\d]+:/o) || ($text[$i] =~ /^\s*[^a-z\d][^:]+:/o) || (index($text[$i], "******") > -1)){ | ||
| 352 | &new_section(\$section_nb, \@text_with_sections, $text[$i]); | ||
| 353 | # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 354 | next; | ||
| 355 | } | ||
| 356 | # if (($text[$i] =~ /^\s*[^a-z\d\(]+/o) && ($text[$i - 1] !~ /[a-z,]\s*$/o)){ | ||
| 357 | # &new_section(\$section_nb, \@text_with_sections, $text[$i]); | ||
| 358 | # next; | ||
| 359 | # } | ||
| 360 | &add_in_section($section_nb, \@text_with_sections, $text[$i]); | ||
| 361 | # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 362 | } | ||
| 363 | return(@text_with_sections); | ||
| 364 | } | ||
| 365 | |||
| 366 | |||
| 367 | # sub identify_listitems { | ||
| 368 | # my ($section, $section_nb_ref) = @_; | ||
| 369 | # my @text_with_sections_and_items; | ||
| 370 | |||
| 371 | # my $item_regex = '^\d+\.\s+'; | ||
| 372 | |||
| 373 | # # warn "->$section"; | ||
| 374 | |||
| 375 | # my @listSection = ( | ||
| 376 | # "DISCHARGE MEDICATIONS", | ||
| 377 | # "MEDICATIONS ON ADMISSION", | ||
| 378 | # "ADMISSION MEDICATIONS", | ||
| 379 | # "ADDENDUM TO MEDICATIONS ON DISCHARGE", | ||
| 380 | # "MEDICATION AT TIME OF DISCHARGE", | ||
| 381 | # # "", | ||
| 382 | # # "", | ||
| 383 | # # "", | ||
| 384 | # # "", | ||
| 385 | # ); | ||
| 386 | |||
| 387 | |||
| 388 | # } | ||
| 389 | |||
| 390 | # sub add_in_section { | ||
| 391 | # my ($section_nb, $ref_text_with_sections, $text) = @_; | ||
| 392 | # # "\n" . | ||
| 393 | |||
| 394 | # $ref_text_with_sections->[$section_nb]->{"content"} .= $text; | ||
| 395 | # } | ||
| 396 | |||
| 397 | # sub new_item { | ||
| 398 | # my ($ref_item_nb, $ref_text_with_items, $text) = @_; | ||
| 399 | |||
| 400 | # $$ref_item_nb++; | ||
| 401 | # $ref_text_with_items->[$$ref_item_nb] = $text; | ||
| 402 | |||
| 403 | # } | ||
| 404 | |||
| 405 | # sub add_in_item { | ||
| 406 | # my ($item_nb, $ref_text_with_items, $text) = @_; | ||
| 407 | |||
| 408 | # # "\n" . | ||
| 409 | # $ref_text_with_items->[$item_nb] .= $text; | ||
| 410 | # } | ||
| 411 | |||
| 412 | # sub print_list { | ||
| 413 | # my ($list_ref) = @_; | ||
| 414 | # my $item; | ||
| 415 | |||
| 416 | # print "<section><list>"; | ||
| 417 | # foreach $item (@{$list_ref}) { | ||
| 418 | # print XMLout($item, "RootName" => "item", | ||
| 419 | # 'NoIndent' => 1, | ||
| 420 | # ); | ||
| 421 | # } | ||
| 422 | # print "</list></section>"; | ||
| 423 | |||
| 424 | |||
| 425 | # } | ||
| 426 | |||
| 427 | |||
| 428 | # sub identify_sections { | ||
| 429 | # my ($text) = @_; | ||
| 430 | |||
| 431 | # my @text = split /\n/, $text; | ||
| 432 | |||
| 433 | # # warn "Size: " . scalar(@text); | ||
| 434 | |||
| 435 | # map {$_ .= "\n"} @text; | ||
| 436 | |||
| 437 | # # for($i=0;$i < scalar(@text); $i++) { | ||
| 438 | # # $text[$i] .= "\n"; | ||
| 439 | # # } | ||
| 440 | |||
| 441 | # my @text_with_sections; | ||
| 442 | # my $section_nb = 0; | ||
| 443 | # $i = 0; | ||
| 444 | |||
| 445 | # # warn "-->" . $text[$i]; | ||
| 446 | # # do { | ||
| 447 | # # if ($text[$i] !~ /^\s*$/o) { | ||
| 448 | # &new_section(\$section_nb, \@text_with_sections, $text[$i]); | ||
| 449 | # # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 450 | # # } | ||
| 451 | # $i++; | ||
| 452 | # do { | ||
| 453 | # &add_in_section($section_nb, \@text_with_sections, $text[$i]); | ||
| 454 | # # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 455 | # $i++; | ||
| 456 | # } while($text[$i] =~ /^\s*\n$/); | ||
| 457 | # # $i--; | ||
| 458 | # $section_nb++; | ||
| 459 | # for(;$i <= $#text; $i++) { | ||
| 460 | # # warn "i: $i\n"; | ||
| 461 | # # warn "-->" . $text[$i]; | ||
| 462 | |||
| 463 | # if (($text[$i] =~ /^\s*[^a-z\d]+:/o) || ($text[$i] =~ /^\s*[^a-z\d][^:]+:/o) || (index($text[$i], "******") > -1)){ | ||
| 464 | # &new_section(\$section_nb, \@text_with_sections, $text[$i]); | ||
| 465 | # # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 466 | # next; | ||
| 467 | # } | ||
| 468 | # # if (($text[$i] =~ /^\s*[^a-z\d\(]+/o) && ($text[$i - 1] !~ /[a-z,]\s*$/o)){ | ||
| 469 | # # &new_section(\$section_nb, \@text_with_sections, $text[$i]); | ||
| 470 | # # next; | ||
| 471 | # # } | ||
| 472 | # &add_in_section($section_nb, \@text_with_sections, $text[$i]); | ||
| 473 | # # warn "==>($section_nb)" . $text_with_sections[$section_nb]->{"content"}; | ||
| 474 | # } | ||
| 475 | # return(@text_with_sections); | ||
| 476 | # } | ||
| 477 | |||
| 478 | |||
| 479 | sub identify_listitems { | ||
| 480 | my ($section, $section_nb_ref) = @_; | ||
| 481 | my @text_with_sections_and_items; | ||
| 482 | |||
| 483 | my $item_regex = '^\b\d+\.\s+'; | ||
| 484 | |||
| 485 | # warn "-> () $section"; | ||
| 486 | |||
| 487 | my @listSection = ( | ||
| 488 | "DISCHARGE MEDICATIONS", | ||
| 489 | "DISCHARGE MEDS", | ||
| 490 | "MEDICATIONS ON ADMISSION", | ||
| 491 | "MEDICATIONS ON DISCHARGE", | ||
| 492 | "ADMISSION MEDICATIONS", | ||
| 493 | "ADMISSION MEDS", | ||
| 494 | "MEDICATIONS UPON TRANSFER", | ||
| 495 | "PREOPERATIVE MEDICATIONS", | ||
| 496 | "PRN MEDICATIONS", | ||
| 497 | "ADDENDUM TO MEDICATIONS ON DISCHARGE", | ||
| 498 | "MEDICATION AT TIME OF DISCHARGE", | ||
| 499 | "CURRENT MEDICATIONS", | ||
| 500 | "DRUG HISTORY", | ||
| 501 | "HOME MEDICATIONS", | ||
| 502 | "HOME MEDS", | ||
| 503 | "MEDICATIONS UPON DISCHARGE", | ||
| 504 | "MEDICATIONS UPON ADMISSION", | ||
| 505 | "PREOP MEDICATIONS", | ||
| 506 | "MEDS", | ||
| 507 | "MEDICATIONS", | ||
| 508 | ); | ||
| 509 | |||
| 510 | # ??? "MEDICATIONS ON \nADMISSION", | ||
| 511 | |||
| 512 | |||
| 513 | # DRUG HISTORY (to confirm) | ||
| 514 | |||
| 515 | my $item_nb = -1; | ||
| 516 | my @items; | ||
| 517 | |||
| 518 | my $j; | ||
| 519 | my @text_section; | ||
| 520 | |||
| 521 | my $section_tail = undef; | ||
| 522 | if ($section =~ /\n(\n+)$/) { | ||
| 523 | $section_tail = $1; | ||
| 524 | } | ||
| 525 | my $k = 0; | ||
| 526 | |||
| 527 | while(index($section, $listSection[$k]) < 0) { | ||
| 528 | $k++; | ||
| 529 | } | ||
| 530 | |||
| 531 | my $sectionType = "narrative"; | ||
| 532 | if ($k < scalar(@listSection)) { | ||
| 533 | my @sections; | ||
| 534 | |||
| 535 | if ($section =~ /$listSection[$k]\s*:?\s*/) { | ||
| 536 | $section = $'; # ' } | ||
| 537 | &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $`); | ||
| 538 | |||
| 539 | my %tmp = ("content" => $&, | ||
| 540 | # "title" => $listSection[$k], | ||
| 541 | ); | ||
| 542 | |||
| 543 | push @sections, \%tmp; | ||
| 544 | &new_section($section_nb_ref, \@text_with_sections_and_items, \@sections); | ||
| 545 | $text_with_sections_and_items[$$section_nb_ref]->{"title"} = $listSection[$k]; | ||
| 546 | |||
| 547 | $sectionType = "list"; | ||
| 548 | |||
| 549 | &new_item(\$item_nb, \@items, ""); | ||
| 550 | # &add_in_section($$section_nb_ref, \@text_with_sections_and_items, \@items); | ||
| 551 | my %tmp2 = ("content" => \@items, | ||
| 552 | "sectionType" => "list", | ||
| 553 | ); | ||
| 554 | |||
| 555 | push @sections, \%tmp2; | ||
| 556 | } | ||
| 557 | # $text_with_sections_and_items[$$section_nb_ref]->{"title"} = $listSection[$k]; | ||
| 558 | # &add_in_section($section_nb_ref, \@text_with_sections_and_items, \@items); | ||
| 559 | } else { | ||
| 560 | |||
| 561 | if ($section =~ /^(\s*([A-Z][^:\d\+\.]+):\s*)(\d+\.\s+)/o) { | ||
| 562 | my @sections; | ||
| 563 | my $start_listsection = $1; | ||
| 564 | my $start_listsection_title = $2; | ||
| 565 | $section = "$3$'"; # ' } | ||
| 566 | my %tmp = ("content" => $start_listsection, | ||
| 567 | # "title" => $listSection[$k], | ||
| 568 | ); | ||
| 569 | |||
| 570 | push @sections, \%tmp; | ||
| 571 | &new_section($section_nb_ref, \@text_with_sections_and_items, \@sections); | ||
| 572 | $text_with_sections_and_items[$$section_nb_ref]->{"title"} = $start_listsection_title; | ||
| 573 | |||
| 574 | $sectionType = "list"; | ||
| 575 | # warn "--> ". $start_listsection . "\n"; | ||
| 576 | |||
| 577 | &new_item(\$item_nb, \@items, ""); | ||
| 578 | # &add_in_section($$section_nb_ref, \@text_with_sections_and_items, \@items); | ||
| 579 | my %tmp2 = ("content" => \@items, | ||
| 580 | "sectionType" => "list", | ||
| 581 | ); | ||
| 582 | |||
| 583 | push @sections, \%tmp2; | ||
| 584 | } | ||
| 585 | # warn "$section: it is not a list\n"; | ||
| 586 | } | ||
| 587 | # warn "type of section: $sectionType\n"; | ||
| 588 | |||
| 589 | @text_section = split /\n/, $section; | ||
| 590 | |||
| 591 | map{$_ .= "\n"} @text_section; | ||
| 592 | |||
| 593 | for($j=0;$j<= $#text_section;$j++) { | ||
| 594 | # warn "=====> " . $text_section[$j] . "\n"; | ||
| 595 | if ($text_section[$j] =~ /$item_regex/) { | ||
| 596 | # warn "match item $item_nb $& $sectionType\n"; | ||
| 597 | if (($item_nb == -1) && ($sectionType ne "list")) { | ||
| 598 | my $title; | ||
| 599 | my @sections; | ||
| 600 | # warn "==>" . $text_section[$j] . "\n"; | ||
| 601 | if ($text_section[$j] =~ /^(\s*([A-Z][^:\d\+\.]+):\s*)/o) { | ||
| 602 | # warn "TITLE = $1 ($&)\n"; | ||
| 603 | $title = $2; | ||
| 604 | $sectionType = "list"; | ||
| 605 | my %tmp = ("content" => $1, | ||
| 606 | # "title" => $listSection[$k], | ||
| 607 | ); | ||
| 608 | &new_item(\$item_nb, \@items, $'); # ' } | ||
| 609 | |||
| 610 | |||
| 611 | push @sections, \%tmp; | ||
| 612 | &new_section($section_nb_ref, \@text_with_sections_and_items, \@sections, $sectionType); | ||
| 613 | $text_with_sections_and_items[$$section_nb_ref]->{"title"} = $title; | ||
| 614 | |||
| 615 | my %tmp2 = ("content" => \@items, | ||
| 616 | "sectionType" => "list", | ||
| 617 | ); | ||
| 618 | |||
| 619 | push @sections, \%tmp2; | ||
| 620 | |||
| 621 | } else { | ||
| 622 | # warn "Pass here\n"; | ||
| 623 | &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $text_section[$j]); | ||
| 624 | } | ||
| 625 | |||
| 626 | # &new_section($section_nb_ref, \@text_with_sections_and_items, \@items, "list"); | ||
| 627 | # $text_with_sections_and_items->{"title"} = $title; | ||
| 628 | } else { | ||
| 629 | &new_item(\$item_nb, \@items, $text_section[$j]); | ||
| 630 | } | ||
| 631 | next; | ||
| 632 | } | ||
| 633 | if ($item_nb == -1) { | ||
| 634 | # warn "add in item -1\n"; | ||
| 635 | &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $text_section[$j]); | ||
| 636 | # warn "==>($$section_nb_ref)" . $text_with_sections_and_items[$$section_nb_ref]->{"content"}; | ||
| 637 | } else { | ||
| 638 | # warn "add in item\n"; | ||
| 639 | &add_in_item($item_nb, \@items, $text_section[$j]); | ||
| 640 | } | ||
| 641 | } | ||
| 642 | if (defined $section_tail) { | ||
| 643 | &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $section_tail); | ||
| 644 | # warn "==>($$section_nb_ref)" . $text_with_sections_and_items[$$section_nb_ref]->{"content"}; | ||
| 645 | } | ||
| 646 | $$section_nb_ref++; | ||
| 647 | return(@text_with_sections_and_items); | ||
| 648 | } | ||
| 649 | |||
| 650 | sub identify_title { | ||
| 651 | my ($text, $titles_ref) = @_; | ||
| 652 | # print STDERR "==>$text\n"; | ||
| 653 | my $title = ""; | ||
| 654 | |||
| 655 | if (($text ne "") && (ref($text) ne "ARRAY")) { | ||
| 656 | $text =~ s/^\n+//o; | ||
| 657 | |||
| 658 | if ($text =~ /^\**([A-Z][^:\d\+\.]+):/o) { | ||
| 659 | $title = $1; | ||
| 660 | if (length($title) != 1) { | ||
| 661 | $titles_ref->{$title}++; | ||
| 662 | } | ||
| 663 | } else { | ||
| 664 | if ($text =~ /^([A-Z\- ]+):/o) { | ||
| 665 | $title = $1; | ||
| 666 | if (length($title) != 1) { | ||
| 667 | $titles_ref->{$title}++; | ||
| 668 | } | ||
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | |||
| 673 | |||
| 674 | # if (|([A-Z+\- ]+)[:\.] | ||
| 675 | # } | ||
| 676 | # if ($text =~ /(^([A-Z\-]{2,}(\s[A-Z\-]+)+)\b)/) { | ||
| 677 | # $title = $1; | ||
| 678 | # print STDERR ">>>title2 = $title\n"; | ||
| 679 | # } | ||
| 680 | # } | ||
| 681 | } | ||
| 682 | return($title); | ||
| 683 | } | ||
| 684 | |||
| 685 | sub identify_title_from_known_titles { | ||
| 686 | my ($section, $titles_ref) = @_; | ||
| 687 | my $known_title; | ||
| 688 | my $text; | ||
| 689 | my @sections_text; | ||
| 690 | my @sections; | ||
| 691 | my $exists_title = 0; | ||
| 692 | my $i = 0; | ||
| 693 | my $title; | ||
| 694 | my $new_text; | ||
| 695 | my $end_title_pos; | ||
| 696 | |||
| 697 | if (ref($section->{"content"}) ne "ARRAY") { | ||
| 698 | # if (!exists($section->{"title"})) { | ||
| 699 | $text = $section->{"content"}; | ||
| 700 | foreach $known_title (keys %{$titles_ref}) { | ||
| 701 | if (index($text, $known_title, 1) != -1) { | ||
| 702 | if ($text =~ s/(\b)(\Q$known_title\E\b)/\1<SECTION>\2<TITLE>\2/) { | ||
| 703 | $exists_title++; | ||
| 704 | # print STDERR "found $known_title in $text\n\n"; | ||
| 705 | } | ||
| 706 | } | ||
| 707 | } | ||
| 708 | if ($exists_title) { | ||
| 709 | @sections_text = split /<SECTION>/, $text; | ||
| 710 | # Processing the first sectino separately | ||
| 711 | $sections[0] = {"content" => $sections_text[0],}; | ||
| 712 | if (exists $section->{"title"}) { | ||
| 713 | $sections[0]->{"title"} = $section->{"title"}; | ||
| 714 | } | ||
| 715 | for($i=1; $i<scalar(@sections_text);$i++) { | ||
| 716 | $end_title_pos = index($sections_text[$i], "<TITLE>",0); | ||
| 717 | $title = substr($sections_text[$i], 0, $end_title_pos); | ||
| 718 | $new_text = substr($sections_text[$i], $end_title_pos+7); | ||
| 719 | |||
| 720 | # print STDERR "title=$title\n"; | ||
| 721 | # print STDERR "content=$new_text\n"; | ||
| 722 | $sections[$i] = {"content" => $new_text, | ||
| 723 | "title" => $title, | ||
| 724 | }; | ||
| 725 | |||
| 726 | } | ||
| 727 | |||
| 728 | } | ||
| 729 | } | ||
| 730 | return(@sections); | ||
| 731 | } | ||
| 732 | |||
| 733 | sub cleanOUTPUT { | ||
| 734 | my ($str) = @_; | ||
| 735 | |||
| 736 | $str =~ s/\x{000C}//go; # CTRL-L - \f | ||
| 737 | $str =~ s/\x{0018}//go; # CTRL-X | ||
| 738 | $str =~ s/\x{0007}//go; # CTRL-G - \a | ||
| 739 | |||
| 740 | $str =~ s/&/&/go; | ||
| 741 | $str =~ s/"/\"/og; | ||
| 742 | $str =~ s/'/\'/og; | ||
| 743 | $str =~ s/</</og; | ||
| 744 | $str =~ s/>/>/og; | ||
| 745 | $str =~ s/\x{0009}/ /og; | ||
| 746 | $str =~ s/[\x{0000}-\x{001F}]//og; | ||
| 747 | chomp $str; | ||
| 748 | # | ||
| 749 | $str =~ s/[\x{2000}-\x{200B}]/ /go; | ||
| 750 | $str =~ s/[\x{2018}-\x{201B}]/\'/go; | ||
| 751 | $str =~ s/[\x{201C}-\x{201F}]/\"/go; | ||
| 752 | $str =~ s/[\x{2010}-\x{2015}]/\-/go; | ||
| 753 | $str =~ s/[\x{2010}-\x{2015}]/\-/go; | ||
| 754 | $str =~ s/[\x{2010}-\x{2015}]/\-/go; | ||
| 755 | # | ||
| 756 | # PRIVATE CODES -> removed | ||
| 757 | $str =~ s/\x{F09F} /\n\n- /go; | ||
| 758 | $str =~ s/[\x{E000}-\x{F8FF}]//go; | ||
| 759 | # | ||
| 760 | $str =~ s/\x{FFFD}//go; | ||
| 761 | # | ||
| 762 | $str =~ s/\x{2024}/./go; | ||
| 763 | $str =~ s/\x{2025}/../go; | ||
| 764 | $str =~ s/\x{2026}/.../go; | ||
| 765 | # | ||
| 766 | # | ||
| 767 | # SPECIAL TREATMENT TO ADD | ||
| 768 | $str =~ s/\x{2022} ?\n+/\- /go; | ||
| 769 | # | ||
| 770 | $str =~ s/\x{fb00}/ff/go; | ||
| 771 | $str =~ s/\x{fb01}/fi/go; | ||
| 772 | $str =~ s/\x{fb02}/fl/go; | ||
| 773 | $str =~ s/\x{fb03}/ffi/go; | ||
| 774 | $str =~ s/\x{fb04}/ffl/go; | ||
| 775 | $str =~ s/\x{fb05}/ft/go; | ||
| 776 | $str =~ s/\x{fb06}/st/go; | ||
| 777 | # | ||
| 778 | $str =~ s/\n- \n+- /\n- /go; | ||
| 779 | |||
| 780 | # { | ||
| 781 | # } | ||
| 782 | |||
| 783 | return($str); | ||
| 784 | } | ||
| 785 | |||
| 786 | |||
| 787 | |||
| 788 | ######################################################################## | ||
| 789 | |||
| 790 | =head1 NAME | ||
| 791 | |||
| 792 | Txt2Ogmios.pl - Script for converting text file in XML Ogmios file | ||
| 793 | |||
| 794 | |||
| 795 | =head1 SYNOPSIS | ||
| 796 | |||
| 797 | Txt2Ogmios.pl [option] --file <filename> --lang <language> --docId <documentId> | ||
| 798 | |||
| 799 | where option can be --help --man | ||
| 800 | |||
| 801 | =head1 OPTIONS AND ARGUMENTS | ||
| 802 | |||
| 803 | =over 4 | ||
| 804 | |||
| 805 | =item --file <filename> | ||
| 806 | |||
| 807 | The switch sets the input text file (to convert). | ||
| 808 | |||
| 809 | =item --lang <language> | ||
| 810 | |||
| 811 | The switch sets the language of the input text file (to convert). The | ||
| 812 | value are set with a ISO 639-1 code (fr for French, en for English). | ||
| 813 | |||
| 814 | =item --docId <documentId>> | ||
| 815 | |||
| 816 | This sets the document identifier in the xml file. | ||
| 817 | |||
| 818 | =item --help | ||
| 819 | |||
| 820 | print help message for using grepTerms.pl | ||
| 821 | |||
| 822 | =item --man | ||
| 823 | |||
| 824 | print man page of grepTerms.pl | ||
| 825 | |||
| 826 | =back | ||
| 827 | |||
| 828 | =head1 DESCRIPTION | ||
| 829 | |||
| 830 | This script converts a text file as input (switch C<file>) into a XML | ||
| 831 | Omgios file. The language property field is set with the switch | ||
| 832 | C<lang>, and the document identifier is set with the switch C<dociI>. | ||
| 833 | |||
| 834 | =head1 SEE ALSO | ||
| 835 | |||
| 836 | =head1 AUTHOR | ||
| 837 | |||
| 838 | Thierry Hamon, E<lt>thierry.hamon@limsi.frE<gt> | ||
| 839 | |||
| 840 | =head1 COPYRIGHT AND LICENSE | ||
| 841 | |||
| 842 | Copyright (C) 2012 Thierry Hamon | ||
| 843 | |||
| 844 | This is free software; you can redistribute it and/or modify it under | ||
| 845 | the same terms as Perl itself, either Perl version 5.8.4 or, at your | ||
| 846 | option, any later version of Perl 5 you may have available. | ||
| 847 | |||
| 848 | =cut | ||
| 849 |