blib/lib/Uplug/IO/LiuAlign.pm untested

filter: Statementstmt 0.0% 0 / 252 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 411 CC 51 SCAR 37.1
CC
51
Cov
0%
write
90.0
FindDataFile
72.0
open
56.0
CRAP 41.0
1 # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  
17 #####################################################################
18 #
19 # LiuStream -
20 #
21 #####################################################################
22 # $Author$
23 # $Id$
24  
25  
26 package Uplug::IO::LiuAlign;
27  
28 use strict;
29 use vars qw(@ISA);
30 use vars qw(%StreamOptions);
31 use Uplug::IO::XML;
32 use Uplug::IO::Any;
33 use Uplug::Data;
34 # use IO::File;
35 # use POSIX qw(tmpnam);
36  
37 @ISA = qw( Uplug::IO::XML );
38  
39 # stream options and their default values!!
40  
41 %StreamOptions = ('DocRootTag' => 'liuAlign',
42 'DocHeaderTag' => 'liuHeader',
43 'DocBodyTag' => 'linkList',
44 'root' => 'sentLink',
45 # 'encoding' => $Uplug::IO::DEFAULTENCODING,
46 'DataStructure' => 'complex',
47 'SkipDataHeader' => 1,
48 'DTDname' => 'liuAlign',
49 'DTDsystemID' => 'liu-align.dtd',
50 # 'DTDpublicID' => '...'
51 );
52 $StreamOptions{DocRoot}{version}='1.0';
53  
54 sub new{
55 my $class=shift;
56 my $self=$class->SUPER::new($class);
57 &Uplug::IO::AddHash2Hash($self->{StreamOptions},\%StreamOptions);
58 return $self;
59 }
60  
61 sub init{
62 my $self = shift;
63 my $options = $_[0];
64 if (ref $options){
65 $self->{DocRoot}->{fromDoc}=$options->{fromDoc};
66 $self->{DocRoot}->{toDoc}=$options->{toDoc};
67 }
68 return $self->SUPER::init($options);
69 }
70  
71 sub initFromDoc{
72 my $self=shift;
73  
74 if (defined $self->{StreamOptions}->{fromDoc}){
75 $self->{StreamOptions}->{DocRoot}->{fromDoc}=
76 $self->{StreamOptions}->{fromDoc};
77 }
78 elsif ((ref($self->{StreamHeader}->{DocRoot}) eq 'HASH') and
79 (defined $self->{StreamHeader}->{DocRoot}->{fromDoc})){
80 $self->{StreamOptions}->{DocRoot}->{fromDoc}=
81 $self->{StreamHeader}->{DocRoot}->{fromDoc};
82 }
83 elsif (not defined $self->{StreamOptions}->{DocRoot}->{fromDoc}){
84 if (defined $self->{StreamOptions}->{file}){
85 $self->{StreamOptions}->{DocRoot}->{fromDoc}=
86 $self->{StreamOptions}->{file}.'.src';
87 }
88 else{
89 $self->{StreamOptions}->{DocRoot}->{fromDoc}=
90 &Uplug::IO::Any::GetTempFileName.'.src';
91 }
92 }
93 $self->{StreamOptions}->{DocRoot}->{fromDoc}=
94 $self->FindDataFile($self->{StreamOptions}->{DocRoot}->{fromDoc});
95 }
96  
97  
98  
99 sub initToDoc{
100 my $self=shift;
101  
102 if (defined $self->{StreamOptions}->{toDoc}){
103 $self->{StreamOptions}->{DocRoot}->{toDoc}=
104 $self->{StreamOptions}->{toDoc};
105 }
106 elsif ((ref($self->{StreamHeader}->{DocRoot}) eq 'HASH') and
107 (defined $self->{StreamHeader}->{DocRoot}->{toDoc})){
108 $self->{StreamOptions}->{DocRoot}->{toDoc}=
109 $self->{StreamHeader}->{DocRoot}->{toDoc};
110 }
111 elsif (not defined $self->{StreamOptions}->{DocRoot}->{toDoc}){
112 if (defined $self->{StreamOptions}->{file}){
113 $self->{StreamOptions}->{DocRoot}->{toDoc}=
114 $self->{StreamOptions}->{file}.'.trg';
115 }
116 else{
117 $self->{StreamOptions}->{DocRoot}->{toDoc}=
118 &Uplug::IO::Any::GetTempFileName.'.trg';
119 }
120 }
121 $self->{StreamOptions}->{DocRoot}->{toDoc}=
122 $self->FindDataFile($self->{StreamOptions}->{DocRoot}->{toDoc});
123 }
124  
125 sub init{
126 my $self=shift;
127 my $ret=$self->SUPER::init(@_);
128 $self->initFromDoc;
129 $self->initToDoc;
130 return $ret;
131 }
132  
133 sub open{
134 my $self = shift;
135 $self->{AccessMode} = shift;
136 my $OptionHash = $_[0];
137  
138 &Uplug::IO::AddHash2Hash($self->{StreamOptions},\%StreamOptions);
139 my $ret=$self->SUPER::open($self->{AccessMode},$OptionHash);
140 $self->initFromDoc;
141 $self->initToDoc;
142  
143  
144 if (($self->{AccessMode} ne 'read') and
145 (-s $self->{StreamOptions}->{DocRoot}->{fromDoc})){
146 $self->{fromDoc}='exists';
147 }
148 else{
149 my %SrcStream=('file' => $self->{StreamOptions}->{DocRoot}->{fromDoc},
150 'format' => 'XML',
151 # 'DocRootTag' => 'cesDoc',
152 'root' => 's');
153 if ($self->{AccessMode} ne 'read'){$SrcStream{DocRootTag}='document';}
154 $self->{source}=Uplug::IO::Any->new(\%SrcStream);
155 my $r;
156 if (not $r=$self->{source}->open($self->{AccessMode},\%SrcStream)){
157 $self->{fromDoc}='exists';
158 }
159 }
160  
161  
162 if (($self->{AccessMode} ne 'read') and
163 (-s $self->{StreamOptions}->{DocRoot}->{toDoc})){
164 $self->{toDoc}='exists';
165 }
166 else{
167 my %TrgStream=('file' => $self->{StreamOptions}->{DocRoot}->{toDoc},
168 'format' => 'XML',
169 # 'DocRootTag' => 'cesDoc',
170 'root' => 's');
171 if ($self->{AccessMode} ne 'read'){$TrgStream{DocRootTag}='document';}
172 $self->{target}=Uplug::IO::Any->new(\%TrgStream);
173 my $r;
174 if (not $r=$self->{target}->open($self->{AccessMode},\%TrgStream)){
175 $self->{toDoc}='exists';
176 }
177 }
178 # if ($self->{AccessMode} eq 'read'){
179 # if ($self->{toDoc} eq 'exists'){return 0;}
180 # if ($self->{fromDoc} eq 'exists'){return 0;}
181 # }
182 return $ret;
183 }
184  
185 sub close{
186 my $self=shift;
187 $self->SUPER::close();
188 if (defined $self->{source}){
189 $self->{source}->close;
190 }
191 if (defined $self->{target}){
192 $self->{target}->close;
193 }
194 }
195  
196 sub write{
197 my $self=shift;
198 my ($data)=@_;
199  
200 $self->Uplug::IO::write($data);
201  
202 my $ret;
203 my $source=Uplug::Data->new;
204 $data->subData($source,'source');
205 my $target=Uplug::Data->new;
206 $data->subData($target,'target');
207 if (ref($data->{link})){
208 my $rootTag=$self->option('root'); # should be this
209 my $linkTag=$data->{link}->getRootNode->getNodeName(); # but is this
210 if (($rootTag ne $linkTag) and # different tags
211 ($data->{link}->getRootNode->getNodeType==1)){ # (element node)
212 $data->{link}->getRootNode->setTagName($rootTag); # change name!!
213 }
214 $ret=$self->SUPER::write($data->{link});
215 }
216 elsif (defined $data->attribute('xtargets')){
217 $ret=$self->SUPER::write($data);
218 }
219 else{
220 my $tag=$self->option('root');
221 my $link=Uplug::Data->new($tag);
222 my @src=$source->findNodes('s');
223 my @srcID=$source->attribute(\@src,'id');
224 foreach (0..$#src){
225 if (not defined $srcID[$_]){
226 $self->{srcID}++;
227 $srcID[$_]=$self->{srcID};
228 $source->setAttribute($src[$_],'id',$srcID[$_]);
229 }
230 $self->{srcID}=$srcID[$_];
231 }
232  
233 my @trg=$target->findNodes('s');
234 my @trgID=$target->attribute(\@trg,'id');
235 foreach (0..$#trg){
236 if (not defined $trgID[$_]){
237 $self->{trgID}++;
238 $trgID[$_]=$self->{trgID};
239 $target->setAttribute($trg[$_],'id',$trgID[$_]);
240 }
241 $self->{trgID}=$trgID[$_];
242 }
243 my $s=join('+',@srcID);
244 my $t=join('+',@trgID);
245 $link->setAttribute('xtargets',"$s\;$t",$tag);
246  
247 my $id=$data->attribute('id');
248 if (not defined $id){
249 $self->{alignID}++;
250 $id=$self->{alignID};
251 }
252 $link->setAttribute('id',$id);
253 $ret=$self->SUPER::write($link);
254 }
255 if ($self->{fromDoc} ne 'exists'){
256 # $source->moveAttribute('s','seg');
257 $self->{source}->write($source);
258 }
259 if ($self->{toDoc} ne 'exists'){
260 # $target->moveAttribute('s','seg');
261 $self->{target}->write($target);
262 }
263 return $ret;
264 }
265  
266 sub read{
267 my $self=shift;
268 my ($data)=@_;
269  
270 $data->init;
271 $data->addChild('align');
272 $data->{link}=Uplug::Data->new;
273 my $ret=$self->SUPER::read($data->{link});
274 my ($src,$trg);
275  
276 my $xtargets=$data->{link}->attribute('xtargets');
277 if (defined $xtargets){
278 ($src,$trg)=split(/\;/,$xtargets);
279 }
280 else{return 0;}
281  
282  
283 my @srcID=split(/[\+\s]/,$src);
284 my @trgID=split(/[\+\s]/,$trg);
285  
286 my $elem=0;
287 if (ref($data->{link}->{content}) eq 'ARRAY'){
288 $elem=@{$data->{link}->{content}};
289 }
290  
291 #----------- sentences ------------------------------
292  
293 &ReadSegments($self->{source},$data,\@srcID,'source');
294 &ReadSegments($self->{target},$data,\@trgID,'target');
295  
296 $self->Uplug::IO::read($data);
297  
298 return $ret;
299  
300 }
301  
302  
303 sub ReadSegments{
304  
305 my ($stream,$data,$IDs,$lang)=@_;
306  
307 if (not @{$IDs}){return;}
308 my $i=0;
309 my $parent=$data->addChild($lang);
310 # my $dom=$data->getDOM();
311 my $sent=Uplug::Data->new();
312  
313 my $fail=0;
314 while ($stream->read($sent)){
315 my $id=$sent->attribute('id');
316 # if (not grep(@{$IDs},$id)){
317 if (not grep ($_ eq $id,@{$IDs})){
318 if ($id<$IDs->[0]){next;}
319 else{
320 $fail++;
321 if ($fail>1){
322 last;
323 } # fail only once!!!
324 # $stream->close; # takes too much time!
325 # $stream->open('read'); # ... ignore it for now ...
326 next;
327 }
328 }
329 my $node=$sent->getRootNode();
330 # $sent->{dom}->removeChild($node);
331 # $node->setOwnerDocument($dom);
332 $data->addChild($node,$parent);
333 if ($id eq $IDs->[-1]){last;}
334 $i++;
335 }
336 $sent->dispose();
337 # $sent->{dom}->dispose();
338 }
339  
340  
341  
342  
343  
344  
345  
346  
347  
348 #sub readheader{
349 # my $self = shift;
350 # $self->SUPER::readheader(@_);
351 #}
352  
353 #sub writeheader{
354 # my $self=shift;
355 # $self->SUPER::writeheader(@_);
356 #}
357  
358 sub ReadSegmentsOld{
359  
360 my ($stream,$data,$IDs,$lang)=@_;
361  
362 if (not @{$IDs}){return;}
363 my $i=0;
364 my $parent=$data->addChild($lang);
365 my $dom=$data->getDOM();
366 my $sent=Uplug::Data->new();
367  
368 my $fail=0;
369 while ($stream->read($sent)){
370 my $id=$sent->attribute('id');
371 # if (not grep(@{$IDs},$id)){
372 if (not grep ($_ eq $id,@{$IDs})){
373 if ($id<$IDs->[0]){next;}
374 else{
375 $fail++;
376 if ($fail>1){
377 last;
378 } # fail only once!!!
379 # $stream->close; # takes too much time
380 # $stream->open('read'); # ... should do something here ...
381 next;
382 }
383 }
384 my $node=$sent->getRootNode();
385 $sent->{dom}->removeChild($node);
386 $node->setOwnerDocument($dom);
387 $data->addChild($node,$parent);
388 if ($id eq $IDs->[-1]){last;}
389 $i++;
390 }
391 $sent->{dom}->dispose();
392 }
393  
394 #sub GetTempFileName{
395 # my $fh;
396 # my $file;
397 # do {$file=tmpnam();}
398 # until ($fh=IO::File->new($file,O_RDWR|O_CREAT|O_EXCL));
399 # $fh->close;
400 # return $file;
401 #}
402  
403  
404  
405  
406 sub files{
407 my $self=shift;
408 my @files=$self->SUPER::files;
409 my $file=$self->{StreamOptions}->{file};
410 my $from=$self->{StreamOptions}->{DocRoot}->{fromDoc};
411 my $to=$self->{StreamOptions}->{DocRoot}->{toDoc};
412 if ($from eq $file.'.src'){
413 push(@files,$from);
414 }
415 if ($to eq $file.'.src'){
416 push(@files,$to);
417 }
418 return @files;
419 }
420  
421 sub delete{
422 my $self=shift;
423 return $self->SUPER::delete;
424 my $file=$self->{StreamOptions}->{file};
425 my $from=$self->{StreamOptions}->{DocRoot}->{fromDoc};
426 my $to=$self->{StreamOptions}->{DocRoot}->{toDoc};
427 if ($from eq $file.'.src'){
428 if (-e $from){
429 print STDERR "# LiuStream.pm: remove file $from!\n";
430 unlink $from;
431 }
432 }
433 if ($to eq $file.'.trg'){
434 if (-e $to){
435 print STDERR "# LiuStream.pm: remove file $to!\n";
436 unlink $to;
437 }
438 }
439 }
440  
441 sub FindDataFile{
442 my $self=shift;
443 my ($file)=@_;
444  
445 if (-e $file){
446 return $file;
447 }
448  
449 #-------------------------------------
450 # if AccessMode is 'read' and the file is not found:
451 # check if the file
452 # 1) is in the same dir as the alignment file
453 # 2) exists relativ to the alignment file directory
454  
455 if ($self->{AccessMode} eq 'read'){
456 my $AlignFile=$self->{StreamOptions}->{file};
457 my $AlignDir='';
458 if ($AlignFile=~/^(.*[\\\/])([^\\\/]+)$/){
459 $AlignDir=$1;
460 $AlignFile=$2;
461 }
462 my $dir='';
463 if ($file=~/^(.*[\\\/])([^\\\/]+)$/){
464 $dir=$1;
465 $file=$2;
466 }
467 if (-e "$AlignDir$file"){
468 return "$AlignDir$file";
469 }
470 if (-e "$AlignDir$dir$file"){
471 return "$AlignDir$dir$file";
472 }
473 if (not -e $file.'gz'){
474 return $file.'.gz';
475 }
476 }
477 return $file;
478 }
479