AlignmentSet.pm

filter: Statementstmt 23.6% 246 / 1041 Branchbran 18.2% 107 / 586 Conditioncond 15.0% 24 / 159 MC/DCmcdc 5.4% 5 / 92 Subroutinesub 55.5% 20 / 36 Podpod 0.0% 0 / 27 totaltotal 20.7% 402 / 1941 CC 374 SCAR 69.7
CC
374
Cov
21%
symmetrize
3660.0
adaptToBilCorpus
2652.0
evaluate
587.9
CRAP 1065.8
« AlignmentSlice.pm ↑ Summary Alignment.pm »
1 ########################################################################
2 # Author: Patrik Lambert (lambert@talp.ucp.es)
3 # Description: Tools library to manage an Alignment Sets, i.e. a set of
4 # sentences aligned at the word (or phrase) level.
5 #-----------------------------------------------------------------------
6 # Copyright 2004 by Patrik Lambert
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 ########################################################################
22  
23 package Lingua::AlignmentSet;
24 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 $VERSION = 1.1;
28 1 ▶use Lingua::AlSetLib 1.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Lingua::Alignment 1.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Lingua::WriteLatexFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Lingua::AlignmentEval;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use Dumpvalue;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 my $dumper=new Dumpvalue;
37 1 my $true = 1;
38 1 my $false = 0;
39  
40 2 ▶sub new {
Subroutine new: called CC=8 SCAR=21.6
Pod undocumented
41 2 my ($pkg,$refToFileSets) = @_;
42 2 my $refToLocation = readLocation($refToFileSets->[0][0]);
43 2 my $format = $refToFileSets->[0][1];
44 2 my $range = $refToFileSets->[0][2];
45 2 my $alSet = {};
46 #default values:
47 0 ▶ if (!defined($format)){$format="TALP"}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $format) { } 0 2 2
48 2 else {$format = uc $format};
49 2 ▶ if (!defined($range)){$range="1-"};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $range) 1 1 2
50 2 ▶ if ($format eq "BLINKER"){
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "BLINKER") 1 1 2
51 #for future ease we save detailed infos contained in the source sample path
52 1 completeBlinkerLocation($refToLocation);
53 }
54
55 2 $alSet->{location}=$refToLocation;
56 2 $alSet->{format}=$format;
57
58 2 setRange($alSet,$range);
59
60 #checking the data:
61 2 ▶ if ($format eq "GIZA"){
Branch: 4 branches
BranchTrueFalseTotal
if ($format eq 'GIZA') { } 0 2 2
elsif ($format eq 'BLINKER') { } 1 1 2
elsif ($format eq 'NAACL') { } 1 0 1
elsif ($format eq 'TALP') { } 0 0 0
62 # if ($ambiguity || $confidence){die "GIZA format not compatible with ambiguity or confidence features"}
63 } elsif ($format eq "BLINKER"){
64 } elsif ($format eq "NAACL"){
65 } elsif ($format eq "TALP"){
66 } else {
67 0 die "Unknown format $format. Can't create alignment set object";
68 }
69 2 return bless $alSet,$pkg;
70 }
71  
72 # create a new AlignmentSet that contains the same data of an already existing alignment set (without copying the addresses)
73 0 ▶sub copy {
Subroutine copy: not called CC=2 SCAR=17.9
Pod undocumented
74 0 my $alSet = shift;
75  
76 0 my $cloneLocation={};
77 0 my ($field,$value);
78 0 ▶ while (($field,$value)=each (%{$alSet->{location}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$alSet{"location"};}) 0 0 0
79 0 $cloneLocation->{$field}=$value;
80 }
81 0 return Lingua::AlignmentSet->new([[$cloneLocation,$alSet->{format},$alSet->{firstSentPair}."-".$alSet->{lastSentPair}]]);
82 }
83  
84 1 ▶sub setWordFiles{
Subroutine setWordFiles: called CC=1 SCAR=0.0
Pod undocumented
85 1 my ($alSet,$sourcePath,$targetPath) = @_;
86
87 1 $alSet->{location}->{source}=$sourcePath;
88 1 $alSet->{location}->{target}=$targetPath;
89 }
90 0 ▶sub setSourceFile{
Subroutine setSourceFile: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my ($alSet,$sourcePath) = @_;
92
93 0 $alSet->{location}->{source}=$sourcePath;
94 }
95 0 ▶sub setTargetFile{
Subroutine setTargetFile: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my ($alSet,$targetPath) = @_;
97
98 0 $alSet->{location}->{target}=$targetPath;
99 }
100  
101 0 ▶sub setTargetToSourceFile{
Subroutine setTargetToSourceFile: not called CC=1 SCAR=6.9
Pod undocumented
102 0 my ($alSet,$targetToSourcePath) = @_;
103
104 0 $alSet->{location}->{targetToSource}=$targetToSourcePath;
105 }
106  
107 0 ▶sub chFormat {
Subroutine chFormat: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my ($alSet,$newLocation,$newFormat,$alignMode)=@_;
109  
110 0 $alSet->convert($newLocation,$newFormat,$alignMode);
111 }
112  
113  
114 # Won't work if the sentence files are not specified
115 0 ▶sub visualise {
Subroutine visualise: not called CC=15 SCAR=54.8
Pod undocumented
116 0 my ($alSet,$representation,$format,$outputFH,$mark,$alignMode,$maxRows,$maxCols)=@_;
117 0 $representation = lc $representation;
118 0 $format = lc $format;
119 0 ▶ if (!defined($outputFH)){$outputFH=*STDOUT}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $outputFH) 0 0 0
120 0 ▶ if ($representation eq "matrix"){
Branch: 1 branch
BranchTrueFalseTotal
if ($representation eq "matrix") 0 0 0
121 0 ▶ if (!defined($mark)){$mark = "cross"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mark) 0 0 0
122 0 ▶ if (!defined($maxRows)){$maxRows = 53} #default maxRows value
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $maxRows) 0 0 0
123 0 ▶ if (!defined($maxCols)){$maxCols = 35} #default maxRows value
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $maxCols) 0 0 0
124 0 $format="latex";
125 }
126 0 my $latex = Lingua::Latex->new;
127 0 ▶ if ($format eq "latex"){
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "latex") 0 0 0
128 0 print $outputFH $latex->startFile;
129 0 print $outputFH $latex->setTabcolsep("0.5mm");
130 }
131 0 my $output = "";
132 0 my $inputSentPairNum = $alSet->{firstSentPair};
133 0 my $i;
134 0 my ($al,$alSetChunk);
135 0 my $FH = $alSet->openFiles();
136 0 ▶ if (($alSet->{format} ne "GIZA") && (!$FH->{source} || !$FH->{target})){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{"format"} ne "GIZA" and not $FH->{"source"} && $FH->{"target"}) 0 0 0
Condition0%0/6
$alSet->{"format"} ne "GIZA" and not $FH->{"source"} && $FH->{"target"}
!ll&&!rl&&r
000
$FH->{'source'} && $FH->{'target'}
!ll&&!rl&&r
000
MC/DC0%0/3
$alSet->{"format"} ne "GIZA" and not $FH->{"source"} && $FH->{"target"}
$alSet->{"format"} ne "GIZA" $FH->{'source'} $FH->{'target'}
Truth table0%0/4
A and not(B && C)
A= $alSet->{"format"} ne "GIZA"B= $FH->{'source'}C= $FH->{'target'}
ABCresult
0XX0
1110
10X1
1101
137 0 die "To use the 'visualise' function, you must specify the sentence (words) files.\n";
138 }
139 0 ▶ while ($alSetChunk = $alSet->loadChunk($FH,$inputSentPairNum,$alignMode)){ # returns 0 if eof or last sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($alSetChunk = $alSet->loadChunk($FH, $inputSentPairNum, $alignMode)) 0 0 0
140 0 $output = "";
141 0 ▶ for ($i=0;$i<@$alSetChunk;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$alSetChunk) 0 0 0
142 0 $al = $$alSetChunk[$i];
143 # print main::Dumper($al);
144 0 ▶ if ($representation eq "matrix"){
Branch: 2 branches
BranchTrueFalseTotal
if ($representation eq 'matrix') { } 0 0 0
elsif ($representation eq 'enumlinks') { } 0 0 0
145 0 $output.= "\n$inputSentPairNum\n".$al->displayAsMatrix($latex,$mark,$maxRows,$maxCols);
146 }elsif ($representation eq "enumlinks"){
147 0 $output.= "\n$inputSentPairNum\n".$al->displayAsLinkEnumeration($format,$latex);
148 } #elsif
149 }#for
150 0 print $outputFH $output;
151 0 $inputSentPairNum++;
152 }
153 0 ▶ if ($format eq "latex"){print $outputFH $latex->endFile};
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "latex") 0 0 0
154 }
155  
156 #only work if the text files are given (not only the alignment files).
157 0 ▶sub getSize {
Subroutine getSize: not called CC=7 SCAR=40.3
Pod undocumented
158 0 my $alSet = shift;
159 0 my ($file,$factor);
160 0 my $size;
161
162 0 ▶ if ($alSet->{format} eq "GIZA"){
Branch: 2 branches
BranchTrueFalseTotal
if ($alSet->{'format'} eq 'GIZA') { } 0 0 0
elsif ($alSet->{'format'} eq 'NAACL' or $alSet->{'format'} eq 'BLINKER' or $alSet->{'format'} eq 'TALP') { } 0 0 0
163 0 $file = $alSet->{location}->{sourceToTarget};
164 0 $factor = 3;
165 ▶ }elsif ($alSet->{format} eq "NAACL" || $alSet->{format} eq "BLINKER" || $alSet->{format} eq "TALP"){
Condition0%0/6
$alSet->{"format"} eq "NAACL" or $alSet->{"format"} eq "BLINKER" or $alSet->{"format"} eq "TALP"
l!l&&r!l&&!r
000
$alSet->{"format"} eq "NAACL" or $alSet->{"format"} eq "BLINKER"
l!l&&r!l&&!r
000
MC/DC0%0/3
$alSet->{"format"} eq "NAACL" or $alSet->{"format"} eq "BLINKER" or $alSet->{"format"} eq "TALP"
$alSet->{"format"} eq "NAACL" $alSet->{"format"} eq "BLINKER" $alSet->{"format"} eq "TALP"
Truth table0%0/4
A or B or C
A= $alSet->{"format"} eq "NAACL"B= $alSet->{"format"} eq "BLINKER"C= $alSet->{"format"} eq "TALP"
ABCresult
1XX1
01X1
0011
0000
166 0 ▶ if (!$alSet->{location}->{source}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($alSet->{"location"}{"source"}) 0 0 0
167 0 die "One of the functions your are using requires you specify the sentence files (source and target)\n";
168 }
169 0 $file = $alSet->{location}->{source};
170 0 $factor = 1;
171 }
172 0 open (FILE,"<$file");
173 0 ▶ $size += tr/\n/\n/ while sysread(FILE, $_, 2 ** 16);
Branch: 1 branch
BranchTrueFalseTotal
sysread FILE, $_, 65536 and do { $size += tr/\n// } 0 0 0
174 0 close(FILE);
175 0 $size = $size / $factor;
176 0 return $size;
177 }
178  
179 # returns a list (in random order) of lineNumbers
180 # to sort this list, do: my @sortedSelection = sort { $a <=> $b; } @selection;
181  
182 0 ▶sub chooseSubsets {
Subroutine chooseSubsets: not called CC=3 SCAR=24.8
Pod undocumented
183 #TO DO: possibility of percentage input for the size
184 0 my ($alSet,$seed,$size) = @_;
185 0 my $alSetSize = $alSet->getSize();
186 0 my $count;
187 0 my @selected=();
188 0 my @notSelected = ();
189 0 my ($ind,$elt);
190  
191 0 ▶ for ($count=1;$count<$alSetSize;$count++){
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $alSetSize) 0 0 0
192 0 push @notSelected,$count;
193 }
194 0 srand $seed;
195 0 ▶ for ($count=0;$count<$size;$count++){
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $size) 0 0 0
196 0 $ind = rand @notSelected;
197 0 $elt = $notSelected[$ind];
198 0 splice @notSelected, $ind, 1;
199 0 push @selected,$elt;
200 }
201 0 return \@selected;
202 }
203 ###################################################################
204 ### EVALUATION ###
205 ###################################################################
206  
207 #code adapted from Rada Mihalcea's wa_eval_align.pl, rada@cs.unt.edu
208 # Evaluation is performed using:
209 # - Standard Precision, Recall, F-measure, separate for S (Sure) and P (Possible) cases
210 # - AER measure, defined as
211 # AER = 1 - ( |A & S| + |A & P| ) / ( |A| + |S| )
212 # [where A represents the alignment, S and P represent the S (Sure) and P (Possible) gold standard alignments]
213  
214 1 ▶sub evaluate {
Subroutine evaluate: called CC=61 SCAR=63.8
Pod undocumented
215 1 my ($submissionAlSet,$answerAlSet,$alignMode,$weighted)=@_;
216 1 ▶ if (!defined($weighted)){$weighted=0}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $weighted) 1 0 1
217 1 my ($line,$alignment);
218 1 my ($FH,$alSetChunk,$i,$al,$fhPos);
219 1 my ($inputSentPairNum,$internalSentPairNum,$sentPairNum);
220 1 my ($sureMatch,$possibleMatch,$possibleMatchSure);
221 1 my ($surePrecision,$sureRecall,$possiblePrecision,$possibleRecall,$sureFMeasure,$possibleFMeasure,$AER);
222 # 1 READ ANSWER AND SUBMISSION FILES
223 # (in the case of NAACL format file it's more efficient to treat it directly, otherwise load to internal structure)
224 # answer file
225 1 my %sureAnswer;
226 1 my %possibleAnswer;
227 1 my $INFINITY = 9999999999;
228 1 $inputSentPairNum = $answerAlSet->{firstSentPair};
229 1 $internalSentPairNum = 1;
230 1 ▶ if ( $answerAlSet->{format} eq "NAACL" && $alignMode eq "as-is" && $answerAlSet->{firstSentPair} == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($answerAlSet->{'format'} eq 'NAACL' and $alignMode eq 'as-is' and $answerAlSet->{'firstSentPair'} == 1) { } 0 1 1
Condition33%2/6
$answerAlSet->{"format"} eq "NAACL" and $alignMode eq "as-is" and $answerAlSet->{"firstSentPair"} == 1
!ll&&!rl&&r
100
$answerAlSet->{"format"} eq "NAACL" and $alignMode eq "as-is"
!ll&&!rl&&r
100
MC/DC0%0/3
$answerAlSet->{"format"} eq "NAACL" and $alignMode eq "as-is" and $answerAlSet->{"firstSentPair"} == 1
$answerAlSet->{"format"} eq "NAACL" $alignMode eq "as-is" $answerAlSet->{"firstSentPair"} == 1
Truth table25%1/4
A and B and C
A= $answerAlSet->{"format"} eq "NAACL"B= $alignMode eq "as-is"C= $answerAlSet->{"firstSentPair"} == 1
ABCresult
0XX0
10X0
1100
1111
231 0 ▶ my $answerFH = IO::File->new("<".$answerAlSet->{location}{sourceToTarget}) or die "Answer alignment file opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
my $answerFH = "IO::File"->new('<' . $answerAlSet->{"location"}{"sourceToTarget"}) or die "Answer alignment file opening error:$!" 0 0 0
232 #go to first sentence pair:
233 0 $fhPos = $answerFH->getpos;
234 0 ▶ while ($answerFH->getline() !~ m/^0*$inputSentPairNum .*/o && !$answerFH->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($answerFH->getline =~ /^0*$inputSentPairNum .*/o or $answerFH->eof) 0 0 0
Condition0%0/3
$answerFH->getline =~ /^0*$inputSentPairNum .*/o or $answerFH->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$answerFH->getline =~ /^0*$inputSentPairNum .*/o or $answerFH->eof
$answerFH->getline =~ /^0*$inputSentPairNum .*/o $answerFH->eof
Truth table0%0/3
A or B
A= $answerFH->getline =~ /^0*$inputSentPairNum .*/oB= $answerFH->eof
ABresult
1X1
011
000
235 0 $fhPos = $answerFH->getpos;
236 }
237 0 ▶ if ($answerFH->eof()){
Branch: 1 branch
BranchTrueFalseTotal
if ($answerFH->eof) 0 0 0
238 0 die "First sentence pair of range not found in ".$answerAlSet->{location}{sourceToTarget};
239 }
240 0 $answerFH->setpos($fhPos); #if we changed sentences, we read the first line of next sentence=>go back one line
241
242 #read file:
243 0 ▶ if ($answerAlSet->{lastSentPair} eq "eof"){
Branch: 1 branch
BranchTrueFalseTotal
if ($answerAlSet->{'lastSentPair'} eq 'eof') { } 0 0 0
244 0 $inputSentPairNum = $INFINITY;
245 }else{
246 0 $inputSentPairNum = $answerAlSet->{lastSentPair}+1;
247 }
248 0 ▶ while(!$answerFH->eof() && ( ($line=$answerFH->getline()) !~ m/^0*$inputSentPairNum .*/o )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($answerFH->eof or ($line = $answerFH->getline) =~ /^0*$inputSentPairNum .*/o) 0 0 0
Condition0%0/3
$answerFH->eof or ($line = $answerFH->getline) =~ /^0*$inputSentPairNum .*/o
l!l&&r!l&&!r
000
MC/DC0%0/2
$answerFH->eof or ($line = $answerFH->getline) =~ /^0*$inputSentPairNum .*/o
$answerFH->eof ($line = $answerFH->getline) =~ /^0*$inputSentPairNum .*/o
Truth table0%0/3
A or B
A= $answerFH->eofB= ($line = $answerFH->getline) =~ /^0*$inputSentPairNum .*/o
ABresult
1X1
011
000
249 0 chomp $line;
250 0 $line =~ s/^\s+|\s+$//g;
251 0 identifySurePossible($line,\%sureAnswer,\%possibleAnswer);
252 }
253 0 $answerFH->close();
254 }else{
255 1 $FH = $answerAlSet->openFiles();
256 1 ▶ while ($alSetChunk = $answerAlSet->loadChunk($FH,$inputSentPairNum,$alignMode)){ # returns 0 if eof or last sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($alSetChunk = $answerAlSet->loadChunk($FH, $inputSentPairNum, $alignMode)) 10 1 11
257 10 ▶ for ($i=0;$i<@$alSetChunk;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$alSetChunk) 10 10 20
258 10 $al = $$alSetChunk[$i];
259 # print "EVALUATE: answer al:\n";
260 # print main::Dumper($al);
261 10 foreach $line (@{$al->writeToBlinker()}){
262 90 $line = "$internalSentPairNum ".$line;
263 90 identifySurePossible($line,\%sureAnswer,\%possibleAnswer);
264 }
265 }
266 10 $inputSentPairNum++;
267 10 $internalSentPairNum++;
268 }
269 1 closeFiles($FH,$answerAlSet->{format});
270 }# if format - else
271
272 # submission file
273 1 my %sureSubmission;
274 my %possibleSubmission;
275 1 $inputSentPairNum = $submissionAlSet->{firstSentPair};
276 1 $internalSentPairNum = 1;
277  
278 1 ▶ if ($submissionAlSet->{format} eq "NAACL" && $alignMode eq "as-is" && $submissionAlSet->{firstSentPair}==1){
Branch: 1 branch
BranchTrueFalseTotal
if ($submissionAlSet->{'format'} eq 'NAACL' and $alignMode eq 'as-is' and $submissionAlSet->{'firstSentPair'} == 1) { } 0 1 1
Condition33%2/6
$submissionAlSet->{"format"} eq "NAACL" and $alignMode eq "as-is" and $submissionAlSet->{"firstSentPair"} == 1
!ll&&!rl&&r
100
$submissionAlSet->{"format"} eq "NAACL" and $alignMode eq "as-is"
!ll&&!rl&&r
010
MC/DC0%0/3
$submissionAlSet->{"format"} eq "NAACL" and $alignMode eq "as-is" and $submissionAlSet->{"firstSentPair"} == 1
$submissionAlSet->{"format"} eq "NAACL" $alignMode eq "as-is" $submissionAlSet->{"firstSentPair"} == 1
Truth table25%1/4
A and B and C
A= $submissionAlSet->{"format"} eq "NAACL"B= $alignMode eq "as-is"C= $submissionAlSet->{"firstSentPair"} == 1
ABCresult
0XX0
10X0
1100
1111
279 0 ▶ my $submissionFH = IO::File->new("<".$submissionAlSet->{location}{sourceToTarget}) or die "Submission alignment file opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
my $submissionFH = "IO::File"->new('<' . $submissionAlSet->{"location"}{"sourceToTarget"}) or die "Submission alignment file opening error:$!" 0 0 0
280  
281 #go to first sentence pair:
282 0 $fhPos = $submissionFH->getpos;
283 0 ▶ while ($submissionFH->getline() !~ m/^0*$inputSentPairNum .*/o && !$submissionFH->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($submissionFH->getline =~ /^0*$inputSentPairNum .*/o or $submissionFH->eof) 0 0 0
Condition0%0/3
$submissionFH->getline =~ /^0*$inputSentPairNum .*/o or $submissionFH->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$submissionFH->getline =~ /^0*$inputSentPairNum .*/o or $submissionFH->eof
$submissionFH->getline =~ /^0*$inputSentPairNum .*/o $submissionFH->eof
Truth table0%0/3
A or B
A= $submissionFH->getline =~ /^0*$inputSentPairNum .*/oB= $submissionFH->eof
ABresult
1X1
011
000
284 0 $fhPos = $submissionFH->getpos;
285 }
286 0 ▶ if ($submissionFH->eof()){
Branch: 1 branch
BranchTrueFalseTotal
if ($submissionFH->eof) 0 0 0
287 0 die "First sentence pair of range not found in ".$submissionAlSet->{location}{sourceToTarget};
288 }
289 0 $submissionFH->setpos($fhPos); #if we changed sentences, we read the first line of next sentence=>go back one line
290
291 #read file:
292 0 ▶ if ($submissionAlSet->{lastSentPair} eq "eof"){
Branch: 1 branch
BranchTrueFalseTotal
if ($submissionAlSet->{'lastSentPair'} eq 'eof') { } 0 0 0
293 0 $inputSentPairNum = $INFINITY;
294 }else{
295 0 $inputSentPairNum = $submissionAlSet->{lastSentPair}+1;
296 }
297 0 ▶ while(!$submissionFH->eof() && (($line = $submissionFH->getline()) !~ m/^0*$inputSentPairNum .*/o )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($submissionFH->eof or ($line = $submissionFH->getline) =~ /^0*$inputSentPairNum .*/o) 0 0 0
Condition0%0/3
$submissionFH->eof or ($line = $submissionFH->getline) =~ /^0*$inputSentPairNum .*/o
l!l&&r!l&&!r
000
MC/DC0%0/2
$submissionFH->eof or ($line = $submissionFH->getline) =~ /^0*$inputSentPairNum .*/o
$submissionFH->eof ($line = $submissionFH->getline) =~ /^0*$inputSentPairNum .*/o
Truth table0%0/3
A or B
A= $submissionFH->eofB= ($line = $submissionFH->getline) =~ /^0*$inputSentPairNum .*/o
ABresult
1X1
011
000
298 0 chomp $line;
299 0 $line =~ s/^\s+|\s+$//g;
300 0 identifySurePossible($line,\%sureSubmission,\%possibleSubmission);
301 }
302 0 $submissionFH->close();
303 }else{
304 1 $FH = $submissionAlSet->openFiles();
305 1 ▶ while ($alSetChunk = $submissionAlSet->loadChunk($FH,$inputSentPairNum,$alignMode)){ # returns 0 if eof or last sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($alSetChunk = $submissionAlSet->loadChunk($FH, $inputSentPairNum, $alignMode)) 10 1 11
306 10 ▶ for ($i=0;$i<@$alSetChunk;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$alSetChunk) 10 10 20
307 10 $al = $$alSetChunk[$i];
308 # print "submission al:\n";
309 # $dumper->dumpValue($al);
310 10 foreach $line (@{$al->writeToBlinker()}){
311 59 $line = "$internalSentPairNum ".$line;
312 59 identifySurePossible($line,\%sureSubmission,\%possibleSubmission);
313 }
314 }
315 10 $inputSentPairNum++;
316 10 $internalSentPairNum++;
317 }
318 1 closeFiles($FH,$submissionAlSet->{format});
319 }# if format=NAACL else
320
321
322 # print "weighted:$weighted\n";
323 # print "SA:".join("-",keys %sureAnswer),"\nSS:".join(" - ",keys %sureSubmission),"\nPA:".join(" - ",keys %possibleAnswer),"\nPS:".join(" - ",keys %possibleSubmission)."\n";
324  
325 # 2 WEIGHT LINKS
326 # It is a kind of "normalization" of multiple links: each link (j i) is weighted according to
327 # the number of links in which j and i are involved: weight(j,i)=0.5*(1/numLinks(j)+1/numLinks(i)).
328  
329 1 my ($link,$j,$hash,$value);
330 1 my %weightsSure;
331 1 my %linksSure;
332 1 my @linksSureInSentence;
333 1 my %linksPossible;
334 1 my @linksPossibleInSentence;
335 1 my %weightsPossible;
336  
337 1 ▶ if ($weighted){
Branch: 1 branch
BranchTrueFalseTotal
if ($weighted) 0 1 1
338 # When only sure links are considered (calculation of Ps and Rs), they are weighted with respect to the union of both sure sets
339 # take union
340 0 foreach $hash ( \%sureSubmission, \%sureAnswer ) {
341 0 ▶ while (($link, $value) = each %$hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (($link, $value) = each %$hash) 0 0 0
342 0 ($sentPairNum,$j,$i)=split(" ",$link);
343 0 $linksSure{$sentPairNum}{"$j $i"} = $value;
344 }
345 }
346 # calculate weight of each link
347 0 foreach $sentPairNum (keys %linksSure){
348 0 @linksSureInSentence =keys %{$linksSure{$sentPairNum}};
349 0 foreach $link (@linksSureInSentence){
350 0 ($j,$i)=split(" ",$link);
351 0 $weightsSure{"$sentPairNum $link"}=0.5*( 1/grep(/^$j /,@linksSureInSentence)+1/grep(/ $i$/,@linksSureInSentence) );
352 }
353 }
354  
355 # When all links are considered (calculation of Pp and Rp, AER), possible AND sure links are weighted with respect to the union of all sets.
356 0 %linksPossible=%linksSure;
357 # add union of possible links
358 0 foreach $hash (\%possibleSubmission, \%possibleAnswer ) {
359 0 ▶ while (($link, $value) = each %$hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (($link, $value) = each %$hash) 0 0 0
360 0 ($sentPairNum,$j,$i)=split(" ",$link);
361 0 $linksPossible{$sentPairNum}{"$j $i"} = $value;
362 }
363 }
364 # calculate weight of each link
365 0 foreach $sentPairNum (keys %linksPossible){
366 0 @linksPossibleInSentence =keys %{$linksPossible{$sentPairNum}};
367 0 foreach $link (@linksPossibleInSentence){
368 0 ($j,$i)=split(" ",$link);
369 0 $weightsPossible{"$sentPairNum $link"}=0.5*( 1/grep(/^$j /,@linksPossibleInSentence)+1/grep(/ $i$/,@linksPossibleInSentence) );
370 }
371 }
372 }
373
374 # 3 SUM UP LINKS
375 # in case of weights distinct from 1: sum of %possibleAnswer and %possibleSubmission is always with %weightsPossible.
376 # however the sum of %sureAnswer and %sureSubmission is with %weightsSure to calculate Ps and Rs, %weightsPossible for Pp, Rp and AER.
377 1 my ($totalPossibleAnswer,$totalPossibleSubmission)=(0,0);
378 1 my ($totalSureAnswer_weightsSure,$totalSureSubmission_weightsSure,$totalSureAnswer_weightsPossible,$totalSureSubmission_weightsPossible)=(0,0,0,0);
379 1 ▶ if ($weighted){
Branch: 1 branch
BranchTrueFalseTotal
if ($weighted) { } 0 1 1
380 0 foreach $link (keys %sureAnswer){
381 0 $totalSureAnswer_weightsSure+=$weightsSure{$link};
382 0 $totalSureAnswer_weightsPossible+=$weightsPossible{$link};
383 }
384 0 foreach $link (keys %sureSubmission){
385 0 $totalSureSubmission_weightsSure+=$weightsSure{$link};
386 0 $totalSureSubmission_weightsPossible+=$weightsPossible{$link};
387 }
388 0 foreach $link (keys %possibleAnswer){
389 0 $totalPossibleAnswer+=$weightsPossible{$link};
390 }
391 0 foreach $link (keys %possibleSubmission){
392 0 $totalPossibleSubmission+=$weightsPossible{$link};
393 }
394 }else{ #every link has a weight 1
395 1 $totalSureAnswer_weightsSure=scalar(keys %sureAnswer);
396 1 $totalSureAnswer_weightsPossible= $totalSureAnswer_weightsSure;
397 1 $totalSureSubmission_weightsSure=scalar(keys %sureSubmission);
398 1 $totalSureSubmission_weightsPossible=$totalSureSubmission_weightsSure;
399 1 $totalPossibleAnswer=scalar(keys %possibleAnswer);
400 1 $totalPossibleSubmission=scalar(keys %possibleSubmission);
401 }
402
403 # 4 COUNT MATCHES
404  
405 # print "sureSubmission:",join("|",keys %sureSubmission),"\n";
406 # print "possibleSubmission:",join("|",keys %possibleSubmission),"\n";
407 # print "sureAnswer:",join("|",keys %sureAnswer),"\n";
408 # print "possibleAnswer:",join("|",keys %possibleAnswer),"\n";
409 # print "\n";
410 # now determine the S[ure] matches
411 1 $sureMatch = 0;
412 1 foreach $alignment (keys %sureSubmission) {
413 59 ▶ if(defined($sureAnswer{$alignment})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sureAnswer{$alignment}) 54 5 59
414 54 ▶ if (!$weighted){$sureMatch++}
Branch: 1 branch
BranchTrueFalseTotal
if (not $weighted) { } 54 0 54
415 0 else {$sureMatch += $weightsSure{$alignment}}
416 }
417 }
418 # and the [P]robable matches
419 # these are checked against both S[ure] and P[robable] correct alignments
420 1 $possibleMatch = 0;
421 1 foreach $alignment (keys %possibleSubmission, keys %sureSubmission) {
422 59 ▶ if(defined($sureAnswer{$alignment}) || defined($possibleAnswer{$alignment})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sureAnswer{$alignment} or defined $possibleAnswer{$alignment}) 54 5 59
Condition66%2/3
defined $sureAnswer{$alignment} or defined $possibleAnswer{$alignment}
l!l&&r!l&&!r
5405
MC/DC50%1/2
defined $sureAnswer{$alignment} or defined $possibleAnswer{$alignment}
defined $sureAnswer{$alignment} defined $possibleAnswer{$alignment}
Truth table66%2/3
A or B
A= defined $sureAnswer{$alignment}B= defined $possibleAnswer{$alignment}
ABresult
1X1
011
000
423 54 ▶ if (!$weighted){$possibleMatch++}
Branch: 1 branch
BranchTrueFalseTotal
if (not $weighted) { } 54 0 54
424 0 else{$possibleMatch += $weightsPossible{$alignment}}
425 }
426 }
427 # and also the intersection between all submitted alignments
428 # and the S [Sure] correct alignments -- as needed by AER
429 1 $possibleMatchSure = 0;
430 1 foreach $alignment (keys %possibleSubmission, keys %sureSubmission) {
431 59 ▶ if(defined($sureAnswer{$alignment})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sureAnswer{$alignment}) 54 5 59
432 54 ▶ if (!$weighted){$possibleMatchSure++}
Branch: 1 branch
BranchTrueFalseTotal
if (not $weighted) { } 54 0 54
433 0 else{$possibleMatchSure+= $weightsPossible{$alignment}}
434 }
435 }
436 # print "sureMatch:$sureMatch possibleMatch:$possibleMatch possibleMatchSure:$possibleMatchSure\n";
437  
438 # 5 COMPUTE EVALUATION MEASURES
439 # now determine the precision, recall, and F-measure for [S]ure alignments
440 1 ▶ if(scalar(keys %sureSubmission) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %sureSubmission != 0) { } 1 0 1
441 1 $surePrecision = $sureMatch / $totalSureSubmission_weightsSure;
442 }else {
443 0 $surePrecision = 0;
444 }
445 1 ▶ if(scalar(keys %sureAnswer) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %sureAnswer != 0) { } 1 0 1
446 1 $sureRecall = $sureMatch / $totalSureAnswer_weightsSure;
447 }else {
448 0 $sureRecall = 0;
449 }
450 1 ▶ if($sureRecall != 0 && $surePrecision != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sureRecall != 0 and $surePrecision != 0) { } 1 0 1
Condition33%1/3
$sureRecall != 0 and $surePrecision != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$sureRecall != 0 and $surePrecision != 0
$sureRecall != 0 $surePrecision != 0
Truth table33%1/3
A and B
A= $sureRecall != 0B= $surePrecision != 0
ABresult
0X0
100
111
451 1 $sureFMeasure = 2 * $sureRecall * $surePrecision / ($sureRecall + $surePrecision);
452 }else {
453 0 $sureFMeasure = 0;
454 }
455  
456  
457 # and now determine the precision, recall, and F-measure for [P]robable alignments
458 1 ▶ if(scalar(keys %sureSubmission) + scalar(keys %possibleSubmission) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(keys %sureSubmission) + scalar(keys %possibleSubmission) != 0) { } 1 0 1
459 1 $possiblePrecision = $possibleMatch / ($totalSureSubmission_weightsPossible+$totalPossibleSubmission);
460 }else {
461 0 $possiblePrecision = 0;
462 }
463 1 ▶ if(scalar(keys %sureAnswer) + scalar(keys %possibleAnswer)!= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(keys %sureAnswer) + scalar(keys %possibleAnswer) != 0) { } 1 0 1
464 1 $possibleRecall = $possibleMatch / ($totalSureAnswer_weightsPossible+$totalPossibleAnswer);
465 }else {
466 0 $possibleRecall = 0;
467 }
468 1 ▶ if($possibleRecall != 0 && $possiblePrecision != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($possibleRecall != 0 and $possiblePrecision != 0) { } 1 0 1
Condition33%1/3
$possibleRecall != 0 and $possiblePrecision != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$possibleRecall != 0 and $possiblePrecision != 0
$possibleRecall != 0 $possiblePrecision != 0
Truth table33%1/3
A and B
A= $possibleRecall != 0B= $possiblePrecision != 0
ABresult
0X0
100
111
469 1 $possibleFMeasure = 2 * $possibleRecall * $possiblePrecision / ($possibleRecall + $possiblePrecision);
470 }else {
471 0 $possibleFMeasure = 0;
472 }
473  
474 # and determine the AER
475 1 ▶ if(scalar(keys %sureSubmission) + scalar(keys %possibleSubmission) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(keys %sureSubmission) + scalar(keys %possibleSubmission) != 0) { } 1 0 1
476 1 $AER = 1 - ($possibleMatchSure + $possibleMatch) / ($totalSureSubmission_weightsPossible+$totalPossibleSubmission+$totalSureAnswer_weightsPossible);
477 }else {
478 0 $AER = 0;
479 }
480 1 return Lingua::AlignmentEval->new($surePrecision,$sureRecall,$sureFMeasure,$possiblePrecision,$possibleRecall,$possibleFMeasure,$AER);
481 }
482 ###################################################################
483 ### PROCESSING ###
484 ###################################################################
485  
486 0 ▶sub processAlignment{
Subroutine processAlignment: not called CC=6 SCAR=37.4
Pod undocumented
487 0 my ($alSet,$AlignmentSub,$newLocation,$newFormat,$alignMode)=@_;
488 0 my $newAlSet = $alSet->copy;
489 0 ▶ if (ref($AlignmentSub) eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $AlignmentSub eq "ARRAY") 0 0 0
490 0 ▶ if ($AlignmentSub->[0] eq "Lingua::Alignment::eliminateWord"){
Branch: 1 branch
BranchTrueFalseTotal
if ($AlignmentSub->[0] eq "Lingua::Alignment::eliminateWord") 0 0 0
491 0 ▶ if (@$AlignmentSub<3){die "Missing parameters for Lingua::Alignment::eliminateWord\n"}
Branch: 1 branch
BranchTrueFalseTotal
if (@$AlignmentSub < 3) { } 0 0 0
492 else{
493 0 my $side = lc $AlignmentSub->[2];
494 0 ▶ if (!$alSet->{location}{$side} || !$newLocation->{$side}){die "Missing $side file for Lingua::Alignment::eliminateWord\n"}
Branch: 1 branch
BranchTrueFalseTotal
unless ($alSet->{"location"}{$side} and $newLocation->{$side}) 0 0 0
Condition0%0/3
$alSet->{"location"}{$side} and $newLocation->{$side}
!ll&&!rl&&r
000
MC/DC0%0/2
$alSet->{"location"}{$side} and $newLocation->{$side}
$alSet->{"location"}{$side} $newLocation->{$side}
Truth table0%0/3
A and B
A= $alSet->{"location"}{$side}B= $newLocation->{$side}
ABresult
0X0
100
111
495 }
496 }
497 }
498 0 $newAlSet->convert($newLocation,$newFormat,$alignMode,$AlignmentSub);
499 0 return $newAlSet;
500 }
501  
502 0 ▶sub symmetrize {
Subroutine symmetrize: not called CC=60 SCAR=82.1
Pod undocumented
503 0 my ($alSet,$newLocation,$newFormat,$ENV,$selectSubgroups,$alignMode,$globals)=@_;
504 #defaults
505 0 ▶ if (!defined($selectSubgroups)){$selectSubgroups=0}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $selectSubgroups) 0 0 0
506 0 ▶ if (!defined($alignMode)){$alignMode="no-null-align"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alignMode) 0 0 0
507 0 ▶ if (!defined($globals->{"minPhraseFrequency"})){$globals->{"minPhraseFrequency"}=2};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $globals->{"minPhraseFrequency"}) 0 0 0
508 0 ▶ if (!defined($globals->{"extendGroups"})){$globals->{"extendGroups"}=0};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $globals->{"extendGroups"}) 0 0 0
509 0 ▶ if (!defined($globals->{"onlyGroups"})){$globals->{"onlyGroups"}=1};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $globals->{"onlyGroups"}) 0 0 0
510 0 ▶ if (!defined($globals->{"defaultActionGrouping"})){$globals->{"defaultActionGrouping"}="Lingua::Alignment::getUnion"};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $globals->{"defaultActionGrouping"}) 0 0 0
511 0 ▶ if (!defined($globals->{"defaultActionGeneral"})){$globals->{"defaultActionGeneral"}="Lingua::Alignment::intersect"};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $globals->{"defaultActionGeneral"}) 0 0 0
512 0 ▶ if (!defined($globals->{"verbose"})){$globals->{"verbose"}="0"};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $globals->{"verbose"}) 0 0 0
513 0 my $verbose = $globals->{"verbose"};
514 0 my $al; # reference alignment- remains unchanged
515 my $modAl; #reference alignment modified with the successive aplication of symRules
516 #load in memory a chunk of the alignment set as a list
517 #of references to (internal representation) alignment objects:
518 0 my ($k,$alSetChunk);
519 0 my $FH = $alSet->openFiles();
520 0 my $newFH;
521 0 ▶ if ($selectSubgroups==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($selectSubgroups == 0) 0 0 0
522 0 $newFH = openLocation($newLocation,$newFormat,">",$alSet->{location});
523 }
524 0 my $internalSentPairNum = 1;
525 0 my ($sentenceNum,$ruleApplied) = ($alSet->{firstSentPair},0);
526  
527 0 my $grSentPairNum=1;
528  
529 0 my ($j,$i);
530 0 my ($lines,$line);
531 0 my $groups = {};
532 0 my $groupsCurrentSentence = {};
533 0 my $groupKeys = [];
534 0 my $subGroups={};
535 0 my $subGroupsCurrentSentence = {};
536 0 my $subGroupKeys=[];
537 0 my ($candidate,$count);
538  
539 0 ▶ if (!$selectSubgroups){ #load subgroup hash and array:
Branch: 1 branch
BranchTrueFalseTotal
unless ($selectSubgroups) 0 0 0
540 0 open(GROUPS,"<$ENV/groups");
541 0 ▶ while (<GROUPS>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline GROUPS)) 0 0 0
542 0 push @$groupKeys,$_;
543 0 @$line = split " | ",$_,2;
544 0 $groups->{$line->[1]}=$line->[0];
545 }
546 0 ▶ if ($globals->{onlyGroups}==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($globals->{"onlyGroups"} == 0) 0 0 0
547 0 open(SUBGROUPS,"<$ENV/subGroups");
548 0 ▶ while (<SUBGROUPS>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline SUBGROUPS)) 0 0 0
549 0 push @$subGroupKeys,$_;
550 0 @$line = split " | ",$_,2;
551 0 $subGroups->{$line->[1]}=$line->[0];
552 }
553 }
554 }
555
556 0 my %anchors;
557 0 my %sourcePerturbed={};
558 0 my %targetPerturbed={}; # Perturbations must be distinct so we keep track of the already detected "Perturbed" $j's
559 0 my ($perturbation,$perturbationNoMod);
560 0 my ($lastAnchorSource,$lastAnchorTarget,$newAnchorSource,$newAnchorTarget);
561 0 my ($ind,$newPerturbationDetected,$anchorsInTarget);
562 0 my ($countPertubs,$countGrouping,$countOneToMany,$countElse,$countNoGroup)=(0,0,0,0,0);
563 0 ▶ while ($alSetChunk = $alSet->loadChunk($FH,$sentenceNum,$alignMode)){ # returns 0 if eof or last sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($alSetChunk = $alSet->loadChunk($FH, $sentenceNum, $alignMode)) 0 0 0
564 0 ▶ for ($k=0;$k<@$alSetChunk;$k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @$alSetChunk) 0 0 0
565 # print "\nsentence pair $sentenceNum\n";
566 0 $ruleApplied=0;
567 0 $al = $$alSetChunk[$k];
568 0 ▶ if ($verbose >0){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
569 0 print $sentenceNum."\n";
570 0 print $al->sourceSentence."\n";
571 0 print $al->targetSentence."\n";
572 }
573 0 $modAl = $al->clone();
574 0 ($lastAnchorSource,$lastAnchorTarget)=(0,0);
575 0 %sourcePerturbed=();
576 0 %targetPerturbed=();
577 0 $j = 1;
578 #detect "perturbations" in the anchor diagonal looping only over $j (to have less repeated zones). We can only miss those where $i is aligned only to NULL
579 0 ▶ while ($j<@{$al->{sourceAl}}){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
580 0 ▶ while ( !$al->isAnchor($j,"source") && $j<(@{$al->{sourceAl}})){
Branch: 1 branch
BranchTrueFalseTotal
if (not $al->isAnchor($j, "source") and $j < @{$al->{"sourceAl"};}) 0 0 0
Condition0%0/3
not $al->isAnchor($j, "source") and $j < @{$al->{"sourceAl"};}
!ll&&!rl&&r
000
MC/DC0%0/2
not $al->isAnchor($j, "source") and $j < @{$al->{"sourceAl"};}
not $al->isAnchor($j, "source") $j < @{$al->{"sourceAl"};}
Truth table0%0/3
not(A) and B
A= not $al->isAnchor($j, "source")B= $j < @{$al->{"sourceAl"};}
ABresult
0X0
100
111
581 0 $j++;
582 }
583 0 ▶ if ($j<=@{$al->{sourceAl}}){
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= @{$al->{"sourceAl"};}) 0 0 0
584 0 ▶ if ($j==@{$al->{sourceAl}}){
Branch: 1 branch
BranchTrueFalseTotal
if ($j == @{$al->{'sourceAl'};}) { } 0 0 0
585 0 ($newAnchorSource,$newAnchorTarget) = ($j,scalar(@{$al->{targetAl}}));
586 }else{
587 0 ($newAnchorSource,$newAnchorTarget) = ($j,$al->{sourceAl}[$j][0]);
588 }
589 0 $newPerturbationDetected=0;
590 0 ▶ if (($newAnchorSource-$lastAnchorSource)!=1 && !$sourcePerturbed{$lastAnchorSource+1}){
Branch: 2 branches
BranchTrueFalseTotal
if ($newAnchorSource - $lastAnchorSource != 1 and not $sourcePerturbed{$lastAnchorSource + 1}) { } 0 0 0
elsif ($newAnchorTarget - $lastAnchorTarget != 1 and not $targetPerturbed{$lastAnchorTarget + 1}) { } 0 0 0
Condition0%0/3
$newAnchorSource - $lastAnchorSource != 1 and not $sourcePerturbed{$lastAnchorSource + 1}
!ll&&!rl&&r
000
MC/DC0%0/2
$newAnchorSource - $lastAnchorSource != 1 and not $sourcePerturbed{$lastAnchorSource + 1}
$newAnchorSource - $lastAnchorSource != 1 not $sourcePerturbed{$lastAnchorSource + 1}
Truth table0%0/3
A and not(B)
A= $newAnchorSource - $lastAnchorSource != 1B= not $sourcePerturbed{$lastAnchorSource + 1}
ABresult
0X0
100
111
591 0 $newPerturbationDetected = 1;
592 ▶ } elsif (($newAnchorTarget-$lastAnchorTarget)!=1 && !$targetPerturbed{$lastAnchorTarget+1}){
Condition0%0/3
$newAnchorTarget - $lastAnchorTarget != 1 and not $targetPerturbed{$lastAnchorTarget + 1}
!ll&&!rl&&r
000
MC/DC0%0/2
$newAnchorTarget - $lastAnchorTarget != 1 and not $targetPerturbed{$lastAnchorTarget + 1}
$newAnchorTarget - $lastAnchorTarget != 1 not $targetPerturbed{$lastAnchorTarget + 1}
Truth table0%0/3
A and not(B)
A= $newAnchorTarget - $lastAnchorTarget != 1B= not $targetPerturbed{$lastAnchorTarget + 1}
ABresult
0X0
100
111
593 0 $anchorsInTarget=1;
594 0 ▶ for ($i=$lastAnchorTarget+1;$i<$newAnchorTarget;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $newAnchorTarget) 0 0 0
595 0 ▶ if (!$al->isAnchor($i,"target")){$anchorsInTarget=0}
Branch: 1 branch
BranchTrueFalseTotal
unless ($al->isAnchor($i, "target")) 0 0 0
596 }
597 0 ▶ if (!$anchorsInTarget){$newPerturbationDetected=1};
Branch: 1 branch
BranchTrueFalseTotal
unless ($anchorsInTarget) 0 0 0
598 }
599 0 ▶ if ( $newPerturbationDetected ){
Branch: 1 branch
BranchTrueFalseTotal
if ($newPerturbationDetected) 0 0 0
600 0 $countPertubs++;
601 # print "\n($lastAnchorSource,$lastAnchorTarget,$newAnchorSource,$newAnchorTarget)\n";
602
603 0 $perturbation = $al->cut($lastAnchorSource,$lastAnchorTarget,$newAnchorSource,$newAnchorTarget);
604 0 $perturbationNoMod = $al->cut($lastAnchorSource,$lastAnchorTarget,$newAnchorSource,$newAnchorTarget);
605  
606 # PRINT PERTURBATION TO FILE
607 #if (exists($newFH->{source})){
608 # $newFH->{source}->print("<s snum=$grSentPairNum> ".join(" ",@{$perturbation->{sourceWords}})." </s>\n");
609 #}
610 #if (exists($newFH->{target})){
611 # $newFH->{target}->print("<s snum=$grSentPairNum> ".join(" ",@{$perturbation->{targetWords}})." </s>\n");
612 #}
613 #$perturbation->getUnion;
614 #$lines = $perturbation->writeToBlinker;
615 #foreach $line (@$lines){
616 # $newFH->{sourceToTarget}->print("$grSentPairNum $line\n");
617 #}
618 #$grSentPairNum++;
619  
620 #END PRINT PERTURBATION TO FILE
621 0 ▶ if ($selectSubgroups){
Branch: 1 branch
BranchTrueFalseTotal
if ($selectSubgroups) { } 0 0 0
622 0 $perturbation->selectSubgroups($groupsCurrentSentence,$subGroupsCurrentSentence,$globals);
623 }else{
624 0 ▶ if (1==0){
Branch: 1 branch
BranchTrueFalseTotal
if (do { ($ruleApplied = $perturbation->applyGrouping($groupKeys, $subGroupKeys, $globals)) > 0 }) { } 0 0 0
625 # if ($ruleApplied=$perturbation->applyOneToMany_2()){
626 $countOneToMany++;
627 }elsif (($ruleApplied=$perturbation->applyGrouping($groupKeys,$subGroupKeys,$globals))>0){
628 0 $countGrouping++;
629 }else{
630 0 my $defaultActionGen = $globals->{defaultActionGeneral};
631 0 $perturbation->$defaultActionGen();
632 0 ▶ if ($ruleApplied==-1){
Branch: 1 branch
BranchTrueFalseTotal
if ($ruleApplied == -1) { } 0 0 0
633 0 $countNoGroup++;
634 }else{
635 0 $perturbation->processNull();
636 0 $countElse++;
637 }
638 }
639 0 $perturbation->paste($modAl);
640 }
641 # print "\ns indices:",join (" ",keys %{$perturbation->{sourceIndices}}),"\n";
642 # print "t indices:",join (" ",keys %{$perturbation->{targetIndices}}),"\n";
643 0 foreach $ind (keys %{$perturbation->{sourceIndices}}){
644 0 ▶ if ($ind>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($ind > 0) 0 0 0
645 0 $sourcePerturbed{$ind+$perturbation->{zeroSource}}=1;
646 }
647 }
648 0 foreach $ind (keys %{$perturbation->{targetIndices}}){
649 0 ▶ if ($ind>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($ind > 0) 0 0 0
650 0 $targetPerturbed{$ind+$perturbation->{zeroTarget}}=1;
651 }
652 }
653 # print "s perturbed:",join (" ",keys %sourcePerturbed),"\n";
654 # print "t perturbed:",join (" ",keys %targetPerturbed),"\n";
655 } #if perturbation
656
657 0 $anchors{"$newAnchorSource $newAnchorTarget"}=1;
658 0 ($lastAnchorSource,$lastAnchorTarget) = ($newAnchorSource,$newAnchorTarget);
659 0 $j++;
660 }
661 } #while j...
662 0 ▶ if ($newFormat eq "NAACL" && !$selectSubgroups){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFormat eq "NAACL" and not $selectSubgroups) 0 0 0
Condition0%0/3
$newFormat eq "NAACL" and not $selectSubgroups
!ll&&!rl&&r
000
MC/DC0%0/2
$newFormat eq "NAACL" and not $selectSubgroups
$newFormat eq "NAACL" not $selectSubgroups
Truth table0%0/3
A and not(B)
A= $newFormat eq "NAACL"B= not $selectSubgroups
ABresult
0X0
100
111
663 0 ▶ if (exists($newFH->{source})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $newFH->{"source"}) 0 0 0
664 0 $newFH->{source}->print("<s snum=$internalSentPairNum> ".join(" ",@{$modAl->{sourceWords}})." </s>\n");
665 }
666 0 ▶ if (exists($newFH->{target})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $newFH->{"target"}) 0 0 0
667 0 $newFH->{target}->print("<s snum=$internalSentPairNum> ".join(" ",@{$modAl->{targetWords}})." </s>\n");
668 }
669 0 $al->intersect();
670 0 $lines = $modAl->writeToBlinker;
671 0 foreach $line (@$lines){
672 0 $newFH->{sourceToTarget}->print("$internalSentPairNum $line\n");
673 }
674 }
675 0 ▶ if (($internalSentPairNum % 1000)==0){print STDERR $internalSentPairNum}
Branch: 2 branches
BranchTrueFalseTotal
if ($internalSentPairNum % 1000 == 0) { } 0 0 0
elsif ($internalSentPairNum % 100 == 0) { } 0 0 0
676 0 elsif (($internalSentPairNum % 100)==0){print STDERR "."}
677 0 $sentenceNum++;
678 0 $internalSentPairNum++;
679 0 ▶ if ($verbose > 0){print "Candidates:\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
680 0 ▶ if ($selectSubgroups){
Branch: 1 branch
BranchTrueFalseTotal
if ($selectSubgroups) 0 0 0
681 0 foreach $candidate (keys %$groupsCurrentSentence){
682 0 ▶ if ($verbose > 0){print "$candidate\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
683 0 $groups->{$candidate}=$groups->{$candidate}+1;
684 }
685 0 %$groupsCurrentSentence=();
686 0 ▶ if ($globals->{onlyGroups}==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($globals->{"onlyGroups"} == 0) 0 0 0
687 0 foreach $candidate (keys %$subGroupsCurrentSentence){
688 0 $subGroups->{$candidate}=$subGroups->{$candidate}+1;
689 }
690 0 %$subGroupsCurrentSentence=();
691 }
692 }
693 }#for k<@alSetChunk
694 } #while alsetchunk
695 0 print STDERR "\n";
696 0 ▶ if ($selectSubgroups==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($selectSubgroups == 0) 0 0 0
697 0 closeFiles($newFH,$newFormat);
698 }
699 0 closeFiles($FH,$alSet->{format});
700 0 ▶ if ($selectSubgroups){
Branch: 1 branch
BranchTrueFalseTotal
if ($selectSubgroups) { } 0 0 0
701 0 ▶ if ($verbose>0){print "\ngroups:",scalar(keys(%$groups))," - subgroups:",scalar(keys(%$subGroups)),"\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
702 0 ▶ open(GROUPS, ">$ENV/groups") or die "File opening error:$!";;
Branch: 1 branch
BranchTrueFalseTotal
open GROUPS, ">$ENV/groups" or die "File opening error:$!" 0 0 0
703 0 ▶ while (($candidate,$count)=each(%$groups)){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$groups) 0 0 0
704 # print "groups $count | $candidate\n";
705 0 ▶ if ($count >= $globals->{minPhraseFrequency}){
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= $globals->{"minPhraseFrequency"}) 0 0 0
706 0 print GROUPS "$count | $candidate\n";
707 }
708 }
709 0 ▶ if ($globals->{onlyGroups}==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($globals->{"onlyGroups"} == 0) 0 0 0
710 0 ▶ open(SUBGROUPS, ">$ENV/subGroups") or die "File opening error:$!";;
Branch: 1 branch
BranchTrueFalseTotal
open SUBGROUPS, ">$ENV/subGroups" or die "File opening error:$!" 0 0 0
711 0 ▶ while (($candidate,$count)=each(%$subGroups)){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$subGroups) 0 0 0
712 # print "SUBGROUPS $count | $candidate\n";
713 0 ▶ if ($count >= $globals->{minPhraseFrequency}){
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= $globals->{"minPhraseFrequency"}) 0 0 0
714 0 print SUBGROUPS "$count | $candidate\n";
715 }
716 }
717 }
718 }else{
719 0 print STDERR "perturbations:$countPertubs (oneToMany:$countOneToMany grouped:$countGrouping not grouped:$countNoGroup others:$countElse)\n";
720 0 $alSet->{location}=$newLocation;
721 0 $alSet->{format}=$newFormat;
722 0 $alSet->{firstSentPair}=1;
723 0 $alSet->{lastSentPair}="eof";
724 }
725 }
726  
727 0 ▶sub orderAsBilCorpus {
Subroutine orderAsBilCorpus: not called CC=18 SCAR=58.3
Pod undocumented
728 0 my ($alSet,$newLocation,$newFormat,$alignMode,$corpSrc,$corpTrg,$verbose)=@_;
729 0 ▶ if (!defined($newFormat)){$newFormat="TALP"}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $newFormat) { } 0 0 0
730 0 else {$newFormat = uc $newFormat}
731 0 $newLocation = readLocation($newLocation);
732 0 my $FH = $alSet->openFiles();
733 0 my $newFH = openLocation($newLocation,$newFormat,">",$alSet->{location});
734 0 my $inputSentPairNum=$alSet->{firstSentPair};
735 0 my $internalSentPairNum = 1;
736 0 ▶ if ($verbose >0){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
737 0 select STDOUT; $| = 1; # enable autoflush (desactivate buffering)
738 }
739 0 ▶ open(CS,"<$corpSrc") || die "$corpSrc file opening error !";
Branch: 1 branch
BranchTrueFalseTotal
open CS, "<$corpSrc" or die "$corpSrc file opening error !" 0 0 0
740 0 ▶ open(CT,"<$corpTrg") || die "$corpTrg file opening error !";
Branch: 1 branch
BranchTrueFalseTotal
open CT, "<$corpTrg" or die "$corpTrg file opening error !" 0 0 0
741  
742 # LOAD BILINGUAL CORPUS IN HASH
743 0 my %newcorp;
744 0 my $cntCorp=0;
745 0 ▶ while (my $s=<CS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $s = readline CS)) 0 0 0
746 0 chomp $s;
747 0 my $t=<CT>;
748 0 chomp $t;
749 0 $newcorp{"$s ||| $t"}=1;
750 0 $cntCorp++;
751 }
752 0 ▶ if ($verbose > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
753 0 print "Number of different sentence pairs in new corpus:".scalar(keys %newcorp)."\n";
754 }
755
756 # PARSE AL SET
757 0 my $cntToFind=$cntCorp;
758 0 my %found;
759 0 ▶ while (my $alSetChunk = $alSet->loadChunk($FH,$inputSentPairNum,"as-is")){ # returns 0 if eof or last sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if (my $alSetChunk = $alSet->loadChunk($FH, $inputSentPairNum, "as-is")) 0 0 0
760 0 ▶ for (my $i=0;$i<@$alSetChunk;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$alSetChunk) 0 0 0
761 0 my $al = $$alSetChunk[$i];
762 0 my $pair=$al->sourceSentence." ||| ".$al->targetSentence;
763 0 ▶ if ($newcorp{$pair}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newcorp{$pair}) 0 0 0
764 0 $found{$pair}=$al;
765 0 $cntToFind--;
766 }
767 } #for
768 0 ▶ if ($cntToFind==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($cntToFind == 0) 0 0 0
769 0 ▶ if ($verbose>0){print STDERR "Leaving loop of giza file at line:$inputSentPairNum\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
770 0 last;
771 }
772 0 $inputSentPairNum++;
773 0 ▶ if ($verbose>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
774 0 ▶ if ($inputSentPairNum % 100000 ==0){print $inputSentPairNum;}
Branch: 1 branch
BranchTrueFalseTotal
if ($inputSentPairNum % 100000 == 0) 0 0 0
775 0 ▶ if ($inputSentPairNum % 10000 ==0){print ".";}
Branch: 1 branch
BranchTrueFalseTotal
if ($inputSentPairNum % 10000 == 0) 0 0 0
776 }
777 } #while
778 0 ▶ if ($verbose>0){print "\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
779 # REORDER AL SET
780 0 print "reordering Alignment set...\n";
781 0 seek CS,0,0; #go back to beginning of file
782 0 seek CT,0,0;
783 0 ▶ while (my $s=<CS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $s = readline CS)) 0 0 0
784 0 chomp $s;
785 0 my $t=<CT>;
786 0 chomp $t;
787 0 ▶ if (exists($found{"$s ||| $t"})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $found{"$s ||| $t"}) { } 0 0 0
788 0 my $al= $found{"$s ||| $t"};
789 0 $al->output($FH,$newFormat,$newFH,$newLocation,$internalSentPairNum);
790 }else{
791 0 die "ERROR: not found sentence pair $internalSentPairNum in Alignment Set\n";
792 }
793 0 $internalSentPairNum++;
794 }
795 0 closeFiles($newFH,$newFormat);
796 0 closeFiles($FH,$alSet->{format});
797 0 $alSet->updateObject($newFormat,$newLocation,$internalSentPairNum-1);
798 }
799  
800 0 ▶sub adaptToBilCorpus {
Subroutine adaptToBilCorpus: not called CC=51 SCAR=78.8
Pod undocumented
801 0 my ($alSet,$newLocation,$newFormat,$alignMode,$corpSrc,$corpTrg,$restrictions,$verbose)=@_;
802 0 my $pdiff=$restrictions->{allowedPercentWordDiff};
803 0 my $mindiff=$restrictions->{minAllowedNumWordDiff};
804 0 my $maxdiff=$restrictions->{maxAllowedNumWordDiff};
805 0 my $nfirst=$restrictions->{numWordsConsideredFirst};
806 0 my $dumper = new Dumpvalue;
807  
808 0 ▶ if (!defined($newFormat)){$newFormat="TALP"}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $newFormat) { } 0 0 0
809 0 else {$newFormat = uc $newFormat}
810 0 $newLocation = readLocation($newLocation);
811 0 my $FH = $alSet->openFiles();
812 0 my $newFH = openLocation($newLocation,$newFormat,">",$alSet->{location});
813 0 my $inputSentPairNum=$alSet->{firstSentPair};
814 0 my $internalSentPairNum = 1;
815 0 select STDOUT; $| = 1; # enable autoflush (desactivate buffering)
816 0 ▶ open(CS,"<$corpSrc") || die "$corpSrc file opening error !";
Branch: 1 branch
BranchTrueFalseTotal
open CS, "<$corpSrc" or die "$corpSrc file opening error !" 0 0 0
817 0 ▶ open(CT,"<$corpTrg") || die "$corpTrg file opening error !";
Branch: 1 branch
BranchTrueFalseTotal
open CT, "<$corpTrg" or die "$corpTrg file opening error !" 0 0 0
818  
819 # DETECT ALSET SENTENCES THAT ARE IN THE CORPUS
820 0 my %newcorp;
821 0 ▶ while (my $s=<CS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $s = readline CS)) 0 0 0
822 0 chomp $s;
823 0 my $t=<CT>;
824 0 chomp $t;
825 0 $newcorp{"$s ||| $t"}=1;
826 }
827 0 ▶ if ($verbose > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
828 0 print "Number of different sentence pairs in new corpus:".scalar(keys %newcorp)."\n";
829 }
830 0 my $count=0;
831 0 ▶ while (my $alSetChunk = $alSet->loadChunk($FH,$inputSentPairNum,$alignMode)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $alSetChunk = $alSet->loadChunk($FH, $inputSentPairNum, $alignMode)) 0 0 0
832 # print $inputSentPairNum."\n";
833 0 ▶ for (my $i=0;$i<@$alSetChunk;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$alSetChunk) 0 0 0
834 0 my $al = $$alSetChunk[$i];
835 0 my $s = $al->sourceSentence;
836 0 my $t = $al->targetSentence;
837 0 my @ws=split / /,$s;
838 0 my @wt=split / /,$t;
839 0 my ($nums,$numt)=(scalar(@ws),scalar(@wt));
840 0 ▶ if (!$newcorp{"$s ||| $t"}){
Branch: 1 branch
BranchTrueFalseTotal
if (not $newcorp{"$s ||| $t"}) { } 0 0 0
841 # DETECT CLOSEST SENTENCES IN NEW CORPUS AND MODIFY ALIGNMENT SET
842 # calculate values for length test (to, later, skip lcs calculation)
843 0 my $sAllowedDiff = Lingua::AlSetLib::max(Lingua::AlSetLib::min($nums*$pdiff/100,$maxdiff),$mindiff);
844 0 my $tAllowedDiff = Lingua::AlSetLib::max(Lingua::AlSetLib::min($numt*$pdiff/100,$maxdiff),$mindiff);
845 0 my $sMin=$nums-$sAllowedDiff;
846 0 my $sMax=$nums+$sAllowedDiff;
847 0 my $tMin=$numt-$tAllowedDiff;
848 0 my $tMax=$numt+$tAllowedDiff;
849 0 ▶ if ($verbose > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
850 0 print $s."\n";
851 0 print $t."\n";
852 0 print "ns:$nums nt:$numt allowed length diff: s:$sAllowedDiff t:$tAllowedDiff;\n\n";
853 }
854 0 my $bestLcs = 0;
855 0 my @bestSrc;
856 my @bestTrg;
857 0 my ($cntGoodLength,$cntPassedFirstLCS,$cnt2)=(0,0,0);
858 # parse new corpus
859 0 foreach my $pair (keys %newcorp){
860 0 my ($cs,$ct)=split / \|\|\| /,$pair;
861 0 my @wcs = split / /,$cs;
862 0 my @wct = split / /,$ct;
863 0 my ($numcs,$numct)=(scalar(@wcs),scalar(@wct));
864  
865 0 ▶ if ($verbose >2){print "ncs:$numcs nct:$numct\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 2) 0 0 0
866 0 ▶ if ($verbose >2){print "CORPUS:\n$cs\n$ct\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 2) 0 0 0
867 #length test:
868 0 ▶ if ($numcs<$sMin || $numcs>$sMax || $numct<$tMin || $numct>$tMax){
Branch: 1 branch
BranchTrueFalseTotal
if ($numcs < $sMin or $numcs > $sMax or $numct < $tMin or $numct > $tMax) { } 0 0 0
Condition0%0/9
$numcs < $sMin or $numcs > $sMax or $numct < $tMin or $numct > $tMax
l!l&&r!l&&!r
000
$numcs < $sMin or $numcs > $sMax or $numct < $tMin
l!l&&r!l&&!r
000
$numcs < $sMin or $numcs > $sMax
l!l&&r!l&&!r
000
MC/DC0%0/4
$numcs < $sMin or $numcs > $sMax or $numct < $tMin or $numct > $tMax
$numcs < $sMin $numcs > $sMax $numct < $tMin $numct > $tMax
Truth table0%0/5
A or B or C or D
A= $numcs < $sMinB= $numcs > $sMaxC= $numct < $tMinD= $numct > $tMax
ABCDresult
1XXX1
01XX1
001X1
00011
00000
869 # skip lcs ratio calculation
870 }else{
871 0 $cntGoodLength++;
872 #calculate LCS, but first looking at the nfirst first words
873 0 my @fws;
874 my @fwt;
875 0 my @fwcs;
876 0 my @fwct;
877 0 ▶ for (my $i=0;$i<$nfirst;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nfirst) 0 0 0
878 0 push @fws,$ws[$i];
879 0 push @fwt,$wt[$i];
880 0 push @fwcs,$wcs[$i];
881 0 push @fwct,$wct[$i];
882 }
883 0 my $sLcs = Lingua::AlSetLib::LCS_ratio(\@fws,\@fwcs);
884 0 my $tLcs = Lingua::AlSetLib::LCS_ratio(\@fwt,\@fwct);
885 # first words LCS test:
886 0 ▶ if ($sLcs == 0 || $tLcs == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($sLcs == 0 or $tLcs == 0) { } 0 0 0
Condition0%0/3
$sLcs == 0 or $tLcs == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$sLcs == 0 or $tLcs == 0
$sLcs == 0 $tLcs == 0
Truth table0%0/3
A or B
A= $sLcs == 0B= $tLcs == 0
ABresult
1X1
011
000
887 # skip lcs ratio calculation
888 }else{
889 0 $cntPassedFirstLCS++;
890 0 my $sLcs = Lingua::AlSetLib::LCS_ratio(\@ws,\@wcs);
891 0 my $tLcs = Lingua::AlSetLib::LCS_ratio(\@wt,\@wct);
892 0 ▶ if ($verbose >1){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 1) 0 0 0
893 0 print "ncs:$numcs nct:$numct\n";
894 0 print "CORPUS:\n$cs\n$ct\n";
895 0 print "chars src lcsr: $sLcs\t trg lcsr:$tLcs\n";
896 }
897 0 my $lcs = $sLcs+$tLcs;
898 0 ▶ if ($lcs > $bestLcs){
Branch: 2 branches
BranchTrueFalseTotal
if ($lcs > $bestLcs) { } 0 0 0
elsif ($lcs == $bestLcs) { } 0 0 0
899 0 $bestLcs = $lcs;
900 0 @bestSrc = ($cs);
901 0 @bestTrg = ($ct);
902 }elsif($lcs == $bestLcs){
903 0 push @bestSrc,$cs;
904 0 push @bestTrg,$ct;
905 }
906 }
907 } # if length test
908 0 ▶ if ($verbose >1){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 1) 0 0 0
909 0 ▶ if ($cnt2>0 && ($cnt2 % 100000)==0){print "$cnt2";}
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt2 > 0 and $cnt2 % 100000 == 0) { } 0 0 0
elsif ($cnt2 > 0 and $cnt2 % 1000 == 0) { } 0 0 0
Condition0%0/3
$cnt2 > 0 and $cnt2 % 100000 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$cnt2 > 0 and $cnt2 % 100000 == 0
$cnt2 > 0 $cnt2 % 100000 == 0
Truth table0%0/3
A and B
A= $cnt2 > 0B= $cnt2 % 100000 == 0
ABresult
0X0
100
111
910 0 ▶ elsif ($cnt2>0 && ($cnt2 % 1000)==0){print ".";}
Condition0%0/3
$cnt2 > 0 and $cnt2 % 1000 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$cnt2 > 0 and $cnt2 % 1000 == 0
$cnt2 > 0 $cnt2 % 1000 == 0
Truth table0%0/3
A and B
A= $cnt2 > 0B= $cnt2 % 1000 == 0
ABresult
0X0
100
111
911 }
912 0 $cnt2++;
913 } # for each sent pair in corpus
914 0 my $uniqBestSrc;
915 my $uniqBestTrg;
916 0 my $cntCharLevel=scalar(@bestSrc);
917 0 ▶ if ($cntCharLevel>1){
Branch: 1 branch
BranchTrueFalseTotal
if ($cntCharLevel > 1) { } 0 0 0
918 0 my $bestLcs = 0;
919 # calculate LCS at character level
920 0 ▶ for (my $i=0;$i<$cntCharLevel;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cntCharLevel) 0 0 0
921 0 my $cs=$bestSrc[$i];
922 0 my $ct=$bestTrg[$i];
923 0 $cntCharLevel++;
924 0 my @chars = split //,$s;
925 0 my @charcs = split //,$cs;
926 0 my $sLcs = Lingua::AlSetLib::LCS_ratio(\@chars,\@charcs);
927 0 my @chart = split //,$t;
928 0 my @charct = split //,$ct;
929 0 my $tLcs = Lingua::AlSetLib::LCS_ratio(\@chart,\@charct);
930 0 my $lcs = $sLcs+$tLcs;
931 0 ▶ if ($lcs > $bestLcs){
Branch: 1 branch
BranchTrueFalseTotal
if ($lcs > $bestLcs) 0 0 0
932 0 $bestLcs = $lcs;
933 0 $uniqBestSrc = $cs;
934 0 $uniqBestTrg = $ct;
935 }
936 }
937 }else{
938 0 $uniqBestSrc = $bestSrc[0];
939 0 $uniqBestTrg = $bestTrg[0];
940 }
941 0 ▶ if ($cntGoodLength == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($cntGoodLength == 0) { } 0 0 0
942 0 print "WARNING: sentence pair $inputSentPairNum not found in corpus\n";
943 }else{
944 0 ▶ if ($verbose >0){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
945 0 my $numCorp=scalar(keys %newcorp);
946 0 print "Passed length test: $cntGoodLength / $numCorp\n";
947 0 print "Passed first words LCS test: $cntPassedFirstLCS\n";
948 0 ▶ print "LCS calculated at character level: "; if ($cntCharLevel==1){print "0"}else{print $cntCharLevel}; print "\n";
Branch: 1 branch
BranchTrueFalseTotal
if ($cntCharLevel == 1) { } 0 0 0
949 0 print "\nbest lcsr: $bestLcs, best pair:\n$uniqBestSrc\n$uniqBestTrg\n";
950 }
951 # detect edits to pass from alset sent pair to corpus sent pair
952 0 my @bs=split / /,$uniqBestSrc;
953 0 my @diffs = Lingua::AlSetLib::diff( \@ws, \@bs );
954 0 ▶ if ($verbose>2){print $dumper->dumpValue(\@diffs);}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 2) 0 0 0
955  
956 # parse output of diff function
957 0 my @updatedPosi; #array: orig posis -> updated posis
958 my %reversePosi; #hash: updated posis -> orig posis
959 0 ▶ for (my $i=0;$i<=$nums;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nums) 0 0 0
960 0 $updatedPosi[$i]=$i;
961 0 $reversePosi{$i}=$i;
962 }
963  
964 0 foreach my $hunk (@diffs){
965 0 my @delPosi;
966 my @del;
967 0 my @addPosi;
968 0 my @add;
969 0 foreach my $change (@$hunk) {
970 0 ▶ if ($change->[0] eq '-'){
Branch: 1 branch
BranchTrueFalseTotal
if ($change->[0] eq '-') { } 0 0 0
971 0 push @delPosi,$change->[1]+1;
972 0 push @del,$change->[2];
973 }else{
974 0 push @addPosi,$change->[1]+1;
975 0 push @add,$change->[2];
976 }
977 }
978 # del posis are relative to first array (@ws) => update posis
979 # add posis are relative to second array (@bs) => don't update posis
980 0 my $numDel=scalar(@delPosi);
981 0 my $numAdd=scalar(@addPosi);
982 0 ▶ if ($numDel==0){ #insertion
Branch: 1 branch
BranchTrueFalseTotal
if ($numDel == 0) { } 0 0 0
983 0 $al->splice("source",$addPosi[0],0,\@add);
984 0 print "insert '",join(" ",@add),"' at position { ",$addPosi[0]," }\n";
985 #update updatedPosi array
986 0 ▶ for (my $i=$reversePosi{"$addPosi[0]"};$i<=$nums;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nums) 0 0 0
987 0 $updatedPosi[$i]+=$numAdd;
988 0 $reversePosi{"$updatedPosi[$i]"}=$i;
989 }
990 }else{ # substitution or deletion
991 0 $al->splice("source",$updatedPosi[$delPosi[0]],$numDel,\@add);
992 0 print "substitute '",join(" ",@del),"' at positions { ",join(" ",@delPosi)," } by '",join(" ",@add),"'\n";
993 #update updatedPosi array
994 0 ▶ for (my $i=$delPosi[0]+$numDel;$i<=$nums;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nums) 0 0 0
995 0 $updatedPosi[$i]+=$numAdd-$numDel;
996 0 $reversePosi{"$updatedPosi[$i]"}=$i;
997 }
998 }
999 }
1000 #target
1001 0 my @bt=split / /,$uniqBestTrg;
1002 0 my @diffs = Lingua::AlSetLib::diff( \@wt, \@bt );
1003 0 ▶ if ($verbose>2){print $dumper->dumpValue(\@diffs);}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 2) 0 0 0
1004 # parse output of diff function
1005 0 @updatedPosi=();
1006 0 %reversePosi=();
1007 0 ▶ for (my $i=0;$i<=$numt;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $numt) 0 0 0
1008 0 $updatedPosi[$i]=$i;
1009 0 $reversePosi{$i}=$i;
1010 }
1011 0 foreach my $hunk (@diffs){
1012 0 my @delPosi;
1013 my @del;
1014 0 my @add;
1015 0 my @addPosi;
1016 0 foreach my $change (@$hunk) {
1017 0 ▶ if ($change->[0] eq '-'){
Branch: 1 branch
BranchTrueFalseTotal
if ($change->[0] eq '-') { } 0 0 0
1018 0 push @delPosi,$change->[1]+1;
1019 0 push @del,$change->[2];
1020 }else{
1021 0 push @addPosi,$change->[1]+1;
1022 0 push @add,$change->[2];
1023 }
1024 }
1025 #update updatedPosi array
1026 0 my $numDel=scalar(@delPosi);
1027 0 my $numAdd=scalar(@addPosi);
1028 0 ▶ if ($numDel==0){ #insertion
Branch: 1 branch
BranchTrueFalseTotal
if ($numDel == 0) { } 0 0 0
1029 0 $al->splice("target",$addPosi[0],0,\@add);
1030 0 print "insert '",join(" ",@add),"' at position { ",$addPosi[0]," }\n";
1031 #update updatedPosi array
1032 0 ▶ for (my $i=$reversePosi{"$addPosi[0]"};$i<=$numt;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $numt) 0 0 0
1033 0 $updatedPosi[$i]+=$numAdd;
1034 0 $reversePosi{"$updatedPosi[$i]"}=$i;
1035 }
1036 }else{ # substitution or deletion
1037 0 $al->splice("target",$updatedPosi[$delPosi[0]],$numDel,\@add);
1038 0 print "substitute '",join(" ",@del),"' at positions { ",join(" ",@delPosi)," } by '",join(" ",@add),"'\n";
1039 #update updatedPosi array
1040 0 ▶ for (my $i=$delPosi[0]+$numDel;$i<=$numt;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $numt) 0 0 0
1041 0 $updatedPosi[$i]+=$numAdd-$numDel;
1042 0 $reversePosi{"$updatedPosi[$i]"}=$i;
1043 }
1044 }
1045 }
1046 0 ▶ if ($verbose>0){print "--------------------------------------------------------------\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 0) 0 0 0
1047 }
1048 }else{
1049 # sentence pair is in corpus: don't modify anything
1050 }
1051 0 $al->output($FH,$newFormat,$newFH,$newLocation,$internalSentPairNum);
1052 0 $count++;
1053 }
1054 0 $inputSentPairNum++;
1055 0 $internalSentPairNum++;
1056 } #while
1057 0 print "$count sentence pairs parsed in alignment set\n";
1058 0 closeFiles($newFH,$newFormat);
1059 0 closeFiles($FH,$alSet->{format});
1060 0 $alSet->updateObject($newFormat,$newLocation,$internalSentPairNum-1);
1061 }
1062  
1063 ######################################################################
1064 ### PRIVATE SUBS
1065 ######################################################################
1066  
1067 2 ▶sub readLocation{
Subroutine readLocation: called CC=2 SCAR=6.9
Pod undocumented
1068 2 my $location = shift;
1069
1070 2 ▶ if (!ref($location)){ #if it is a path, put it in a location hash
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $location) 1 1 2
1071 1 $location = {"sourceToTarget"=>$location}
1072 }
1073 2 return $location;
1074 }
1075  
1076 2 ▶sub setRange {
Subroutine setRange: called CC=8 SCAR=22.6
Pod undocumented
1077 2 my ($alSet,$range) = @_;
1078
1079 2 my @limits = split /-/, $range;
1080 2 my $numLimits = scalar(@limits);
1081 2 ▶ if ($numLimits == 0 || $numLimits >2){
Branch: 2 branches
BranchTrueFalseTotal
if ($numLimits == 0 or $numLimits > 2) { } 0 2 2
elsif ($numLimits == 1) { } 1 1 2
Condition33%1/3
$numLimits == 0 or $numLimits > 2
l!l&&r!l&&!r
002
MC/DC0%0/2
$numLimits == 0 or $numLimits > 2
$numLimits == 0 $numLimits > 2
Truth table33%1/3
A or B
A= $numLimits == 0B= $numLimits > 2
ABresult
1X1
011
000
1082 0 die "Invalid Range:$range\n";
1083 }elsif ($numLimits == 1){
1084 1 $limits[1]="";
1085 }
1086 2 $limits[0] =~ s/^\s+|\s+$//g;
1087 2 $limits[1] =~ s/^\s+|\s+$//g;
1088 2 ▶ if ($limits[0] !~ /\d+/ || $limits[0] == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (not $limits[0] =~ /\d+/ or $limits[0] == 0) { } 0 2 2
Condition33%1/3
not $limits[0] =~ /\d+/ or $limits[0] == 0
l!l&&r!l&&!r
002
MC/DC0%0/2
not $limits[0] =~ /\d+/ or $limits[0] == 0
not $limits[0] =~ /\d+/ $limits[0] == 0
Truth table33%1/3
not(A) or B
A= not $limits[0] =~ /\d+/B= $limits[0] == 0
ABresult
1X1
011
000
1089 0 $alSet->{firstSentPair}="1";
1090 }else{
1091 2 $alSet->{firstSentPair}=$limits[0];
1092 }
1093 2 ▶ if ($limits[1] !~ /\d+/ || $limits[1] == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (not $limits[1] =~ /\d+/ or $limits[1] == 0) { } 1 1 2
Condition66%2/3
not $limits[1] =~ /\d+/ or $limits[1] == 0
l!l&&r!l&&!r
101
MC/DC50%1/2
not $limits[1] =~ /\d+/ or $limits[1] == 0
not $limits[1] =~ /\d+/ $limits[1] == 0
Truth table66%2/3
not(A) or B
A= not $limits[1] =~ /\d+/B= $limits[1] == 0
ABresult
1X1
011
000
1094 1 $alSet->{lastSentPair}="eof";
1095 }else{
1096 1 $alSet->{lastSentPair}=$limits[1];
1097 }
1098 }
1099  
1100 #for future ease we save detailed infos contained in the source sample path
1101 #input: sourceToTarget dir (not optional), targetToSource dir (if exists), source path (optional) and target path (if necessary)
1102 #output: target (if not specified in input), sampleNum (sample number)
1103 1 ▶sub completeBlinkerLocation{
Subroutine completeBlinkerLocation: called CC=4 SCAR=18.6
Pod undocumented
1104 1 my $refToLocation = shift;
1105 1 my ($sourceLang,$targetLang);
1106  
1107 1 ▶ if ($refToLocation->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($refToLocation->{"source"}) 0 1 1
1108 0 my ($sourceDir,$sourceFileName)=split /\/([^\/]+)$/,$refToLocation->{source};
1109 0 ▶ if ($sourceFileName =~ /^(EN|FR)\.sample.\d+$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($sourceFileName =~ /^(EN|FR)\.sample.\d+$/) 0 0 0
1110 #extract the sample number and target file:
1111 0 my ($sourceLang,$nothing,$sampleNum) = split /\./,$sourceFileName;
1112 0 $refToLocation->{sampleNum} = $sampleNum;
1113 }
1114 }
1115 1 ▶ if (!$refToLocation->{sampleNum}){
Branch: 1 branch
BranchTrueFalseTotal
if (not $refToLocation->{"sampleNum"}) 1 0 1
1116 1 $refToLocation->{sampleNum} = 1;
1117 }
1118 }
1119  
1120 # open (for read or write) the files contained in a "location" hash (ex. at the {location} key of the alignment set hash)
1121 # if opens for write needs old location hash to check you won't delete the old format files
1122 # returns a ref to a hash containing the filehandle variables (hash with same keys as "location" except for Blinker format)
1123 2 ▶sub openLocation {
Subroutine openLocation: called CC=28 SCAR=56.9
Pod undocumented
1124 2 my ($location,$format,$openMode,$oldLocation) = @_; #oldLocation: optional parameter
1125 2 my %FH;
1126  
1127 2 ▶ if ($openMode eq ">"){
Branch: 1 branch
BranchTrueFalseTotal
if ($openMode eq ">") 0 2 2
1128 0 ▶ if ($format eq "BLINKER"){
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "BLINKER") 0 0 0
1129 0 completeBlinkerLocation($location);
1130 }
1131 # check that your new files are different to prevent from deleting the old ones
1132 0 my %oldFiles = reverse %$oldLocation;
1133 0 my ($key,$newFile);
1134 0 ▶ while (($key, $newFile)=each %$location){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$location) 0 0 0
1135 0 ▶ if ($oldFiles{$newFile} && $key ne "sampleNum"){
Branch: 1 branch
BranchTrueFalseTotal
if ($oldFiles{$newFile} and $key ne "sampleNum") 0 0 0
Condition0%0/3
$oldFiles{$newFile} and $key ne "sampleNum"
!ll&&!rl&&r
000
MC/DC0%0/2
$oldFiles{$newFile} and $key ne "sampleNum"
$oldFiles{$newFile} $key ne "sampleNum"
Truth table0%0/3
A and B
A= $oldFiles{$newFile}B= $key ne "sampleNum"
ABresult
0X0
100
111
1136 0 die "Convert function: you are opening for write one of the old format file: $newFile\n";
1137 }
1138 }
1139 #end of check
1140
1141 # create directory structure where to create the file/directory if it doesn't exist, create it
1142 0 my $type;
1143 0 ▶ if ($format eq "BLINKER"){$type = "dir"}
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'BLINKER') { } 0 0 0
1144 0 else {$type = "file"}
1145 0 createDirStructure($location->{sourceToTarget},$type);
1146 0 ▶ if ($location->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"targetToSource"}) 0 0 0
1147 0 createDirStructure($location->{targetToSource},$type);
1148 }
1149 0 ▶ if ($location->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"source"}) 0 0 0
1150 0 createDirStructure($location->{source},"file");
1151 }
1152 0 ▶ if ($location->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"target"}) 0 0 0
1153 0 createDirStructure($location->{target},"file");
1154 }
1155 #end create directory structure
1156 }
1157
1158 2 ▶ if ($format eq "GIZA"){
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'GIZA') { } 0 2 2
elsif ($format eq 'NAACL' or $format eq 'TALP') { } 1 1 2
elsif ($format eq 'BLINKER') { } 1 0 1
1159 0 ▶ $FH{sourceToTarget} = IO::File->new($openMode.$location->{sourceToTarget}) or die "GIZA file (".$location->{sourceToTarget}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"sourceToTarget"} = "IO::File"->new($openMode . $location->{"sourceToTarget"}) or die 'GIZA file (' . $location->{"sourceToTarget"} . ") opening error:$!" 0 0 0
1160 0 ▶ if ($location->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"targetToSource"}) 0 0 0
1161 0 ▶ $FH{targetToSource} = IO::File->new($openMode.$location->{targetToSource}) or die "GIZA file (".$location->{targetToSource}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"targetToSource"} = "IO::File"->new($openMode . $location->{"targetToSource"}) or die 'GIZA file (' . $location->{"targetToSource"} . ") opening error:$!" 0 0 0
1162 }
1163 ▶ } elsif ($format eq "NAACL" || $format eq "TALP"){
Condition66%2/3
$format eq "NAACL" or $format eq "TALP"
l!l&&r!l&&!r
101
MC/DC50%1/2
$format eq "NAACL" or $format eq "TALP"
$format eq "NAACL" $format eq "TALP"
Truth table66%2/3
A or B
A= $format eq "NAACL"B= $format eq "TALP"
ABresult
1X1
011
000
1164 1 ▶ if ($location->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"source"}) 1 0 1
1165 1 ▶ $FH{source} = IO::File->new($openMode.$location->{source}) or die "Source file (".$location->{source}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"source"} = "IO::File"->new($openMode . $location->{"source"}) or die 'Source file (' . $location->{"source"} . ") opening error:$!" 1 0 1
1166 }
1167 1 ▶ if ($location->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"target"}) 1 0 1
1168 1 ▶ $FH{target} = IO::File->new($openMode.$location->{target}) or die "Target file (".$location->{target}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"target"} = "IO::File"->new($openMode . $location->{"target"}) or die 'Target file (' . $location->{"target"} . ") opening error:$!" 1 0 1
1169 }
1170 1 ▶ $FH{sourceToTarget} = IO::File->new($openMode.$location->{sourceToTarget}) or die "Alignment file (".$location->{sourceToTarget}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"sourceToTarget"} = "IO::File"->new($openMode . $location->{"sourceToTarget"}) or die 'Alignment file (' . $location->{"sourceToTarget"} . ") opening error:$!" 1 0 1
1171 1 ▶ if ($location->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"targetToSource"}) 0 1 1
1172 0 ▶ $FH{targetToSource} = IO::File->new($openMode.$location->{targetToSource}) or die "Alignment file (".$location->{targetToSource}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"targetToSource"} = "IO::File"->new($openMode . $location->{"targetToSource"}) or die 'Alignment file (' . $location->{"targetToSource"} . ") opening error:$!" 0 0 0
1173 }
1174 } elsif ($format eq "BLINKER"){
1175 1 ▶ if ($location->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"source"}) 0 1 1
1176 0 ▶ $FH{source} = IO::File->new($openMode.$location->{source}) or die "BLINKER source file (".$location->{source}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"source"} = "IO::File"->new($openMode . $location->{"source"}) or die 'BLINKER source file (' . $location->{"source"} . ") opening error:$!" 0 0 0
1177 }
1178 1 ▶ if ($location->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($location->{"target"}) 0 1 1
1179 0 ▶ $FH{target} = IO::File->new($openMode.$location->{target}) or die "BLINKER source file (".$location->{target}.") opening error:$!";
Branch: 1 branch
BranchTrueFalseTotal
$FH{"target"} = "IO::File"->new($openMode . $location->{"target"}) or die 'BLINKER source file (' . $location->{"target"} . ") opening error:$!" 0 0 0
1180 }
1181 }
1182 2 return (\%FH);
1183 }
1184  
1185  
1186 # if you want to create a file of path "directory_structure/file", makes "directory_structure" if necessary.
1187 # if you want to create a directory of path "directory_structure", makes it if it doesn't exist
1188 # type is "dir" (if you want to create a directory) or "file" (a file)
1189 0 ▶sub createDirStructure {
Subroutine createDirStructure: not called CC=7 SCAR=40.3
Pod undocumented
1190 0 my ($path,$type)=@_;
1191
1192 0 ▶ if ($type eq "dir"){
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'dir') { } 0 0 0
elsif ($type eq 'file') { } 0 0 0
1193 0 ▶ unless(-e $path && -d _){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path and -d _) 0 0 0
Condition0%0/3
-e $path and -d _
!ll&&!rl&&r
000
MC/DC0%0/2
-e $path and -d _
-e $path -d _
Truth table0%0/3
A and B
A= -e $pathB= -d _
ABresult
0X0
100
111
1194 0 system('mkdir -p '.$path);
1195 }
1196 }elsif ($type eq "file"){
1197 0 $path =~ s/\/$//;
1198 0 ▶ if ($path =~ /\//){
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/]) 0 0 0
1199 0 my ($dir,$file)=split /\/[^\/]+$/,$path;
1200 0 ▶ unless (-e $dir){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dir) 0 0 0
1201 0 system('mkdir -p '.$dir);
1202 }
1203 }
1204 }
1205 }
1206  
1207 # open files of an alignment set for read and go to first sentence pair
1208 2 ▶sub openFiles {
Subroutine openFiles: called CC=26 SCAR=60.2
Pod undocumented
1209 2 my $alSet = shift;
1210 2 my %FH = %{openLocation($alSet->{location},$alSet->{format},"<")};
1211 2 my $fhPos;
1212 my $lineNb;
1213  
1214 # go to first Sentence pair:
1215 2 ▶ if ($alSet->{format} eq "TALP"){
Branch: 4 branches
BranchTrueFalseTotal
if ($alSet->{'format'} eq 'TALP') { } 0 2 2
elsif ($alSet->{'format'} eq 'GIZA') { } 0 2 2
elsif ($alSet->{'format'} eq 'NAACL') { } 1 1 2
elsif ($alSet->{'format'} eq 'BLINKER') { } 1 0 1
1216 0 ▶ for ($lineNb=$alSet->{firstSentPair}-1;$lineNb>0;$lineNb--){ #go to first Sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($lineNb > 0) 0 0 0
1217 0 ▶ if ($FH{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"source"}) 0 0 0
1218 0 $FH{source}->getline();
1219 }
1220 0 ▶ if ($FH{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"target"}) 0 0 0
1221 0 $FH{target}->getline();
1222 }
1223 0 ▶ if ($FH{sourceToTarget}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"sourceToTarget"}) 0 0 0
1224 0 $FH{sourceToTarget}->getline();
1225 }
1226 0 ▶ if ($FH{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"targetToSource"}) 0 0 0
1227 0 $FH{targetToSource}->getline();
1228 }
1229 }
1230 }elsif ($alSet->{format} eq "GIZA"){
1231 0 ▶ for ($lineNb=$alSet->{firstSentPair}-1;$lineNb>0;$lineNb--){ #go to first Sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($lineNb > 0) 0 0 0
1232 0 $FH{sourceToTarget}->getline();
1233 0 $FH{sourceToTarget}->getline();
1234 0 $FH{sourceToTarget}->getline();
1235 0 ▶ if ($FH{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"targetToSource"}) 0 0 0
1236 0 $FH{targetToSource}->getline();
1237 0 $FH{targetToSource}->getline();
1238 0 $FH{targetToSource}->getline();
1239 }
1240 }
1241 } elsif ($alSet->{format} eq "NAACL"){
1242 1 ▶ for ($lineNb=$alSet->{firstSentPair}-1;$lineNb>0;$lineNb--){
Branch: 1 branch
BranchTrueFalseTotal
if ($lineNb > 0) 0 1 1
1243 0 ▶ if ($FH{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"source"}) 0 0 0
1244 0 $FH{source}->getline();
1245 }
1246 0 ▶ if ($FH{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"target"}) 0 0 0
1247 0 $FH{target}->getline();
1248 }
1249 0 $fhPos = $FH{sourceToTarget}->getpos;
1250 0 ▶ while ($FH{sourceToTarget}->getline() !~ m/^0*$alSet->{firstSentPair} .*/ && !$FH{sourceToTarget}->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($FH{"sourceToTarget"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ or $FH{"sourceToTarget"}->eof) 0 0 0
Condition0%0/3
$FH{"sourceToTarget"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ or $FH{"sourceToTarget"}->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$FH{"sourceToTarget"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ or $FH{"sourceToTarget"}->eof
$FH{"sourceToTarget"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ $FH{"sourceToTarget"}->eof
Truth table0%0/3
A or B
A= $FH{"sourceToTarget"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/B= $FH{"sourceToTarget"}->eof
ABresult
1X1
011
000
1251 0 $fhPos = $FH{sourceToTarget}->getpos;
1252 }
1253 0 ▶ if ($FH{sourceToTarget}->eof()){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"sourceToTarget"}->eof) 0 0 0
1254 0 die "First sentence pair of range (number ".$alSet->{firstSentPair}.") not found in ".$alSet->{location}{sourceToTarget};
1255 }
1256 0 $FH{sourceToTarget}->setpos($fhPos); #if we changed sentences, we read the first line of next sentence=>go back one line
1257 0 ▶ if ($FH{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"targetToSource"}) 0 0 0
1258 0 $fhPos = $FH{targetToSource}->getpos;
1259 0 ▶ while ($FH{targetToSource}->getline() !~ m/^0*$alSet->{firstSentPair} .*/ && !$FH{targetToSource}->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($FH{"targetToSource"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ or $FH{"targetToSource"}->eof) 0 0 0
Condition0%0/3
$FH{"targetToSource"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ or $FH{"targetToSource"}->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$FH{"targetToSource"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ or $FH{"targetToSource"}->eof
$FH{"targetToSource"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/ $FH{"targetToSource"}->eof
Truth table0%0/3
A or B
A= $FH{"targetToSource"}->getline =~ /^0*$alSet->{"firstSentPair"} .*/B= $FH{"targetToSource"}->eof
ABresult
1X1
011
000
1260 0 $fhPos = $FH{targetToSource}->getpos;
1261 }
1262 0 ▶ if ($FH{targetToSource}->eof()){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"targetToSource"}->eof) 0 0 0
1263 0 die "First sentence pair of range (number ".$alSet->{firstSentPair}.") not found in ".$alSet->{location}{targetToSource};
1264 }
1265 0 $FH{targetToSource}->setpos($fhPos);
1266 }
1267 }
1268 } elsif ($alSet->{format} eq "BLINKER"){
1269 1 ▶ if ($FH{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"source"}) 0 1 1
1270 0 ▶ for ($lineNb=$alSet->{firstSentPair}-1;$lineNb>0;$lineNb--){
Branch: 1 branch
BranchTrueFalseTotal
if ($lineNb > 0) 0 0 0
1271 0 $FH{source}->getline();
1272 }
1273 }
1274 1 ▶ if ($FH{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH{"target"}) 0 1 1
1275 0 ▶ for ($lineNb=$alSet->{firstSentPair}-1;$lineNb>0;$lineNb--){
Branch: 1 branch
BranchTrueFalseTotal
if ($lineNb > 0) 0 0 0
1276 0 $FH{target}->getline();
1277 }
1278 }
1279 }
1280 2 return (\%FH);
1281 }
1282  
1283 # close the files contained in the hash at the {location} key of the alignment set hash
1284 2 ▶sub closeFiles {
Subroutine closeFiles: called CC=12 SCAR=31.4
Pod undocumented
1285 2 my ($FH,$format) = @_;
1286
1287 2 ▶ if ($format eq "GIZA"){
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'GIZA') { } 0 2 2
elsif ($format eq 'NAACL' or $format eq 'TALP') { } 1 1 2
elsif ($format eq 'BLINKER') { } 1 0 1
1288 0 $FH->{sourceToTarget}->close();
1289 0 ▶ if ($$FH{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->{"targetToSource"}) 0 0 0
1290 0 $FH->{targetToSource}->close();
1291 }
1292 ▶ } elsif ($format eq "NAACL" || $format eq "TALP"){
Condition66%2/3
$format eq "NAACL" or $format eq "TALP"
l!l&&r!l&&!r
101
MC/DC50%1/2
$format eq "NAACL" or $format eq "TALP"
$format eq "NAACL" $format eq "TALP"
Truth table66%2/3
A or B
A= $format eq "NAACL"B= $format eq "TALP"
ABresult
1X1
011
000
1293 1 ▶ if ($FH->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->{"source"}) 1 0 1
1294 1 $FH->{source}->close();
1295 }
1296 1 ▶ if ($FH->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->{"target"}) 1 0 1
1297 1 $FH->{target}->close();
1298 }
1299 1 ▶ if ($FH->{sourceToTarget}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->{"sourceToTarget"}) 1 0 1
1300 1 $FH->{sourceToTarget}->close();
1301 }
1302 1 ▶ if ($FH->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->{"targetToSource"}) 0 1 1
1303 0 $FH->{targetToSource}->close();
1304 }
1305 } elsif ($format eq "BLINKER"){
1306 1 ▶ if ($FH->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->{"source"}) 0 1 1
1307 0 $FH->{source}->close();
1308 }
1309 1 ▶ if ($FH->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->{"target"}) 0 1 1
1310 0 $FH->{target}->close();
1311 }
1312 }
1313 }
1314  
1315 # convert a chunk of alignment set file to an array of references to simple (1 sentence) alignment objects
1316 # returns 0 if the file is at eof
1317 22 ▶sub loadChunk {
Subroutine loadChunk: called CC=38 SCAR=57.4
Pod undocumented
1318 22 my ($alSet,$alFH,$sentPairNum,$alignMode) = @_;
1319 22 my ($sourceString,$targetString,$alString,$reverseAlString);
1320 22 my $st_alignments=[];
1321 22 my $ts_alignments=[];
1322 22 my $al;
1323 my $theEnd;
1324
1325 22 ▶ if (!defined($alignMode) || $alignMode =~ /^as.?is$/i){
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $alignMode or $alignMode =~ /^as.?is$/i) { } 0 22 22
elsif ($alignMode =~ /^null.?align$/i) { } 0 22 22
elsif ($alignMode =~ /^no.?null.?align$/i) { } 22 0 22
Condition33%1/3
not defined $alignMode or $alignMode =~ /^as.?is$/i
l!l&&r!l&&!r
0022
MC/DC0%0/2
not defined $alignMode or $alignMode =~ /^as.?is$/i
not defined $alignMode $alignMode =~ /^as.?is$/i
Truth table33%1/3
not(A) or B
A= not defined $alignModeB= $alignMode =~ /^as.?is$/i
ABresult
1X1
011
000
1326 0 $alignMode = "as-is";
1327 }elsif ($alignMode =~ /^null.?align$/i){
1328 0 $alignMode = "null-align";
1329 }elsif ($alignMode =~ /^no.?null.?align$/i){
1330 22 $alignMode = "no-null-align";
1331 }else{
1332 0 die 'Incorrect alignment mode. Correct modes are "as-is","null-align" or "no-null-align".'."\n";
1333 }
1334 22 ▶ if ($alSet->{format} eq "TALP"){
Branch: 4 branches
BranchTrueFalseTotal
if ($alSet->{'format'} eq 'TALP') { } 0 22 22
elsif ($alSet->{'format'} eq 'GIZA') { } 0 22 22
elsif ($alSet->{'format'} eq 'NAACL') { } 11 11 22
elsif ($alSet->{'format'} eq 'BLINKER') { } 11 0 11
1335 0 ▶ if ($alSet->{lastSentPair} eq "eof"){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{'lastSentPair'} eq 'eof') { } 0 0 0
1336 0 $theEnd = $$alFH{sourceToTarget}->eof();
1337 }else{
1338 0 ▶ $theEnd = ($$alFH{sourceToTarget}->eof() || $sentPairNum > $alSet->{lastSentPair});
Condition0%0/3
$alFH->{'sourceToTarget'}->eof || $sentPairNum > $alSet->{'lastSentPair'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$alFH->{'sourceToTarget'}->eof || $sentPairNum > $alSet->{'lastSentPair'}
$alFH->{'sourceToTarget'}->eof $sentPairNum > $alSet->{'lastSentPair'}
Truth table0%0/3
A || B
A= $alFH->{'sourceToTarget'}->eofB= $sentPairNum > $alSet->{'lastSentPair'}
ABresult
1X1
011
000
1339 }
1340 0 ▶ if ($theEnd){
Branch: 1 branch
BranchTrueFalseTotal
if ($theEnd) { } 0 0 0
1341 0 return 0;
1342 }else{
1343 0 ▶ if ($alFH->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"source"}) 0 0 0
1344 0 $sourceString = $alFH->{source}->getline();
1345 }
1346 0 ▶ if ($alFH->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"target"}) 0 0 0
1347 0 $targetString = $alFH->{target}->getline();
1348 }
1349 0 ▶ if ($alFH->{sourceToTarget}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"sourceToTarget"}) 0 0 0
1350 0 $alString = $alFH->{sourceToTarget}->getline();
1351 }
1352 0 ▶ if ($alFH->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"targetToSource"}) 0 0 0
1353 0 $reverseAlString = $alFH->{targetToSource}->getline();
1354 }
1355 0 $al = Lingua::Alignment->new;
1356 0 $al->loadFromTalp($alString,$reverseAlString,$sourceString,$targetString);
1357 }
1358 }elsif ($alSet->{format} eq "GIZA"){
1359 0 ▶ if ($alSet->{lastSentPair} eq "eof"){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{'lastSentPair'} eq 'eof') { } 0 0 0
1360 0 $theEnd = $$alFH{sourceToTarget}->eof();
1361 }else{
1362 0 ▶ $theEnd = ($$alFH{sourceToTarget}->eof() || $sentPairNum > $alSet->{lastSentPair});
Condition0%0/3
$alFH->{'sourceToTarget'}->eof || $sentPairNum > $alSet->{'lastSentPair'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$alFH->{'sourceToTarget'}->eof || $sentPairNum > $alSet->{'lastSentPair'}
$alFH->{'sourceToTarget'}->eof $sentPairNum > $alSet->{'lastSentPair'}
Truth table0%0/3
A || B
A= $alFH->{'sourceToTarget'}->eofB= $sentPairNum > $alSet->{'lastSentPair'}
ABresult
1X1
011
000
1363 }
1364 0 ▶ if ($theEnd){
Branch: 1 branch
BranchTrueFalseTotal
if ($theEnd) { } 0 0 0
1365 0 return 0;
1366 }else{
1367 0 $$alFH{sourceToTarget}->getline();
1368 0 $targetString = $$alFH{sourceToTarget}->getline();
1369 0 $alString = $$alFH{sourceToTarget}->getline();
1370 0 ▶ if ($$alFH{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"targetToSource"}) 0 0 0
1371 0 $$alFH{targetToSource}->getline();
1372 0 $$alFH{targetToSource}->getline();
1373 0 $reverseAlString = $$alFH{targetToSource}->getline();
1374 }
1375 0 $al = Lingua::Alignment->new;
1376 0 $al->loadFromGiza($alString,$targetString,$reverseAlString);
1377 }
1378 } elsif ($alSet->{format} eq "NAACL"){
1379 11 my $fhPos;
1380 11 ▶ if ($alSet->{lastSentPair} eq "eof"){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{'lastSentPair'} eq 'eof') { } 0 11 11
1381 0 $theEnd = $$alFH{sourceToTarget}->eof();
1382 }else{
1383 11 ▶ $theEnd = ($$alFH{sourceToTarget}->eof() || $sentPairNum > $alSet->{lastSentPair});
Condition66%2/3
$alFH->{'sourceToTarget'}->eof || $sentPairNum > $alSet->{'lastSentPair'}
l!l&&r!l&&!r
1010
MC/DC50%1/2
$alFH->{'sourceToTarget'}->eof || $sentPairNum > $alSet->{'lastSentPair'}
$alFH->{'sourceToTarget'}->eof $sentPairNum > $alSet->{'lastSentPair'}
Truth table66%2/3
A || B
A= $alFH->{'sourceToTarget'}->eofB= $sentPairNum > $alSet->{'lastSentPair'}
ABresult
1X1
011
000
1384 }
1385 11 ▶ if ($theEnd){
Branch: 1 branch
BranchTrueFalseTotal
if ($theEnd) { } 1 10 11
1386 1 return 0;
1387 }else{
1388 10 ▶ if ($$alFH{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"source"}) 10 0 10
1389 10 $sourceString = $$alFH{source}->getline();
1390 #strip tags and memorize snum:
1391 10 $sourceString =~ s/<s snum=\d+>(.*)<\/s>/$1/;
1392 }
1393 10 ▶ if ($$alFH{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"target"}) 10 0 10
1394 10 $targetString = $$alFH{target}->getline();
1395 #strip tags and memorize snum:
1396 10 $targetString =~ s/<s snum=(\d+)>(.*)<\/s>/$2/;
1397 }
1398 10 $fhPos = $$alFH{sourceToTarget}->getpos;
1399 10 $alString = $$alFH{sourceToTarget}->getline();
1400 10 my ($num,$theRest)=split " ",$alString,2;
1401 10 ▶ if ($num==$sentPairNum){ #skip if there is no link for this sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($num == $sentPairNum) 10 0 10
1402 10 $fhPos = $$alFH{sourceToTarget}->getpos;
1403 10 push @$st_alignments,$theRest;
1404 10 ▶ while ($$alFH{sourceToTarget}->getline() =~ m/^$sentPairNum (.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"sourceToTarget"}->getline =~ /^$sentPairNum (.*)$/) 58 10 68
1405 58 push @$st_alignments,$1;
1406 58 $fhPos = $$alFH{sourceToTarget}->getpos;
1407 }
1408 }
1409 10 $$alFH{sourceToTarget}->setpos($fhPos); #if we changed sentences, we read the first line of next sentence=>go back one line
1410 10 ▶ if ($$alFH{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"targetToSource"}) 0 10 10
1411 0 $fhPos = $$alFH{targetToSource}->getpos;
1412 0 $alString = $$alFH{targetToSource}->getline();
1413 0 my ($num,$theRest)=split " ",$alString,2;
1414 0 ▶ if ($num==$sentPairNum){ #skip if there is no link for this sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($num == $sentPairNum) 0 0 0
1415 0 $fhPos = $$alFH{targetToSource}->getpos;
1416 0 push @$ts_alignments,$theRest;
1417 0 ▶ while ($$alFH{targetToSource}->getline() =~ m/^$sentPairNum (.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"targetToSource"}->getline =~ /^$sentPairNum (.*)$/) 0 0 0
1418 0 push @$ts_alignments,$1;
1419 0 $fhPos = $$alFH{targetToSource}->getpos;
1420 }
1421 }
1422 0 $$alFH{targetToSource}->setpos($fhPos); #if we changed sentences, we read the first line of next sentence=>go back one line
1423 }
1424 10 $al = Lingua::Alignment->new;
1425 10 $al->loadFromBlinker($st_alignments,$ts_alignments,$sourceString,$targetString);
1426 }
1427 } elsif ($alSet->{format} eq "BLINKER"){
1428 11 ▶ if ($alSet->{lastSentPair} eq "eof"){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{'lastSentPair'} eq 'eof') { } 11 0 11
1429 11 $theEnd = !(-e $alSet->{location}->{sourceToTarget}."/samp".$alSet->{location}->{sampleNum}.".SentPair".($sentPairNum-1));
1430 }else{
1431 0 ▶ $theEnd = !(-e $alSet->{location}->{sourceToTarget}."/samp".$alSet->{location}->{sampleNum}.".SentPair".($sentPairNum-1)) || $sentPairNum > $alSet->{lastSentPair};
Condition0%0/3
!(-e $alSet->{'location'}{'sourceToTarget'} . '/samp' . $alSet->{'location'}{'sampleNum'} . '.SentPair' . ($sentPairNum - 1)) || $sentPairNum > $alSet->{'lastSentPair'}
l!l&&r!l&&!r
000
MC/DC0%0/2
!(-e $alSet->{'location'}{'sourceToTarget'} . '/samp' . $alSet->{'location'}{'sampleNum'} . '.SentPair' . ($sentPairNum - 1)) || $sentPairNum > $alSet->{'lastSentPair'}
!(-e $alSet->{'location'}{'sourceToTarget'} . '/samp' . $alSet->{'location'}{'sampleNum'} . '.SentPair' . ($sentPairNum - 1)) $sentPairNum > $alSet->{'lastSentPair'}
Truth table0%0/3
not(A) || B
A= !(-e $alSet->{'location'}{'sourceToTarget'} . '/samp' . $alSet->{'location'}{'sampleNum'} . '.SentPair' . ($sentPairNum - 1))B= $sentPairNum > $alSet->{'lastSentPair'}
ABresult
1X1
011
000
1432 }
1433 11 ▶ if ($theEnd){
Branch: 1 branch
BranchTrueFalseTotal
if ($theEnd) { } 1 10 11
1434 1 return 0;
1435 }else{
1436 10 ▶ if ($alFH->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"source"}) 0 10 10
1437 0 $sourceString = $alFH->{source}->getline();
1438 }
1439 10 ▶ if ($alFH->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alFH->{"target"}) 0 10 10
1440 0 $targetString = $alFH->{target}->getline();
1441 }
1442 10 open(AL,"< ".$alSet->{location}->{sourceToTarget}."/samp".$alSet->{location}->{sampleNum}.".SentPair".($sentPairNum-1));
1443 10 @$st_alignments = <AL>;
1444 10 close(AL);
1445 10 ▶ if ($alSet->{location}->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{"location"}{"targetToSource"}) 0 10 10
1446 0 open(AL,"< ".$alSet->{location}->{targetToSource}."/samp".$alSet->{location}->{sampleNum}.".SentPair".($sentPairNum-1));
1447 0 @$ts_alignments = <AL>;
1448 0 close(AL);
1449 }
1450 10 $al = Lingua::Alignment->new;
1451 10 $al->loadFromBlinker($st_alignments,$ts_alignments,$sourceString,$targetString);
1452 }
1453 }
1454 20 ▶ if ($alignMode eq "null-align"){
Branch: 2 branches
BranchTrueFalseTotal
if ($alignMode eq 'null-align') { } 0 20 20
elsif ($alignMode eq 'no-null-align') { } 20 0 20
1455 0 $al->forceNullAlign();
1456 }elsif ($alignMode eq "no-null-align"){
1457 20 $al->forceNoNullAlign();
1458 }
1459 20 return [$al];
1460 }
1461  
1462  
1463 0 ▶sub updateObject {
Subroutine updateObject: not called CC=9 SCAR=45.0
Pod undocumented
1464 0 my ($alSet,$newFormat,$newLocation,$lastSentPairNum)=@_;
1465 0 $alSet->{location}->{sourceToTarget}=$newLocation->{sourceToTarget};
1466 0 $alSet->{location}->{targetToSource}=$newLocation->{targetToSource};
1467 0 ▶ if ($newLocation->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newLocation->{'source'}) { } 0 0 0
1468 0 $alSet->{location}->{source}=$newLocation->{source};
1469 }else{
1470 0 ▶ if ($alSet->{firstSentPair} != 1 || $alSet->{format} ne $newFormat){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{"firstSentPair"} != 1 or $alSet->{"format"} ne $newFormat) 0 0 0
Condition0%0/3
$alSet->{"firstSentPair"} != 1 or $alSet->{"format"} ne $newFormat
l!l&&r!l&&!r
000
MC/DC0%0/2
$alSet->{"firstSentPair"} != 1 or $alSet->{"format"} ne $newFormat
$alSet->{"firstSentPair"} != 1 $alSet->{"format"} ne $newFormat
Truth table0%0/3
A or B
A= $alSet->{"firstSentPair"} != 1B= $alSet->{"format"} ne $newFormat
ABresult
1X1
011
000
1471 # in this case the numeration of the converted alignment file and that of the (not converted) source file will not correspond
1472 0 delete($alSet->{location}->{source});
1473 # warn "After converting into ",$newLocation->{sourceToTarget},", the numeration of the source words file",
1474 # " didn't correspond any more to that of the alignment file. So the 'source' entry has been removed from the location hash.";
1475 }
1476 }
1477 0 ▶ if ($newLocation->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newLocation->{'target'}) { } 0 0 0
1478 0 $alSet->{location}->{target}=$newLocation->{target};
1479 }else{
1480 0 ▶ if ($alSet->{firstSentPair} != 1 || $alSet->{format} ne $newFormat){
Branch: 1 branch
BranchTrueFalseTotal
if ($alSet->{"firstSentPair"} != 1 or $alSet->{"format"} ne $newFormat) 0 0 0
Condition0%0/3
$alSet->{"firstSentPair"} != 1 or $alSet->{"format"} ne $newFormat
l!l&&r!l&&!r
000
MC/DC0%0/2
$alSet->{"firstSentPair"} != 1 or $alSet->{"format"} ne $newFormat
$alSet->{"firstSentPair"} != 1 $alSet->{"format"} ne $newFormat
Truth table0%0/3
A or B
A= $alSet->{"firstSentPair"} != 1B= $alSet->{"format"} ne $newFormat
ABresult
1X1
011
000
1481 # in this case the numeration of the converted alignment file and that of the (not converted) source file will not correspond
1482 0 delete($alSet->{location}->{target});
1483 # warn "After converting into ",$newLocation->{sourceToTarget},", the numeration of the target words file ",
1484 # "didn't correspond any more to that of the alignment file. So the 'target' entry has been removed from the location hash.";
1485 }
1486 }
1487 0 $alSet->{format}=$newFormat;
1488 0 ▶ if ($newFormat eq "BLINKER"){
Branch: 2 branches
BranchTrueFalseTotal
if ($newFormat eq 'BLINKER') { } 0 0 0
elsif (exists $alSet->{'location'}{'sampleNum'}) { } 0 0 0
1489 0 $alSet->{location}->{sampleNum}=$newLocation->{sampleNum};
1490 }elsif(exists($alSet->{location}->{sampleNum})){
1491 0 delete($alSet->{location}->{sampleNum});
1492 }
1493 0 $alSet->{firstSentPair}=1;
1494 0 $alSet->{lastSentPair}=$lastSentPairNum;
1495 }
1496  
1497 # returns the alignment set, with a unique new file set that has the required location,format and range values.
1498 # TO DO: conversion to Giza++ format
1499 0 ▶sub convert {
Subroutine convert: not called CC=6 SCAR=37.4
Pod undocumented
1500 0 my ($alSet,$newLocation,$newFormat,$alignMode,$AlignmentSub)=@_;
1501 0 ▶ if (!defined($newFormat)){$newFormat="TALP"}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $newFormat) { } 0 0 0
1502 0 else {$newFormat = uc $newFormat}
1503 0 $newLocation = readLocation($newLocation);
1504 0 my $FH = $alSet->openFiles();
1505 0 my $newFH = openLocation($newLocation,$newFormat,">",$alSet->{location});
1506 0 my ($i,$al,$alSetChunk,$line,$lines);
1507 0 my $inputSentPairNum=$alSet->{firstSentPair};
1508 0 my $internalSentPairNum = 1;
1509 0 ▶ while ($alSetChunk = $alSet->loadChunk($FH,$inputSentPairNum,$alignMode)){ # returns 0 if eof or last sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($alSetChunk = $alSet->loadChunk($FH, $inputSentPairNum, $alignMode)) 0 0 0
1510 # print $inputSentPairNum."\n";
1511 0 ▶ for ($i=0;$i<@$alSetChunk;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$alSetChunk) 0 0 0
1512 0 $al = $$alSetChunk[$i];
1513 0 ▶ if (defined($AlignmentSub)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $AlignmentSub) 0 0 0
1514 #look if $AlignmentSub is a ref to an Array or a subroutine
1515 0 ▶ if (ref($AlignmentSub) eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $AlignmentSub eq 'ARRAY') { } 0 0 0
1516 0 my ($sub,@params) = @$AlignmentSub;
1517 0 $al->$sub(@params);
1518 }else{
1519 0 $al->$AlignmentSub();
1520 }
1521 }
1522 0 $al->output($FH,$newFormat,$newFH,$newLocation,$internalSentPairNum);
1523 } #for
1524 0 $inputSentPairNum++;
1525 0 $internalSentPairNum++;
1526 } #while
1527 0 closeFiles($newFH,$newFormat);
1528 0 closeFiles($FH,$alSet->{format});
1529 0 $alSet->updateObject($newFormat,$newLocation,$internalSentPairNum-1);
1530 }
1531  
1532  
1533 # returns the alignment set, with only manyToMany links (source and target words file don't change)
1534 # at the moment, only works for sourceToTarget alignment (sourceAl)
1535 0 ▶sub printManyToMany {
Subroutine printManyToMany: not called CC=10 SCAR=47.0
Pod undocumented
1536 0 my ($alSet,$newLocation,$newFormat)=@_;
1537 0 ▶ if (!defined($newFormat)){$newFormat="TALP"}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $newFormat) { } 0 0 0
1538 0 else {$newFormat = uc $newFormat}
1539 0 $newLocation = readLocation($newLocation);
1540 0 my $dumper=new Dumpvalue;
1541 0 my $FH = $alSet->openFiles();
1542 0 my $newFH = openLocation($newLocation,$newFormat,">",$alSet->{location});
1543 0 my ($i,$al,$alSetChunk,$line,$lines);
1544 0 my $inputSentPairNum=$alSet->{firstSentPair};
1545 0 my $internalSentPairNum = 1;
1546
1547 0 ▶ while ($alSetChunk = $alSet->loadChunk($FH,$inputSentPairNum,"no-null-align")){ # returns 0 if eof or last sentence pair
Branch: 1 branch
BranchTrueFalseTotal
if ($alSetChunk = $alSet->loadChunk($FH, $inputSentPairNum, "no-null-align")) 0 0 0
1548 0 ▶ for ($i=0;$i<@$alSetChunk;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$alSetChunk) 0 0 0
1549 0 $al = $$alSetChunk[$i];
1550 0 my $clone = $al->clone;
1551 # look for manyToMany links
1552 0 my $clusters = $al->getAlClusters;
1553 0 $al->{sourceAl}=[];
1554 # print $dumper->dumpValue($clusters);
1555 0 ▶ for (my $c=0;$c<@$clusters;$c++){
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @$clusters) 0 0 0
1556 0 ▶ if ( @{$clusters->[$c]{source}}>1 || @{$clusters->[$c]{target}}>1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$clusters->[$c]{"source"};} > 1 or @{$clusters->[$c]{"target"};} > 1) 0 0 0
Condition0%0/3
@{$clusters->[$c]{"source"};} > 1 or @{$clusters->[$c]{"target"};} > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$clusters->[$c]{"source"};} > 1 or @{$clusters->[$c]{"target"};} > 1
@{$clusters->[$c]{"source"};} > 1 @{$clusters->[$c]{"target"};} > 1
Truth table0%0/3
A or B
A= @{$clusters->[$c]{"source"};} > 1B= @{$clusters->[$c]{"target"};} > 1
ABresult
1X1
011
000
1557 # this is a many to many alignment
1558 0 foreach my $j (@{$clusters->[$c]{source}}){
1559 0 foreach my $k (@{$clusters->[$c]{target}}){
1560 0 ▶ if ($clone->isIn("sourceAl",$j,$k)){
Branch: 1 branch
BranchTrueFalseTotal
if ($clone->isIn("sourceAl", $j, $k)) 0 0 0
1561 0 push @{$al->{sourceAl}[$j]},$k;
1562 }
1563 }
1564 }
1565 }
1566 }
1567 0 $al->output($FH,$newFormat,$newFH,$newLocation,$internalSentPairNum);
1568 } #for
1569 0 $inputSentPairNum++;
1570 0 $internalSentPairNum++;
1571 } #while
1572 0 closeFiles($newFH,$newFormat);
1573 0 closeFiles($FH,$alSet->{format});
1574 0 $alSet->updateObject($newFormat,$newLocation,$internalSentPairNum-1);
1575 }
1576  
1577  
1578 # identifies a link as sure or possible
1579 # input: a Naacl-file line containing the link and refs to sure and possible hashes
1580 # action: add to the relevant hash a key corresponding to this link
1581 149 ▶sub identifySurePossible{
Subroutine identifySurePossible: called CC=14 SCAR=42.5
Pod undocumented
1582 149 my ($line,$sure,$possible)=@_;
1583 149 my @components;
1584 my $alignment;
1585
1586 # print "line:$line\n";
1587
1588 #code adapted from Rada Mihalcea's wa_eval_align.pl, rada@cs.unt.edu
1589 # get all line components: format should be
1590 # sentence_no position_L1 position_L2 [S|P] [confidence]
1591 149 @components = split /\s+/, $line;
1592
1593 149 ▶ if(scalar(@components) < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @components < 3) 0 149 149
1594 0 print STDERR "Incorrect format in answer file\n";
1595 0 exit;
1596 }
1597
1598 149 $alignment = $components[0]." ".$components[1]." ".$components[2];
1599
1600 # identify the S[ure] alignments
1601 149 ▶ if( scalar (@components) == 3 || (scalar (@components) == 4 && ($components[3] =~ /^[\d\.]+$/ || $components[3] eq 'S')) ||
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @components == 3 or scalar @components == 4 and $components[3] =~ /^[\d\.]+$/ || $components[3] eq "S" or scalar @components == 5 and $components[3] eq "S" || $components[4] eq "S") 149 0 149
Condition11%2/18
scalar @components == 3 or scalar @components == 4 and $components[3] =~ /^[\d\.]+$/ || $components[3] eq "S" or scalar @components == 5 and $components[3] eq "S" || $components[4] eq "S"
l!l&&r!l&&!r
14900
scalar @components == 3 or scalar @components == 4 and $components[3] =~ /^[\d\.]+$/ || $components[3] eq "S"
l!l&&r!l&&!r
14900
scalar @components == 4 and $components[3] =~ /^[\d\.]+$/ || $components[3] eq "S"
!ll&&!rl&&r
000
$components[3] =~ /^[\d\.]+$/ or $components[3] eq "S"
l!l&&r!l&&!r
000
scalar @components == 5 and $components[3] eq "S" || $components[4] eq "S"
!ll&&!rl&&r
000
$components[3] eq "S" or $components[4] eq "S"
l!l&&r!l&&!r
000
MC/DC0%0/7
scalar @components == 3 or scalar @components == 4 and $components[3] =~ /^[\d\.]+$/ || $components[3] eq "S" or scalar @components == 5 and $components[3] eq "S" || $components[4] eq "S"
scalar @components == 3 scalar @components == 4 $components[3] =~ /^[\d\.]+$/ $components[3] eq "S" scalar @components == 5 $components[3] eq "S" $components[4] eq "S"
Truth table9%1/11
A or B and C or D or E and F or G
A= scalar @components == 3B= scalar @components == 4C= $components[3] =~ /^[\d\.]+$/D= $components[3] eq "S"E= scalar @components == 5F= $components[3] eq "S"G= $components[4] eq "S"
ABCDEFGresult
1XXXXXX1
011XXXX1
0101XXX1
00XX11X1
00XX1011
010011X1
01001011
00XX0XX0
00XX1000
01000XX0
01001000
1602 (scalar (@components) == 5 && ($components[3] eq 'S' || $components[4] eq 'S'))) {
1603 149 $sure->{$alignment} = 1;
1604 }
1605
1606 # identify the P[robable] alignments
1607 149 ▶ if( (scalar (@components) == 4 && $components[3] eq 'P') || (scalar (@components) == 5 &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @components == 4 and $components[3] eq "P" or scalar @components == 5 and $components[3] eq "P" || $components[4] eq "P") 0 149 149
Condition25%3/12
scalar @components == 4 and $components[3] eq "P" or scalar @components == 5 and $components[3] eq "P" || $components[4] eq "P"
l!l&&r!l&&!r
00149
scalar @components == 4 and $components[3] eq "P"
!ll&&!rl&&r
14900
scalar @components == 5 and $components[3] eq "P" || $components[4] eq "P"
!ll&&!rl&&r
14900
$components[3] eq "P" or $components[4] eq "P"
l!l&&r!l&&!r
000
MC/DC0%0/5
scalar @components == 4 and $components[3] eq "P" or scalar @components == 5 and $components[3] eq "P" || $components[4] eq "P"
scalar @components == 4 $components[3] eq "P" scalar @components == 5 $components[3] eq "P" $components[4] eq "P"
Truth table11%1/9
A and B or C and D or E
A= scalar @components == 4B= $components[3] eq "P"C= scalar @components == 5D= $components[3] eq "P"E= $components[4] eq "P"
ABCDEresult
11XXX1
0X11X1
0X1011
1011X1
101011
0X0XX0
0X1000
100XX0
101000
1608 ($components[3] eq 'P' || $components[4] eq 'P'))) {
1609 0 $possible->{$alignment} = 1;
1610 }
1611 }
1612  
1613 1 1;
1614 __END__
« AlignmentSlice.pm ↑ Summary Alignment.pm »