Tools.pm untested

filter: Statementstmt 0.0% 0 / 407 Branchbran 0.0% 0 / 224 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 709 CC 111 SCAR 51.5
CC
111
Cov
0%
set_corpus_info
420.0
find_sentalign_file
306.0
find_opus_document
272.0
CRAP 172.3
1 #-*-perl-*-
2 #---------------------------------------------------------------------------
3 # Copyright (C) 2004-2017 Joerg Tiedemann
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #---------------------------------------------------------------------------
19  
20 =head1 NAME
21  
22 OPUS::Tools - a collection of tools for processing OPUS corpora
23  
24 =head1 SYNOPSIS
25  
26 # read bitexts (print aligned sentences to screen in readable format)
27 opus-read OPUS/corpus/RF/xml/en-fr.xml.gz | less
28  
29 # convert an OPUS bitext to plain text (Moses) format
30 zcat OPUS/corpus/RF/xml/en-fr.xml.gz | opus2moses -d OPUS/corpus/RF/xml -e RF.en-fr.en -f RF.en-fr.fr
31  
32 # create a multilingual corpus from the parallel RF corpus
33 # using 'en' as the pivot language
34 opus2multi OPUS/corpus/RF/xml sv de en es fr
35  
36  
37 =head1 DESCRIPTION
38  
39 This is not a library but just a collection of scripts for processing/converting OPUS corpora.
40 Download corpus data in XML from L<http://opus.lingfil.uu.se>
41  
42  
43 =cut
44  
45 package OPUS::Tools;
46  
47 use strict;
48 use DB_File;
49 use Exporter 'import';
50  
51 use Archive::Zip qw/ :ERROR_CODES :CONSTANTS /;
52 use Archive::Zip::MemberRead;
53 use File::Basename qw(dirname basename);
54  
55 use OPUS::Tools::ISO639 qw / iso639_TwoToThree iso639_ThreeToName /;
56  
57  
58 our @EXPORT = qw(set_corpus_info delete_all_corpus_info
59 find_opus_document find_opus_documents
60 find_bitext find_sentalign_file
61 open_bitext open_opus_document
62 $OPUS_HOME $OPUS_CORPUS $OPUS_HTML
63 $OPUS_DOWNLOAD $OPUS_RELEASES
64 $OPUS_PUBLIC);
65  
66  
67 # set OPUS home dir
68 my @ALT_OPUS_HOME = ( '/proj/OPUS', # taito
69 '/projects/nlpl/data/OPUS', # abel
70 '/home/opus/OPUS', # lingfil
71 $ENV{HOME}.'/OPUS', # user home
72 $ENV{HOME}.'/research/OPUS');
73  
74 our $OPUS_HOME;
75 foreach (@ALT_OPUS_HOME){
76 if (-d $_){
77 $OPUS_HOME = $_;
78 last;
79 }
80 }
81  
82  
83 ## set OPUS release dir
84 my @ALT_OPUS_NLPL = ( '/projappl/nlpl/data/OPUS', # puhti
85 '/proj/nlpl/data/OPUS', # taito
86 '/projects/nlpl/data/OPUS', # abel
87 $OPUS_HOME.'/releases');
88  
89 our $OPUS_RELEASES = $OPUS_HOME;
90 foreach (@ALT_OPUS_NLPL){
91 if (-d $_){
92 $OPUS_RELEASES = $_;
93 last;
94 }
95 }
96  
97  
98 # our $OPUS_HOME = '/proj/nlpl/data/OPUS';
99 our $OPUS_PUBLIC = $OPUS_HOME.'/public_html';
100 # our $OPUS_PUBLIC = $OPUS_HOME.'/web';
101 our $OPUS_HTML = $OPUS_HOME.'/html';
102 our $OPUS_CORPUS = $OPUS_HOME.'/corpus';
103 our $OPUS_DOWNLOAD = $OPUS_HOME.'/download';
104 our $INFODB_HOME = $OPUS_PUBLIC;
105  
106 our $VERBOSE = 0;
107  
108  
109  
110 ## variables for info databases
111  
112 my %LangNames;
113 my %Corpora;
114 my %LangPairs;
115 my %Bitexts;
116 my %Info;
117  
118 my $DBOPEN = 0;
119  
120 ## ZipFiles = hash of zip file handles (key = zipfile)
121 ## CorpusBase = corpus base dir of zip archives
122 my %ZipFiles;
123 my %CorpusBase;
124  
125  
126 ## various database files that keep information about corpora
127 ##
128 ## LangNames: maps language names to language Ids
129 ## Corpora: "language" -> list-of-corpora
130 ## LangPairs: "source-lang" -> list-of-target-langs
131 ## Bitexts: "src-trg" -> list-of-corpora
132 ## Info: "corpus/src-trg" -> list-of-files-and-statistics
133  
134 sub open_info_dbs{
135 system("mkdir -p $INFODB_HOME") unless (-d $INFODB_HOME);
136 print STDERR "open info DBs in $INFODB_HOME\n";
137 tie %LangNames,"DB_File","$INFODB_HOME/LangNames.db";
138 tie %Corpora,"DB_File","$INFODB_HOME/Corpora.db";
139 tie %LangPairs,"DB_File","$INFODB_HOME/LangPairs.db";
140 tie %Bitexts,"DB_File","$INFODB_HOME/Bitexts.db";
141 tie %Info,"DB_File","$INFODB_HOME/Info.db";
142 $DBOPEN = 1;
143 }
144  
145 sub close_info_dbs{
146 untie %LangNames;
147 untie %Corpora;
148 untie %LangPairs;
149 untie %Bitexts;
150 untie %Info;
151 $DBOPEN = 0;
152 }
153  
154 sub set_corpus_info{
155 my ($corpus,$release,$src,$trg,$infostr) = @_;
156  
157 unless (defined $corpus && defined $src && defined $trg){
158 print STDERR "specify corpus src trg";
159 return 0;
160 }
161 $release = get_corpus_version($corpus) unless ($release);
162  
163 my $ReleaseBase = $corpus.'/'.$release;
164 my $ReleaseDir = $OPUS_RELEASES.'/'.$ReleaseBase;
165 my $corpuskey = $corpus.'@'.$release;
166  
167 &open_info_dbs unless ($DBOPEN);
168  
169 ## set corpus for source and target language
170 foreach my $l ($src,$trg){
171 if (! exists $LangNames{$l}){
172 $LangNames{$l} = &iso639_ThreeToName(&iso639_TwoToThree($l));
173 }
174 if (exists $Corpora{$l}){
175 my @corpora = split(/\:/,$Corpora{$l});
176 my ($found) = grep(index($_,$corpus.'@') == 0,@corpora);
177 if ($found){
178 my @releases = split(/\@/,$found);
179 shift(@releases);
180 unless (grep($_ eq $release,@releases)){
181 $found .= '@'.$release;
182 @corpora = grep(index($_,$corpus.'@') != 0,@corpora);
183 push(@corpora,$found);
184 }
185 }
186 else{
187 push(@corpora,$corpuskey);
188 }
189 @corpora = sort @corpora;
190 $Corpora{$l} = join(':',@corpora);
191 }
192 else{
193 $Corpora{$l} = $corpuskey;
194 }
195 }
196  
197 ## set corpus for bitext
198 my $langpair = join('-',sort ($src,$trg));
199 if (exists $Bitexts{$langpair}){
200 my @corpora = split(/\:/,$Bitexts{$langpair});
201 my ($found) = grep(index($_,$corpus.'@') == 0,@corpora);
202 if ($found){
203 my @releases = split(/\@/,$found);
204 shift(@releases);
205 unless (grep($_ eq $release,@releases)){
206 $found .= '@'.$release;
207 @corpora = grep(index($_,$corpus.'@') != 0,@corpora);
208 push(@corpora,$found);
209 }
210 }
211 else{
212 push(@corpora,$corpuskey);
213 }
214 @corpora = sort @corpora;
215 $Bitexts{$langpair} = join(':',@corpora);
216 }
217 else{
218 $Bitexts{$langpair} = $corpuskey;
219 }
220  
221  
222 ## set src-trg
223 if (exists $LangPairs{$src}){
224 my @lang = split(/\:/,$LangPairs{$src});
225 unless (grep($_ eq $trg,@lang)){
226 push(@lang,$trg);
227 @lang = sort @lang;
228 $LangPairs{$src} = join(':',@lang);
229 }
230 }
231 else{
232 $LangPairs{$src} = $trg;
233 }
234  
235 ## set trg-src
236 if (exists $LangPairs{$trg}){
237 my @lang = split(/\:/,$LangPairs{$trg});
238 unless (grep($_ eq $src,@lang)){
239 push(@lang,$src);
240 @lang = sort @lang;
241 $LangPairs{$trg} = join(':',@lang);
242 }
243 }
244 else{
245 $LangPairs{$trg} = $src;
246 }
247  
248 unless ($infostr){
249 $infostr = read_info_files($corpus,$release,$src,$trg);
250 }
251  
252 my $key = $corpus.'/'.$release.'/'.$langpair;
253 if ($infostr){
254 if ($VERBOSE){
255 if (exists $Info{$key}){
256 my $info = $Info{$key};
257 print STDERR "overwrite corpus info!\n";
258 print STDERR "old = ",$Info{$key},"\n";
259 print STDERR "new = ",$infostr,"\n";
260 }
261 }
262 $Info{$key} = $infostr;
263 }
264  
265 ## info for monolingual data
266 for my $l ($src,$trg){
267 $infostr = read_info_files($corpus,$release,$l);
268 if ($infostr){
269 $key = $corpus.'/'.$release.'/'.$l;
270 $Info{$key} = $infostr;
271 }
272 }
273 }
274  
275  
276 sub delete_all_corpus_info{
277 my ($corpus) = @_;
278  
279 unless (defined $corpus){
280 print STDERR "specify corpus src trg";
281 return 0;
282 }
283  
284 &open_info_dbs unless ($DBOPEN);
285  
286 foreach my $c (keys %Corpora){
287 my @corpora = split(/\:/,$Corpora{$c});
288 if (grep($_ eq $corpus,@corpora)){
289 @corpora = grep(index($_,$corpus.'@') != 0,@corpora);
290 @corpora = grep($_ ne '',@corpora);
291 $Corpora{$c} = join(':',@corpora);
292 }
293 }
294  
295 my %src2trg=();
296  
297 foreach my $c (keys %Bitexts){
298 my @corpora = split(/\:/,$Bitexts{$c});
299 if (grep($_ eq $corpus,@corpora)){
300 @corpora = grep(index($_,$corpus.'@') != 0,@corpora);
301 @corpora = grep($_ ne '',@corpora);
302 if (@corpora){
303 $Bitexts{$c} = join(':',@corpora);
304 my ($s,$t) = split(/\-/,$c);
305 $src2trg{$s}{$t}++;
306 $src2trg{$t}{$s}++;
307 }
308 else{
309 delete $Bitexts{$c};
310 }
311 }
312 else{
313 my ($s,$t) = split(/\-/,$c);
314 $src2trg{$s}{$t}++;
315 $src2trg{$t}{$s}++;
316 }
317 }
318
319 foreach my $l (keys %LangPairs){
320 $LangPairs{$l}=join(':',sort keys %{$src2trg{$l}});
321 }
322  
323 foreach my $i (keys %Info){
324 my ($c,$l,$r) = split(/\//,$i);
325 if ($c eq $corpus){
326 delete $Info{$i};
327 }
328 }
329 }
330  
331  
332 sub get_corpus_version{
333 my ($corpus) = @_;
334 if (-e "$OPUS_HOME/corpus/Makefile.def"){
335 open F, "<$OPUS_HOME/corpus/Makefile.def";
336 while (<F>){
337 if (/VERSION\s+=\s+(\S+)(\s|\Z)/){
338 return $1;
339 }
340 }
341 }
342 return 'latest';
343 }
344  
345  
346 sub read_info_files{
347 my ($corpus,$release,$src,$trg) = @_;
348  
349 return &read_bitext_info(@_) if ($trg);
350 return &read_monolingual_info(@_) if ($src);
351 return '';
352 }
353  
354 sub read_bitext_info{
355 my ($corpus,$release,$src,$trg) = @_;
356  
357 my $langpair = join('-',sort ($src,$trg));
358 $release = get_corpus_version($corpus) unless ($release);
359 my $ReleaseBase = $corpus.'/'.$release;
360 my $ReleaseDir = $OPUS_RELEASES.'/'.$ReleaseBase;
361 my $InfoDir = $ReleaseDir.'/info';
362  
363 my $moses = 'moses='.$ReleaseBase.'/moses/'.$langpair.'.txt.zip';
364 my $tmx = 'tmx='.$ReleaseBase.'/tmx/'.$langpair.'.tmx.gz';
365 my $xces = 'xces='.$ReleaseBase.'/xml/'.$langpair.'.xml.gz:';
366 $xces .= $ReleaseBase.'/xml/'.$src.'.zip:';
367 $xces .= $ReleaseBase.'/xml/'.$trg.'.zip';
368  
369 # my $moses = 'moses=moses/'.$langpair.'.txt.zip';
370 # my $tmx = 'tmx=tmx/'.$langpair.'.tmx.gz';
371 # my $xces = 'xces=xml/'.$langpair.'.xml.gz:';
372 # $xces .= 'xml/'.$src.'.zip:';
373 # $xces .= 'xml/'.$trg.'.zip';
374  
375 my @infos = ();
376  
377 if (-e "$InfoDir/$langpair.txt.info"){
378 open F, "<$InfoDir/$langpair.txt.info";
379 my @val = <F>;
380 chomp(@val);
381 $moses .= ':'.join(':',@val);
382 push(@infos,$moses);
383 }
384  
385 if (-e "$InfoDir/$langpair.tmx.info"){
386 open F, "<$InfoDir/$langpair.tmx.info";
387 my @val = <F>;
388 chomp(@val);
389 $tmx .= ':'.join(':',@val);
390 push(@infos,$tmx);
391 }
392  
393 if (-e "$InfoDir/$langpair.info"){
394 open F, "<$InfoDir/$langpair.info";
395 my @val = <F>;
396 chomp(@val);
397 $xces .= ':'.join(':',@val);
398 push(@infos,$xces);
399 }
400  
401 if (-e "$ReleaseDir/smt/$langpair.alg.zip"){
402 push(@infos,"alg=$ReleaseBase/smt/$langpair.alg.zip");
403 }
404 if (-e "$ReleaseDir/smt/$langpair.zip"){
405 push(@infos,"smt=$ReleaseBase/smt/$langpair.zip");
406 }
407 if (-e "$ReleaseDir/dic/$langpair.dic.gz"){
408 push(@infos,"dic=$ReleaseBase/dic/$langpair.dic.gz");
409 }
410  
411 return join('+',@infos);
412 }
413  
414  
415 sub read_monolingual_info{
416 my ($corpus,$release,$lang) = @_;
417  
418 $release = get_corpus_version($corpus) unless ($release);
419 my $ReleaseBase = $corpus.'/'.$release;
420 my $ReleaseDir = $OPUS_RELEASES.'/'.$ReleaseBase;
421 my $InfoDir = $ReleaseDir.'/info';
422  
423 my @infos = ();
424  
425 my @langstats = ();
426 if (-e "$InfoDir/$lang.info"){
427 open F, "<$InfoDir/$lang.info";
428 @langstats = <F>;
429 chomp(@langstats);
430 }
431  
432 if (-e "$ReleaseDir/xml/$lang.zip"){
433 my $resource = "xml=$ReleaseBase/xml/$lang.zip";
434 $resource .= ':'.join(':',@langstats) if (@langstats);
435 push(@infos,$resource);
436 }
437 if (-e "$ReleaseDir/raw/$lang.zip"){
438 my $resource = "raw=$ReleaseBase/raw/$lang.zip";
439 pop(@langstats);
440 $resource .= ':'.join(':',@langstats) if (@langstats);
441 push(@infos,$resource);
442 }
443 if (-e "$ReleaseDir/mono/$lang.txt.gz"){
444 push(@infos,"mono=$ReleaseBase/mono/$lang.txt.gz");
445 }
446 if (-e "$ReleaseDir/mono/$lang.tok.gz"){
447 push(@infos,"monotok=$ReleaseBase/mono/$lang.tok.gz");
448 }
449 if (-e "$ReleaseDir/freq/$lang.freq.gz"){
450 push(@infos,"freq=$ReleaseBase/freq/$lang.freq.gz");
451 }
452 if (-e "$ReleaseDir/parsed/$lang.zip"){
453 push(@infos,"parsed=$ReleaseBase/parsed/$lang.zip");
454 }
455  
456 return join('+',@infos);
457 }
458  
459  
460  
461  
462  
463 # old style of download files in tar
464  
465 sub read_old_info_files{
466 my ($corpus,$src,$trg) = @_;
467  
468 my $CorpusXML = $OPUS_HOME.'/corpus/'.$corpus.'/xml';
469 my $langpair = join('-',sort ($src,$trg));
470  
471 my $key = $corpus.'/'.$langpair;
472 my $moses = 'moses='.$key.'.txt.zip';
473 my $tmx = 'tmx='.$key.'.tmx.gz';
474 my $xces = 'xces='.$key.'.xml.gz:';
475 $xces .= $corpus.'/'.$src.'.tar.gz:';
476 $xces .= $corpus.'/'.$trg.'.tar.gz';
477  
478 my @infos = ();
479  
480 if (-e "$CorpusXML/$langpair.txt.info"){
481 open F, "<$CorpusXML/$langpair.txt.info";
482 my @val = <F>;
483 chomp(@val);
484 $moses .= ':'.join(':',@val);
485 push(@infos,$moses);
486 }
487  
488 if (-e "$CorpusXML/$langpair.tmx.info"){
489 open F, "<$CorpusXML/$langpair.tmx.info";
490 my @val = <F>;
491 chomp(@val);
492 $tmx .= ':'.join(':',@val);
493 push(@infos,$tmx);
494 }
495  
496 if (-e "$CorpusXML/$langpair.info"){
497 open F, "<$CorpusXML/$langpair.info";
498 my @val = <F>;
499 chomp(@val);
500 $xces .= ':'.join(':',@val);
501 push(@infos,$xces);
502 }
503  
504 return join('+',@infos);
505 }
506  
507  
508 sub open_bitext{
509 my ($Bitext,$CorpusName,$CorpusRelease,$CorpusType,$SrcId,$TrgId) = @_;
510 my ($SentAlignFile, $CorpusDir) = find_bitext($Bitext,$CorpusName,
511 $CorpusRelease,$CorpusType,
512 $SrcId,$TrgId);
513 my $fh;
514 if ($SentAlignFile=~/\.gz/){
515 open $fh,"gzip -cd <$SentAlignFile |";
516 }
517 else{
518 open $fh,"<$SentAlignFile";
519 }
520 return wantarray ? ($fh,$CorpusDir) : $fh;
521 }
522  
523  
524 ## find the bitext given some parameters
525 ## return (SentAlignFile,CorpusDir)
526  
527 sub find_bitext{
528 my ($Bitext,$CorpusName,$CorpusRelease,$CorpusType,$SrcId,$TrgId) = @_;
529  
530 ## bitext = langpair if not specified otherwise
531 $Bitext = join('-',sort($SrcId,$TrgId)) unless ($Bitext);
532  
533 ## unless we have the path to the sentence alignment file
534 ## split into components giving either
535 ##
536 ## CorpusName/Release/Type/LangPair
537 ## CorpusName/Type/LangPair
538 ## CorpusName/LangPair
539 ##
540 ## sentence alignments are expected to be in either
541 ## OPUS_RELEASES/CorpusName/Release/xml/LangPair.xml.gz OR
542 ## OPUS_CORPUS/CorpusName/xml/LangPair.xml.gz
543  
544 unless (-e $Bitext){
545 my @parts = split(/\//,$Bitext);
546 my $LangPair = pop(@parts);
547 $CorpusName = shift(@parts) if (@parts);
548 $CorpusType = pop(@parts) if (@parts);
549 $CorpusRelease = pop(@parts) if (@parts);
550  
551 $Bitext = join('/',($OPUS_RELEASES,$CorpusName,
552 $CorpusRelease,'xml',$LangPair));
553 $Bitext .= '.xml.gz';
554 unless (-e $Bitext){
555 $Bitext = join('/',($OPUS_CORPUS,$CorpusName,
556 'xml',$LangPair));
557 $Bitext .= '.xml.gz';
558 }
559 }
560  
561 ## set corpus home dir
562 ## (relative to aligned documents in sentence alignment file)
563 ##
564 ## - use CorpusName if it is a directory OR
565 ## - OPUS_RELEASES/CorpusName/CorpusRelease/CorpusType OR
566 ## - OPUS_CORPUS/CorpusName/CorpusType
567  
568 my $CorpusDir = undef;
569 if (-d $CorpusName){
570 $CorpusDir = $CorpusName;
571 }
572 elsif ($CorpusName){
573 $CorpusDir = join('/',($OPUS_RELEASES,$CorpusName,
574 $CorpusRelease,$CorpusType));
575 unless (-d $CorpusDir){
576 $CorpusDir = join('/',($OPUS_CORPUS,$CorpusName,$CorpusType));
577 }
578 }
579 return ($Bitext,$CorpusDir);
580 }
581  
582  
583  
584  
585  
586 ## make some guesses to find a document if the path in doc does not exist
587 sub find_sentalign_file{
588 my ($dir,$SrcID,$TrgID,$ALIGN,$release) = @_;
589  
590 if ($dir && -d $dir){
591 return "$dir/$SrcID-$TrgID.xml.gz" if (-e "$dir/$SrcID-$TrgID.xml.gz");
592 return "$dir/$TrgID-$SrcID.xml.gz" if (-e "$dir/$TrgID-$SrcID.xml.gz");
593 }
594  
595 return $ALIGN if (-e $ALIGN);
596 return "$ALIGN.gz" if (-e "$ALIGN.gz");
597 return "$dir/$ALIGN" if (-d $dir && -e "$dir/$ALIGN");
598 return "$dir/$ALIGN.gz" if (-d $dir && -e "$dir/$ALIGN.gz");
599  
600 $dir = dirname($ALIGN) unless ($dir);
601 my @ALTDIR = ( $dir,
602 "$OPUS_RELEASES/$dir/$release",
603 "$OPUS_RELEASES/$dir",
604 "$OPUS_CORPUS/$dir" );
605  
606 foreach (@ALTDIR){
607 if (-d "$_/xml"){
608 $dir = "$_/xml";
609 last;
610 }
611 elsif (-d "$_/raw"){
612 $dir = "$_/raw";
613 last;
614 }
615 }
616  
617 unless($ALIGN){
618 return "$dir/$SrcID-$TrgID.xml.gz" if (-e "$dir/$SrcID-$TrgID.xml.gz");
619 return "$dir/$TrgID-$SrcID.xml.gz" if (-e "$dir/$TrgID-$SrcID.xml.gz");
620 }
621  
622 my $base = basename($ALIGN);
623 if (-f "$dir/$base"){ $ALIGN = "$dir/$base"; }
624 elsif (-f "$dir/$base.gz"){ return "$dir/$base.gz"; }
625 elsif (-f "$dir/$base.xml.gz"){ return "$dir/$base.xml.gz"; }
626 }
627  
628  
629  
630  
631 ## make some guesses to find a document if the path in doc does not exist
632 sub find_opus_document{
633 my ($dir,$doc) = @_;
634  
635 return "$dir/$doc" if (-e "$dir/$doc");
636 return $doc if (-e $doc);
637  
638 ## gzipped and w/o dir
639 return "$doc.gz" if (-e "$doc.gz");
640 return "$dir/$doc.gz" if (-e "$dir/$doc.gz");
641  
642 ## various alternatives in OPUS_CORPUS homedir
643 return "$OPUS_CORPUS/$dir/$doc" if (-e "$OPUS_CORPUS/$dir/$doc");
644 return "$OPUS_CORPUS/$dir/$doc.gz" if (-e "$OPUS_CORPUS/$dir/$doc.gz");
645 return "$OPUS_CORPUS/$doc" if (-e "$OPUS_CORPUS/$doc");
646 return "$OPUS_CORPUS/$doc.gz" if (-e "$OPUS_CORPUS/$doc.gz");
647 return "$OPUS_CORPUS/$dir/xml/$doc" if (-e "$OPUS_CORPUS/$dir/xml/$doc");
648 return "$OPUS_CORPUS/$dir/xml/$doc.gz" if (-e "$OPUS_CORPUS/$dir/xml/$doc.gz");
649 return "$OPUS_CORPUS/$dir/raw/$doc" if (-e "$OPUS_CORPUS/$dir/raw/$doc");
650 return "$OPUS_CORPUS/$dir/raw/$doc.gz" if (-e "$OPUS_CORPUS/$dir/raw/$doc.gz");
651  
652 ## try /raw/ instead of /xml/
653 my $tmpdoc = $doc;
654 $tmpdoc =~s/(\A|\/)xml\//${1}raw\//;
655 return find_opus_document($dir,$tmpdoc) if ($doc ne $tmpdoc);
656  
657 my $tmpdir = $dir;
658 $tmpdir =~s/(\A|\/)xml(\/|\Z)/${1}raw$2/;
659 return find_opus_document($tmpdir,$doc) if ($dir ne $tmpdir);
660  
661 if (($doc ne $tmpdoc) && ($dir ne $tmpdir)){
662 return find_opus_document($tmpdir,$tmpdoc);
663 }
664 return undef;
665 }
666  
667  
668  
669 ## open zip files and store a handle in ZipFiles
670 sub open_zip_file{
671 my $corpus = shift;
672 if (exists $ZipFiles{$corpus}){
673 return $ZipFiles{$corpus};
674 }
675  
676 my $zipfile = $corpus;
677 unless (-e $zipfile){
678 my @parts = split(/\//,$zipfile);
679 if ($#parts){
680 $parts[-2] = 'raw';
681 $zipfile = join('/',@parts);
682 }
683 }
684 if (-e $zipfile){
685 $ZipFiles{$corpus} = Archive::Zip->new($zipfile);
686 my $basename = basename($zipfile);
687 $basename =~s/\.zip$//;
688 my $fh = Archive::Zip::MemberRead->new($ZipFiles{$corpus},
689 $basename.'/'.'INFO');
690 unless ($fh->{member}){
691 $fh = Archive::Zip::MemberRead->new($ZipFiles{$corpus},'INFO');
692 }
693 if ($fh->{member}){
694 my $line = $fh->getline();
695 my ($name,$type) = split(/\//,$line);
696 $CorpusBase{$corpus} = $name.'/'.$type;
697 }
698 return $ZipFiles{$corpus} if ($ZipFiles{$corpus});
699 }
700  
701 delete $ZipFiles{$zipfile};
702 return undef;
703 }
704  
705  
706 ## make some guesses to find a document if the path in doc does not exist
707 sub open_opus_document{
708 my ($fh,$dir,$doc) = @_;
709  
710 my ($lang) = split(/\//,$doc);
711 my $zipfile = $dir.'/'.$lang.'.zip';
712  
713 ## try to open a zip file
714 my $zip = open_zip_file($zipfile);
715 if ($zip){
716 $doc =~s/\.gz$//; # remove .gz extension
717 my $FileBase = $CorpusBase{$zipfile}; # file base in the corpus
718 $$fh = Archive::Zip::MemberRead->new($zip,$FileBase.'/'.$doc);
719 return 1 if ($$fh);
720 }
721  
722 ## no zip file? then look for physical file
723 if ($doc = find_opus_document($dir,$doc)){
724 close $$fh if (defined $$fh);
725 if ($doc=~/\.gz$/){
726 return open $$fh,"gzip -cd <$doc |";
727 }
728 else{
729 return open $$fh,"<$doc";
730 }
731 }
732 ## no file found - try zip archives
733 return 0;
734 }
735  
736  
737  
738 =head1
739  
740 find_opus_documents($dir,$ext[,$mindepth[,$depth]])
741  
742 =cut
743  
744 sub find_opus_documents{
745 my $dir = shift;
746 my $ext = shift;
747 my $mindepth = shift;
748 my $depth = shift;
749  
750 my $ext = 'xml' unless (defined $ext);
751  
752 ## return files in zip files if available
753 my $zip = open_zip_file($dir.'.zip');
754 if ($zip){
755 return $zip->memberNames();
756 }
757  
758 my @docs=();
759 if (opendir(DIR, $dir)){
760 my @files = grep { /^[^\.]/ } readdir(DIR);
761 closedir DIR;
762 foreach my $f (@files){
763 if (-f "$dir/$f" && $f=~/\.$ext(.gz)?$/){
764 if ((not defined($mindepth)) ||
765 ($depth>=$mindepth)){
766 push (@docs,"$dir/$f");
767 }
768 }
769 if (-d "$dir/$f"){
770 $depth++;
771 push (@docs,FindDocuments("$dir/$f",$ext,$mindepth,$depth));
772 }
773 }
774 }
775 return @docs;
776 }
777  
778 # my @files = $zip->memberNames();
779  
780  
781  
782 1;
783  
784 __END__