| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use XML::LibXML; | ||
| 6 | |||
| 7 | use Getopt::Long; | ||
| 8 | use Pod::Usage; | ||
| 9 | |||
| 10 | my $verbose; | ||
| 11 | my $help; | ||
| 12 | my $man; | ||
| 13 | |||
| 14 | my $line; | ||
| 15 | my $i; | ||
| 16 | my $rep = "."; | ||
| 17 | my $prefix; | ||
| 18 | my $base; | ||
| 19 | my $doc_xml = ""; | ||
| 20 | my @tab_docs_xml; | ||
| 21 | my $docset; | ||
| 22 | |||
| 23 | my $collectionMax; | ||
| 24 | my @collection; | ||
| 25 | |||
| 26 | my $charset = 'UTF-8'; | ||
| 27 | |||
| 28 | my $xmlhead="<?xml version=\"1.0\" encoding=\"$charset\"?>\n<documentCollection xmlns=\"http://alvis.info/enriched/\" version=\"1.1\">\n"; | ||
| 29 | my $xmlfoot="</documentCollection>\n"; | ||
| 30 | |||
| 31 | if (scalar(@ARGV) ==0) { | ||
| 32 | $help = 1; | ||
| 33 | } | ||
| 34 | |||
| 35 | Getopt::Long::Configure ("bundling"); | ||
| 36 | |||
| 37 | GetOptions('help|?' => \$help, | ||
| 38 | 'man' => \$man, | ||
| 39 | 'verbose|v' => \$verbose, | ||
| 40 | 'dir|d=s' => \$rep, | ||
| 41 | 'prefix|p:s' => \$prefix, | ||
| 42 | 'base|b=s' => \$base, | ||
| 43 | 'size|s=i' => \$collectionMax, | ||
| 44 | 'file|f=s' => \@collection, | ||
| 45 | ); | ||
| 46 | |||
| 47 | pod2usage(1) if $help; | ||
| 48 | pod2usage(-exitstatus => 0, -verbose => 2) if $man; | ||
| 49 | |||
| 50 | |||
| 51 | foreach $docset (@collection) { | ||
| 52 | $doc_xml = ""; | ||
| 53 | print STDERR "Reading $docset ... "; | ||
| 54 | open DOCSET, $docset; | ||
| 55 | |||
| 56 | binmode(DOCSET, ":utf8"); | ||
| 57 | while($line=<DOCSET>) { | ||
| 58 | $doc_xml .= $line; | ||
| 59 | } | ||
| 60 | close DOCSET; | ||
| 61 | print STDERR "done\n"; | ||
| 62 | |||
| 63 | print STDERR "Splitting $docset ... "; | ||
| 64 | @tab_docs_xml = &split_to_docRecs($doc_xml); | ||
| 65 | print STDERR "done\n"; | ||
| 66 | |||
| 67 | warn "\t" . scalar(@tab_docs_xml) . " documents split\n"; | ||
| 68 | warn "\t" . scalar(@tab_docs_xml) . " documents to print\n"; | ||
| 69 | |||
| 70 | warn "\tStart at " . ($base+1) . "\n"; | ||
| 71 | |||
| 72 | print STDERR "Printing $docset ... "; | ||
| 73 | my $j = 0; | ||
| 74 | for($i = 0; $i<scalar(@tab_docs_xml); $i++) { | ||
| 75 | |||
| 76 | if ($i % $collectionMax == 0) { | ||
| 77 | open DOCREC, ">$rep/$prefix-" . ($base + $j + 1) . ".xml" or die "No such file\n"; | ||
| 78 | binmode(DOCREC, ":utf8"); | ||
| 79 | $j++; | ||
| 80 | print DOCREC $xmlhead; | ||
| 81 | } | ||
| 82 | |||
| 83 | print DOCREC ${$tab_docs_xml[$i]}[1]; | ||
| 84 | if (($i + 1) % $collectionMax == 0) { | ||
| 85 | print DOCREC $xmlfoot; | ||
| 86 | close DOCREC; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | if (($i + 1) % $collectionMax != 0) { | ||
| 90 | print DOCREC $xmlfoot; | ||
| 91 | close DOCREC; | ||
| 92 | } | ||
| 93 | print STDERR "done\n"; | ||
| 94 | |||
| 95 | print STDERR "\tLast document number: $i\n\n"; | ||
| 96 | $base += $i; | ||
| 97 | } | ||
| 98 | |||
| 99 | # print STDERR "Building the $rep/seq file ... "; | ||
| 100 | # open SEQ, ">$rep/seq"; | ||
| 101 | # print SEQ "0 " . ($base - 1) . "\n"; | ||
| 102 | # close SEQ; | ||
| 103 | # print STDERR "done\n"; | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | sub split_to_docRecs | ||
| 108 | { | ||
| 109 | my $xml=shift; | ||
| 110 | |||
| 111 | my @recs=(); | ||
| 112 | |||
| 113 | my $doc; | ||
| 114 | my $Parser=XML::LibXML->new(); | ||
| 115 | |||
| 116 | eval | ||
| 117 | { | ||
| 118 | $doc=$Parser->parse_string($xml); | ||
| 119 | }; | ||
| 120 | if ($@) | ||
| 121 | { | ||
| 122 | warn "Parsing the doc failed: $@. Trying to get the IDs..\n"; | ||
| 123 | eval | ||
| 124 | { | ||
| 125 | $xml=~s/<documentRecord\s(xmlns=[^\s]+)*\sid\s*=\s*\"([^\"]*?)\">/&unparseable_id($2)/esgo; | ||
| 126 | }; | ||
| 127 | } | ||
| 128 | else | ||
| 129 | { | ||
| 130 | if ($doc) | ||
| 131 | { | ||
| 132 | my $root=$doc->documentElement(); | ||
| 133 | for my $rec_node ($root->getChildrenByTagName('documentRecord')) | ||
| 134 | { | ||
| 135 | my $id=$rec_node->getAttribute("id"); | ||
| 136 | if (defined($id)) | ||
| 137 | { | ||
| 138 | $xml=$rec_node->toString(); | ||
| 139 | push(@recs,[$id,$xml]); | ||
| 140 | } | ||
| 141 | else | ||
| 142 | { | ||
| 143 | my $rec_str=$rec_node->toString(); | ||
| 144 | $rec_str=~s/\n/ /sgo; | ||
| 145 | warn "No id for record $rec_str\n"; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | else | ||
| 150 | { | ||
| 151 | my $doc_str=$xml; | ||
| 152 | $doc_str=~s/\n/ /sgo; | ||
| 153 | warn "Parsing the doc failed. Doc: $doc_str\n"; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | return @recs; | ||
| 158 | } | ||
| 159 | |||
| 160 | ######################################################################## | ||
| 161 | |||
| 162 | =head1 NAME | ||
| 163 | |||
| 164 | splitCollection.pl - Perl script for spliting a collection of XML documents | ||
| 165 | |||
| 166 | |||
| 167 | =head1 SYNOPSIS | ||
| 168 | |||
| 169 | splitCollection.pl [--help] [--man] [--rcfile=file] [--lang=lang] [--params=params] [--format=format] | ||
| 170 | |||
| 171 | =head1 OPTIONS AND ARGUMENTS | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =item B<--help> brief help message | ||
| 176 | |||
| 177 | =item B<--man> full documentation | ||
| 178 | |||
| 179 | =item B<--verbose> go into the verbose mode | ||
| 180 | |||
| 181 | =item B<--file> <fileame>, B<--file> <fileame> file to split | ||
| 182 | |||
| 183 | =item B<--dir> <dirname>, B<-d> <dirname> directory containing the output files (by default, the current directory) | ||
| 184 | |||
| 185 | =item B<--prefix> <prefix>, B<-p> <prefix> prefix of the output filenames | ||
| 186 | |||
| 187 | =item B<--base> <number>, B<-b> <number> start number of the output filenames | ||
| 188 | |||
| 189 | =item B<--size> <size>, B<-s> <size> number of documents per output file | ||
| 190 | |||
| 191 | =back | ||
| 192 | |||
| 193 | =head1 DESCRIPTION | ||
| 194 | |||
| 195 | The script splits a document collection in ALVIS XML format into | ||
| 196 | several files in the same format. The ALVIS XML format is the format | ||
| 197 | used by the Ogmios platform to load file and record linguistic | ||
| 198 | annotations. | ||
| 199 | |||
| 200 | Intput file is given by the option C<--file>. Output files are stored | ||
| 201 | in a directory specified with the option C<--dir>. Each output file | ||
| 202 | has the prefix indicated by the option C<--prefix>. | ||
| 203 | |||
| 204 | =head1 EXAMPLE | ||
| 205 | |||
| 206 | SplitCollection.pl -f examples/twodocs.xml -d examples -p subcoll -b 1 -s 1 | ||
| 207 | |||
| 208 | =head1 AUTHOR | ||
| 209 | |||
| 210 | Thierry Hamon, E<lt>thierry.hamon@limsi.frE<gt> | ||
| 211 | |||
| 212 | =head1 COPYRIGHT AND LICENSE | ||
| 213 | |||
| 214 | Copyright (C) 2012 Thierry Hamon | ||
| 215 | |||
| 216 | This is free software; you can redistribute it and/or modify it under | ||
| 217 | the same terms as Perl itself, either Perl version 5.8.4 or, at your | ||
| 218 | option, any later version of Perl 5 you may have available. | ||
| 219 | |||
| 220 | =cut | ||
| 221 |