blib/script/Txt2Ogmios-TabularFiles.pl untested

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