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