blib/script/Txt2Ogmios.pl untested

filter: Statementstmt 0.0% 0 / 327 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 455 CC 31 SCAR 36.3
CC
31
Cov
0%
identify_listitems
110.0
identify_title
42.0
identify_title_from_known_titles
42.0
CRAP 37.9
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use XML::Simple;
5  
6 use utf8;
7 use open qw(:utf8 :std);
8  
9 use Getopt::Long;
10 use Pod::Usage;
11  
12 my $doc_id;
13 my $text;
14 my $i;
15 my $title;
16 my $section_regex = '^\s*[^a-z\d]+($|:)';
17  
18 my $help = 0;
19 my $man = 0;
20  
21 my $filename;
22 my $lang;
23  
24 if (scalar(@ARGV) ==0) {
25 $help = 1;
26 }
27  
28 Getopt::Long::Configure ("bundling");
29  
30 GetOptions('help|?' => \$help,
31 'man' => \$man,
32 'file|f=s' => \$filename,
33 'docId|i=s' => \$doc_id,
34 'lang|l=s' => \$lang,
35 );
36  
37 pod2usage(1) if $help;
38 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
39  
40 my @doc_initial_text;
41  
42 my $fh = *STDIN;
43 if (defined $filename) {
44 open $fh, "<:utf8", $filename;
45 }
46 my @text = <$fh>;
47  
48 if (defined $filename) {
49 close FILE;
50 }
51  
52 $text[0] =~ s///;
53  
54 my @initial_text = @text;
55  
56 binmode(stdout,":utf8");
57  
58 map { $_ = &cleanOUTPUT($_); } @initial_text;
59  
60 my @text_with_sections;
61 my $line;
62  
63 foreach $line (@initial_text) {
64 push @text_with_sections, { "content" => $line};
65 }
66  
67 print '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
68 print '<documentCollection xmlns="http://alvis.info/enriched/" version="1.1">'. "\n";
69  
70 &print_documentRecord($doc_id, $filename, \@text_with_sections, $lang);
71  
72 print '</documentCollection>' . "\n";
73  
74 exit;
75  
76 my $initial_text;
77  
78 print '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
79 print '<documentCollection xmlns="http://alvis.info/enriched/" version="1.1">'. "\n";
80  
81 my %titles;
82  
83 my %documents;
84 my $section;
85  
86 my $max_doc_id;
87  
88 foreach $initial_text (@doc_initial_text) {
89 warn "Processing document \n";
90  
91 my @text_with_sections = &identify_sections($initial_text);
92
93 my @text_with_sections_and_items;
94 my $section_nb = 0;
95 my @text_section;
96  
97 foreach $section (@text_with_sections) {
98 push @text_with_sections_and_items, &identify_listitems($section->{"content"}, \$section_nb);
99 }
100 $documents{$doc_id} = \@text_with_sections_and_items;
101 $max_doc_id = $doc_id;
102 }
103  
104 foreach $doc_id (sort {$a <=> $b} keys %documents) {
105 warn "Processing document $doc_id / $max_doc_id (title identification second step)\n";
106
107 my @text_with_sections_and_items = @{$documents{$doc_id}};
108  
109 foreach $section (@text_with_sections_and_items) {
110 if ($title = &identify_title($section->{"content"}, \%titles)) {
111 $section->{"title"} = $title;
112 }
113 }
114  
115 &print_documentRecord($doc_id, $filename, \@text_with_sections_and_items, $lang);
116 }
117  
118 print '</documentCollection>' . "\n";
119  
120 sub new_section {
121 my ($ref_section_nb, $ref_text_with_sections, $text, $sectionType) = @_;
122
123  
124 $sectionType = "narrative" unless $sectionType;
125 $$ref_section_nb++;
126
127 $ref_text_with_sections->[$$ref_section_nb]->{"content"} = $text;
128 $ref_text_with_sections->[$$ref_section_nb]->{"sectionType"} = $sectionType;
129  
130 }
131  
132 sub add_in_section {
133 my ($section_nb, $ref_text_with_sections, $text) = @_;
134  
135 if (ref($ref_text_with_sections->[$section_nb]->{"content"}) eq "ARRAY") {
136 push @{$ref_text_with_sections->[$section_nb]->{"content"}}, $text;
137 } else {
138 $ref_text_with_sections->[$section_nb]->{"content"} .= $text;
139 }
140 }
141  
142 sub new_item {
143 my ($ref_item_nb, $ref_text_with_items, $text) = @_;
144
145 $$ref_item_nb++;
146 $ref_text_with_items->[$$ref_item_nb] = $text;
147  
148 }
149  
150 sub add_in_item {
151 my ($item_nb, $ref_text_with_items, $text) = @_;
152
153 $ref_text_with_items->[$item_nb] .= $text;
154 }
155  
156 sub print_list {
157 my ($list_ref, $title) = @_;
158 my $item;
159  
160 my $item_str = "";
161 foreach $item (@{$list_ref}) {
162 if ($item ne "") {
163 $item_str .= XMLout($item, "RootName" => "item",
164 'NoIndent' => 1,
165 );
166 }
167 }
168 if ($item_str ne "") {
169 if (defined $title) {
170 print "<section title=\"$title\"><list>";
171 } else {
172 print "<section><list>";
173 }
174 print $item_str;
175 print "</list></section>\n";
176 }
177  
178
179 }
180  
181 sub print_documentRecord {
182  
183 my ($doc_id, $path, $documentRecord_ref, $language) = @_;
184  
185 my $section;
186 my $item;
187  
188 print ' <documentRecord id="' . $doc_id . '">' . "\n";
189 print ' <acquisition>' . "\n";
190 print ' <acquisitionData>' . "\n";
191 print ' <modifiedDate>' . "2011" . '</modifiedDate>' . "\n";
192 print ' <urls>' . "\n";
193 print ' <url>file://' . $path . "#$doc_id" . '</url>' . "\n";
194 print ' </urls>' . "\n";
195 print ' </acquisitionData>' . "\n";
196 print ' <canonicalDocument>' . "\n";
197 print ' <section>';
198  
199 print_sections($documentRecord_ref);
200  
201 print '</section>' . "\n";
202 print ' </canonicalDocument>' . "\n";
203 print ' <analysis>' . "\n";
204 print ' <property name="language">' . $language . '</property>' . "\n";
205 print ' </analysis>' . "\n";
206 print ' </acquisition>' . "\n";
207 print ' </documentRecord>' . "\n";
208 }
209  
210 sub print_sections {
211 my ($sections) = @_;
212  
213 my $section;
214 foreach $section (@$sections) {
215 if ((defined $section->{"sectionType"}) && ($section->{"sectionType"} eq "list")) {
216 &print_list($section->{"content"}, $section->{"title"});
217 } else {
218 if (ref ($section->{"content"}) eq "ARRAY") {
219 if (defined $section->{"title"}) {
220 print "<section title=\"" . $section->{"title"} . "\">";
221 } else {
222 print "<section>";
223 }
224 &print_sections($section->{"content"});
225 print "</section>\n";
226 } else {
227 &print_section($section);
228 }
229 }
230 }
231 }
232  
233 sub print_section {
234 my ($section) = @_;
235 if ($section->{"content"} !~ /^[\s\n]*$/o) {
236 if (exists $section->{"title"}) {
237 print XMLout($section, 'RootName' => "section",
238 'NoIndent' => 1,
239 ) ;
240 print "\n";
241 } else {
242 print XMLout($section, 'RootName' => "section",
243 'NoIndent' => 1,
244 );
245 print "\n";
246 }
247 }
248 }
249  
250 sub identify_sections {
251 my ($text) = @_;
252  
253 my @text = split /\n/, $text;
254  
255 map {$_ .= "\n"} @text;
256 my @text_with_sections;
257 my $section_nb = 0;
258 $i = 0;
259  
260 &new_section(\$section_nb, \@text_with_sections, $text[$i]);
261 $i++;
262 do {
263 &add_in_section($section_nb, \@text_with_sections, $text[$i]);
264 $i++;
265 } while($text[$i] =~ /^\s*\n$/);
266 $section_nb++;
267 for(;$i <= $#text; $i++) {
268 if (($text[$i] =~ /^\s*[^a-z\d]+:/o) || ($text[$i] =~ /^\s*[^a-z\d][^:]+:/o) || (index($text[$i], "******") > -1)){
269 &new_section(\$section_nb, \@text_with_sections, $text[$i]);
270 next;
271 }
272 &add_in_section($section_nb, \@text_with_sections, $text[$i]);
273 }
274 return(@text_with_sections);
275 }
276  
277 sub identify_listitems {
278 my ($section, $section_nb_ref) = @_;
279 my @text_with_sections_and_items;
280  
281 my $item_regex = '^\b\d+\.\s+';
282  
283 my @listSection = (
284 "DISCHARGE MEDICATIONS",
285 "DISCHARGE MEDS",
286 "MEDICATIONS ON ADMISSION",
287 "MEDICATIONS ON DISCHARGE",
288 "ADMISSION MEDICATIONS",
289 "ADMISSION MEDS",
290 "MEDICATIONS UPON TRANSFER",
291 "PREOPERATIVE MEDICATIONS",
292 "PRN MEDICATIONS",
293 "ADDENDUM TO MEDICATIONS ON DISCHARGE",
294 "MEDICATION AT TIME OF DISCHARGE",
295 "CURRENT MEDICATIONS",
296 "DRUG HISTORY",
297 "HOME MEDICATIONS",
298 "HOME MEDS",
299 "MEDICATIONS UPON DISCHARGE",
300 "MEDICATIONS UPON ADMISSION",
301 "PREOP MEDICATIONS",
302 "MEDS",
303 "MEDICATIONS",
304 );
305  
306 # ??? "MEDICATIONS ON \nADMISSION",
307  
308  
309 # DRUG HISTORY (to confirm)
310  
311 my $item_nb = -1;
312 my @items;
313  
314 my $j;
315 my @text_section;
316  
317 my $section_tail = undef;
318 if ($section =~ /\n(\n+)$/) {
319 $section_tail = $1;
320 }
321 my $k = 0;
322  
323 while(index($section, $listSection[$k]) < 0) {
324 $k++;
325 }
326  
327 my $sectionType = "narrative";
328 if ($k < scalar(@listSection)) {
329 my @sections;
330  
331 if ($section =~ /$listSection[$k]\s*:?\s*/) {
332 $section = $'; # ' }
333 &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $`);
334  
335 my %tmp = ("content" => $&,
336 # "title" => $listSection[$k],
337 );
338
339 push @sections, \%tmp;
340 &new_section($section_nb_ref, \@text_with_sections_and_items, \@sections);
341 $text_with_sections_and_items[$$section_nb_ref]->{"title"} = $listSection[$k];
342  
343 $sectionType = "list";
344
345 &new_item(\$item_nb, \@items, "");
346 my %tmp2 = ("content" => \@items,
347 "sectionType" => "list",
348 );
349  
350 push @sections, \%tmp2;
351 }
352 } else {
353 if ($section =~ /^(\s*([A-Z][^:\d\+\.]+):\s*)(\d+\.\s+)/o) {
354 my @sections;
355 my $start_listsection = $1;
356 my $start_listsection_title = $2;
357 $section = "$3$'"; # ' }
358 my %tmp = ("content" => $start_listsection,
359 # "title" => $listSection[$k],
360 );
361
362 push @sections, \%tmp;
363 &new_section($section_nb_ref, \@text_with_sections_and_items, \@sections);
364 $text_with_sections_and_items[$$section_nb_ref]->{"title"} = $start_listsection_title;
365  
366 $sectionType = "list";
367  
368 &new_item(\$item_nb, \@items, "");
369 # &add_in_section($$section_nb_ref, \@text_with_sections_and_items, \@items);
370 my %tmp2 = ("content" => \@items,
371 "sectionType" => "list",
372 );
373  
374 push @sections, \%tmp2;
375 }
376 }
377  
378 @text_section = split /\n/, $section;
379  
380 map{$_ .= "\n"} @text_section;
381  
382 for($j=0;$j<= $#text_section;$j++) {
383 if ($text_section[$j] =~ /$item_regex/) {
384 if (($item_nb == -1) && ($sectionType ne "list")) {
385 my $title;
386 my @sections;
387 if ($text_section[$j] =~ /^(\s*([A-Z][^:\d\+\.]+):\s*)/o) {
388 $title = $2;
389 $sectionType = "list";
390 my %tmp = ("content" => $1,
391 # "title" => $listSection[$k],
392 );
393 &new_item(\$item_nb, \@items, $'); # ' }
394  
395
396 push @sections, \%tmp;
397 &new_section($section_nb_ref, \@text_with_sections_and_items, \@sections, $sectionType);
398 $text_with_sections_and_items[$$section_nb_ref]->{"title"} = $title;
399
400 my %tmp2 = ("content" => \@items,
401 "sectionType" => "list",
402 );
403
404 push @sections, \%tmp2;
405
406 } else {
407 &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $text_section[$j]);
408 }
409 } else {
410 &new_item(\$item_nb, \@items, $text_section[$j]);
411 }
412 next;
413 }
414 if ($item_nb == -1) {
415 &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $text_section[$j]);
416 } else {
417 &add_in_item($item_nb, \@items, $text_section[$j]);
418 }
419 }
420 if (defined $section_tail) {
421 &add_in_section($$section_nb_ref, \@text_with_sections_and_items, $section_tail);
422 }
423 $$section_nb_ref++;
424 return(@text_with_sections_and_items);
425 }
426  
427 sub identify_title {
428 my ($text, $titles_ref) = @_;
429 my $title = "";
430  
431 if (($text ne "") && (ref($text) ne "ARRAY")) {
432 $text =~ s/^\n+//o;
433 if ($text =~ /^\**([A-Z][^:\d\+\.]+):/o) {
434 $title = $1;
435 if (length($title) != 1) {
436 $titles_ref->{$title}++;
437 }
438 } else {
439 if ($text =~ /^([A-Z\- ]+):/o) {
440 $title = $1;
441 if (length($title) != 1) {
442 $titles_ref->{$title}++;
443 }
444 }
445 }
446 }
447 return($title);
448 }
449  
450 sub identify_title_from_known_titles {
451 my ($section, $titles_ref) = @_;
452 my $known_title;
453 my $text;
454 my @sections_text;
455 my @sections;
456 my $exists_title = 0;
457 my $i = 0;
458 my $title;
459 my $new_text;
460 my $end_title_pos;
461  
462 if (ref($section->{"content"}) ne "ARRAY") {
463 # if (!exists($section->{"title"})) {
464 $text = $section->{"content"};
465 foreach $known_title (keys %{$titles_ref}) {
466 if (index($text, $known_title, 1) != -1) {
467 if ($text =~ s/(\b)(\Q$known_title\E\b)/\1<SECTION>\2<TITLE>\2/) {
468 $exists_title++;
469 }
470 }
471 }
472 if ($exists_title) {
473 @sections_text = split /<SECTION>/, $text;
474 # Processing the first sectino separately
475 $sections[0] = {"content" => $sections_text[0],};
476 if (exists $section->{"title"}) {
477 $sections[0]->{"title"} = $section->{"title"};
478 }
479 for($i=1; $i<scalar(@sections_text);$i++) {
480 $end_title_pos = index($sections_text[$i], "<TITLE>",0);
481 $title = substr($sections_text[$i], 0, $end_title_pos);
482 $new_text = substr($sections_text[$i], $end_title_pos+7);
483 $sections[$i] = {"content" => $new_text,
484 "title" => $title,
485 };
486
487 }
488 }
489 }
490 return(@sections);
491 }
492  
493 sub cleanOUTPUT {
494 my ($str) = @_;
495  
496 $str =~ s/\x{000C}//go; # CTRL-L - \f
497 $str =~ s/\x{0018}//go; # CTRL-X
498 $str =~ s/\x{0007}//go; # CTRL-G - \a
499  
500 $str =~ s/&amp;/&/go;
501 $str =~ s/&quot;/\"/og;
502 $str =~ s/&apos;/\'/og;
503 $str =~ s/&lt;/</og;
504 $str =~ s/&gt;/>/og;
505 $str =~ s/\x{0009}/ /og;
506 $str =~ s/[\x{0000}-\x{001F}]//og;
507 chomp $str;
508  
509 $str =~ s/[\x{2000}-\x{200B}]/ /go;
510 $str =~ s/[\x{2018}-\x{201B}]/\'/go;
511 $str =~ s/[\x{201C}-\x{201F}]/\"/go;
512 $str =~ s/[\x{2010}-\x{2015}]/\-/go;
513 $str =~ s/[\x{2010}-\x{2015}]/\-/go;
514 $str =~ s/[\x{2010}-\x{2015}]/\-/go;
515  
516 # PRIVATE CODES -> removed
517 $str =~ s/\x{F09F} /\n\n- /go;
518 $str =~ s/[\x{E000}-\x{F8FF}]//go;
519 $str =~ s/\x{FFFD}//go;
520 $str =~ s/\x{2024}/./go;
521 $str =~ s/\x{2025}/../go;
522 $str =~ s/\x{2026}/.../go;
523  
524  
525 # SPECIAL TREATMENT TO ADD
526 $str =~ s/\x{2022} ?\n+/\- /go;
527 $str =~ s/\x{fb00}/ff/go;
528 $str =~ s/\x{fb01}/fi/go;
529 $str =~ s/\x{fb02}/fl/go;
530 $str =~ s/\x{fb03}/ffi/go;
531 $str =~ s/\x{fb04}/ffl/go;
532 $str =~ s/\x{fb05}/ft/go;
533 $str =~ s/\x{fb06}/st/go;
534 $str =~ s/\n- \n+- /\n- /go;
535 return($str);
536 }
537  
538 ########################################################################
539  
540 =encoding utf8
541  
542 =head1 NAME
543  
544 Txt2Ogmios.pl - Script for converting text file in XML Ogmios file
545  
546 =head1 SYNOPSIS
547  
548 Txt2Ogmios.pl [option] --file <filename> --lang <language> --docId <documentId>
549  
550 where option can be --help --man
551  
552 =head1 OPTIONS AND ARGUMENTS
553  
554 =over 4
555  
556 =item --file <filename>
557  
558 The switch sets the input text file (to convert).
559  
560 =item --lang <language>
561  
562 The switch sets the language of the input text file (to convert). The
563 value are set with a ISO 639-1 code (fr for French, en for English).
564  
565 =item --docId <documentId>>
566  
567 This sets the document identifier in the xml file.
568  
569 =item --help
570  
571 print help message for using grepTerms.pl
572  
573 =item --man
574  
575 print man page of grepTerms.pl
576  
577 =back
578  
579 =head1 DESCRIPTION
580  
581 This script converts a text file as input (switch C<file>) into a XML
582 Omgios file. The language property field is set with the switch
583 C<lang>, and the document identifier is set with the switch C<dociI>.
584  
585 =head1 SEE ALSO
586  
587 =head1 AUTHOR
588  
589 Thierry Hamon, E<lt>thierry.hamon@limsi.frE<gt>
590  
591 =head1 COPYRIGHT AND LICENSE
592  
593 Copyright (C) 2012 Thierry Hamon
594  
595 This is free software; you can redistribute it and/or modify it under
596 the same terms as Perl itself, either Perl version 5.8.4 or, at your
597 option, any later version of Perl 5 you may have available.
598  
599 =cut
600