Alignment.pm

filter: Statementstmt 8.0% 84 / 1050 Branchbran 4.7% 21 / 440 Conditioncond 0.0% 0 / 58 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 22.5% 9 / 40 Podpod 0.0% 0 / 36 totaltotal 6.8% 114 / 1660 CC 293 SCAR 58.6
CC
293
Cov
7%
cut
1260.0
selectSideWithLinks
600.0
output
552.0
CRAP 351.1
« AlignmentSet.pm ↑ Summary AlSetLib.pm »
1  
2 ########################################################################
3 # Author: Patrik Lambert (lambert@talp.ucp.es)
4 # Contributions from Adria de Gispert (agispert@gps.tsc.upc.es)
5 # and Josep Maria Crego (jmcrego@gps.tsc.upc.es)
6 # Description: Library of tools to process a set of links between the
7 # words of two sentences.
8 #
9 #-----------------------------------------------------------------------
10 #
11 # Copyright 2004 by Patrik Lambert
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ########################################################################
27  
28 package Lingua::Alignment;
29 1 $VERSION=1.1;
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Lingua::AlignmentSlice;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Lingua::AlSetLib 1.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Dumpvalue;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 #an alignment is a hash with 4 components:
36 # {sourceAl} ref to source position array, each position containing the array of aligned target positions.
37 # Each linked target token is indicated with the array: (position,S(sure)/P(possible),confidence score)
38 # {targetAl} same as sourceAl but reversed
39 # {sourceWords} and {targetWords}: array of corresponding words
40 # {sourceLinks}: hash (indexed by the source token position $j and target $i in the link: {$j $i} of arrays giving
41 # {targetLinks}: same as sourceLinks, for target alignment
42 # more information about the link: ( S(sure) or P(possible) , confidence )
43 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
44 20 my $pkg = shift;
45 20 my $al = {};
46
47 20 $al->{sourceAl}=[];
48 20 $al->{targetAl}=[];
49 20 $al->{sourceWords} = [];
50 20 $al->{targetWords} = [];
51 20 $al->{sourceLinks} = {};
52 20 $al->{targetLinks} = {};
53 20 return bless $al,$pkg;
54 }
55  
56 0 ▶sub loadFromGiza {
Subroutine loadFromGiza: not called CC=5 SCAR=34.0
Pod undocumented
57 0 my ($al,$alignmentString,$targetString,$reverseAlignmentString) = @_;
58 0 my ($i,$elem,$positionsString);
59
60 #TARGET
61 0 $targetString =~ s/^\s+//; #trim
62 0 $targetString =~ s/\s+$//; #trim
63 0 $targetString =~ s/\s{2,}/ /g; #remove multiple spaces
64 0 ▶ if ($targetString !~ /^NULL /){
Branch: 1 branch
BranchTrueFalseTotal
unless ($targetString =~ /^NULL /) 0 0 0
65 0 $al->{targetWords}=["NULL"]; #we keep a place for the NULL word of the other direction
66 }
67 0 push @{$al->{targetWords}},split(/ /,$targetString);
68  
69 0 $alignmentString =~ s/\s{2,}/ /g; #remove multiple spaces
70  
71 #SOURCE
72 0 my $srcString = $alignmentString;
73 0 $srcString =~ s/ \(\{[^\}]+\}\)//g;
74 0 $srcString =~ s/^\s+//; $srcString =~ s/\s+$//;
75 0 @{$al->{sourceWords}}=split / /,$srcString;
76  
77 #S2T LINKS
78 # here you can't use a hash because you would loose the order
79 0 $_ = $alignmentString;
80 0 my @correspondances = /\(\{(.+?)\}\)/g; #take what is between parentesis ie links
81 0 foreach my $positionsString (@correspondances){
82 0 $positionsString =~ s/^\s+//; #trim
83 0 $positionsString =~ s/\s+$//; #trim
84 0 push @{$al->{sourceAl}}, [split / /,$positionsString];
85 }
86  
87 #REVERSE ALIGNMENT
88 0 ▶ if (length($reverseAlignmentString)>0){
Branch: 1 branch
BranchTrueFalseTotal
if (length $reverseAlignmentString > 0) 0 0 0
89 0 $reverseAlignmentString =~ s/\(\{ \}\)/\(\{ \}\)/g; #insert blanks in unlinked words
90 0 $reverseAlignmentString =~ s/\}\)\s*$//g; #rtrim
91
92 0 @correspondances = split /\(\{\s|\}\)\s/, $reverseAlignmentString;
93 0 ▶ for ($i=0;$i<@correspondances;$i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @correspondances) 0 0 0
94 0 $positionsString = $correspondances[$i+1];
95 0 $positionsString =~ s/^\s+|\s+$//g; #trim
96 0 $positionsString =~ s/\s{2,}/ /g; #remove multiple spaces
97 0 push @{$al->{targetAl}}, [split / /,$positionsString];
98 }
99 }
100 }
101  
102 #input: $refToAlignedPairs_ts (target to source),$sourceSentence and $targetSentence are optional
103 20 ▶sub loadFromBlinker{
Subroutine loadFromBlinker: called CC=15 SCAR=33.7
Pod undocumented
104 20 my ($al,$refToAlignedPairs_st,$refToAlignedPairs_ts,$sourceSentence,$targetSentence)=@_;
105 20 my $i;
106 my $pairStr;
107 20 my @pair;
108 20 my @pairs;
109  
110 #LOAD SENTENCES (if applicable)
111 20 ▶ if (defined($sourceSentence)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sourceSentence) 10 10 20
112 10 $sourceSentence =~ s/^\s+|\s+$//g; #trim
113 10 $sourceSentence =~ s/\s{2,}/ /g; #remove multiple space
114
115 10 ▶ if ($sourceSentence !~ /^NULL /){
Branch: 1 branch
BranchTrueFalseTotal
unless ($sourceSentence =~ /^NULL /) 0 10 10
116 0 $al->{sourceWords}=["NULL"];
117 }
118 10 push @{$al->{sourceWords}},split(/ /,$sourceSentence);
119 }
120 20 ▶ if (defined($targetSentence)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $targetSentence) 10 10 20
121 10 $targetSentence =~ s/^\s+|\s+$//g;
122 10 $targetSentence =~ s/\s{2,}/ /g;
123
124 10 ▶ if ($targetSentence !~ /^NULL /){
Branch: 1 branch
BranchTrueFalseTotal
unless ($targetSentence =~ /^NULL /) 0 10 10
125 0 $al->{targetWords}=["NULL"];
126 }
127 10 push @{$al->{targetWords}},split(/ /,$targetSentence);
128 }
129
130 #LOAD SOURCE TO TARGET ALIGNMENT:
131 #read alignment data
132 20 foreach $pairStr (@$refToAlignedPairs_st){
133 165 $pairStr =~ s/^\s+|\s+$//g; #trim
134 165 $pairStr =~ s/\s{2,}/ /g; #remove multiple space
135 165 @pair = split / /,$pairStr;
136 165 push @{$pairs[$pair[0]]},$pair[1];
137 #load extra information (like S/P, confidence)
138 165 ▶ if (@pair > 2){
Branch: 1 branch
BranchTrueFalseTotal
if (@pair > 2) 0 165 165
139 0 $al->{sourceLinks}->{$pair[0]." ".$pair[1]}=[splice(@pair,2)] ;
140 }
141 }
142 # take into account unaligned words to have no undef entry in array:
143 # Since we really want to think in terms of alignment and not words, we don't base ourself on the number of words
144 20 ▶ for ($i=0;$i<@pairs;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pairs) 152 20 172
145 152 ▶ if (defined($pairs[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pairs[$i]) { } 124 28 152
146 124 push @{$al->{sourceAl}},$pairs[$i];
147 }else{
148 28 push @{$al->{sourceAl}},[];
149 }
150 }
151 # print main::Dumper($refToAlignedPairs_st,$al->{sourceAl});
152  
153 #LOAD TARGET TO SOURCE ALIGNMENT:
154 20 ▶ if (defined($refToAlignedPairs_ts)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refToAlignedPairs_ts) 20 0 20
155 20 ▶ if (@$refToAlignedPairs_ts>0){
Branch: 1 branch
BranchTrueFalseTotal
if (@$refToAlignedPairs_ts > 0) 0 20 20
156 0 @pairs=();
157 #read alignment data
158 0 foreach $pairStr (@$refToAlignedPairs_ts){
159 0 $pairStr =~ s/^\s+|\s+$//g; #trim
160 0 $pairStr =~ s/\s{2,}/ /g; #remove multiple space
161 0 @pair = split / /,$pairStr;
162 0 push @{$pairs[$pair[0]]},$pair[1];
163 #load extra information (like S/P, confidence)
164 0 ▶ if (@pair > 2){
Branch: 1 branch
BranchTrueFalseTotal
if (@pair > 2) 0 0 0
165 0 $al->{targetLinks}->{$pair[0]." ".$pair[1]}=[splice(@pair,2)] ;
166 }
167 }
168 # take into account unaligned words to have no undef entry in array:
169 0 ▶ for ($i=0;$i<@pairs;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pairs) 0 0 0
170 0 ▶ if (defined($pairs[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pairs[$i]) { } 0 0 0
171 0 push @{$al->{targetAl}},$pairs[$i];
172 }else{
173 0 push @{$al->{targetAl}},[];
174 }
175 }
176 }
177 }
178 # print main::Dumper($refToAlignedPairs_ts,$al->{targetAl});
179 }
180  
181 0 ▶sub loadFromTalp{
Subroutine loadFromTalp: not called CC=19 SCAR=59.4
Pod undocumented
182 0 my ($al,$st_string,$ts_string,$sourceSentence,$targetSentence)=@_;
183  
184 #LOAD SENTENCES (if applicable)
185 0 ▶ if (defined($sourceSentence)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sourceSentence) 0 0 0
186 0 $sourceSentence =~ s/^\s+//g; #trim
187 0 $sourceSentence =~ s/\s+$//g; #trim
188 0 $sourceSentence =~ s/\s{2,}/ /g; #remove multiple space
189 0 ▶ if ($sourceSentence !~ /^NULL /){
Branch: 1 branch
BranchTrueFalseTotal
unless ($sourceSentence =~ /^NULL /) 0 0 0
190 0 $al->{sourceWords}=["NULL"];
191 }
192 0 push @{$al->{sourceWords}},split(/ /,$sourceSentence);
193 }
194 0 ▶ if (defined($targetSentence)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $targetSentence) 0 0 0
195 0 $targetSentence =~ s/^\s+//g;
196 0 $targetSentence =~ s/\s+$//g;
197 0 $targetSentence =~ s/\s{2,}/ /g;
198 0 ▶ if ($targetSentence !~ /^NULL /){
Branch: 1 branch
BranchTrueFalseTotal
unless ($targetSentence =~ /^NULL /) 0 0 0
199 0 $al->{targetWords}=["NULL"];
200 }
201 0 push @{$al->{targetWords}},split(/ /,$targetSentence);
202 }
203
204 #LOAD SOURCE TO TARGET ALIGNMENT:
205 0 ▶ if ($st_string ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($st_string ne "") 0 0 0
206 0 my @pairs;
207 0 $st_string =~ s/\s{2,}/ /g; #remove multiple space
208 0 $st_string =~ s/^\s+//g; #trim
209 0 $st_string =~ s/\s+$//g; #trim
210 #read alignment data
211 0 my @lnks=split (/ /,$st_string);
212 0 foreach my $pairStr (@lnks){
213 0 my @info = split /:/,$pairStr;
214 0 my ($src,$sep,$trg) = split /([^\d])/,$info[0];
215 0 push @{$pairs[$src]},$trg;
216 #load extra information (like S/P, confidence)
217 0 ▶ if ($sep eq "s"){
Branch: 2 branches
BranchTrueFalseTotal
if ($sep eq 's') { } 0 0 0
elsif ($sep eq 'p') { } 0 0 0
218 0 $al->{sourceLinks}->{$src." ".$trg}=["S"];
219 }elsif ($sep eq "p" ){
220 0 $al->{sourceLinks}->{$src." ".$trg}=["P"];
221 }
222 0 ▶ for (my $i=1;$i<@info;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @info) 0 0 0
223 0 push @{$al->{sourceLinks}->{$src." ".$trg}},$info[$i];
224 }
225 }
226 # take into account unaligned words to have no undef entry in array:
227 # Since we really want to think in terms of alignment and not words, we don't base ourself on the number of words
228 0 ▶ for (my $i=0;$i<@pairs;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pairs) 0 0 0
229 0 ▶ if (defined($pairs[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pairs[$i]) { } 0 0 0
230 0 push @{$al->{sourceAl}},$pairs[$i];
231 }else{
232 0 push @{$al->{sourceAl}},[];
233 }
234 }
235 }
236 # print main::Dumper($refToAlignedPairs_st,$al->{sourceAl});
237 0 my $refToAlignedPairs_ts;
238 my $pairStr;
239 0 my @pair;
240 #LOAD TARGET TO SOURCE ALIGNMENT:
241 0 ▶ if ($ts_string ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($ts_string ne "") 0 0 0
242 0 $ts_string =~ s/^\s+|\s+$//g; #trim
243 0 $ts_string =~ s/\s{2,}/ /g; #remove multiple space
244 0 my @pairs=();
245 #read alignment data
246 0 my @lnks=split (/ /,$ts_string);
247 0 foreach my $pairStr (@lnks){
248 0 my @info = split /:/,$pairStr;
249 0 my ($src,$sep,$trg) = split /([^\d])/,$info[0];
250 0 push @{$pairs[$src]},$trg;
251 #load extra information (like S/P, confidence)
252 0 ▶ if ($sep eq "s"){
Branch: 2 branches
BranchTrueFalseTotal
if ($sep eq 's') { } 0 0 0
elsif ($sep eq 'p') { } 0 0 0
253 0 $al->{targetLinks}->{$src." ".$trg}=["S"];
254 }elsif ($sep eq "p" ){
255 0 $al->{targetLinks}->{$src." ".$trg}=["P"];
256 }
257 0 ▶ for (my $i=1;$i<@info;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @info) 0 0 0
258 0 push @{$al->{targetLinks}->{$src." ".$trg}},$info[$i];
259 }
260 }
261 # take into account unaligned words to have no undef entry in array:
262 0 ▶ for (my $i=0;$i<@pairs;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pairs) 0 0 0
263 0 ▶ if (defined($pairs[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pairs[$i]) { } 0 0 0
264 0 push @{$al->{targetAl}},$pairs[$i];
265 }else{
266 0 push @{$al->{targetAl}},[];
267 }
268 }
269 }
270 # print main::Dumper($refToAlignedPairs_ts,$al->{targetAl});
271 }
272  
273 # sourceSentence: returns the target sentence tokens without NULL word (separated by " "), by parsing the alignment object
274 0 ▶sub sourceSentence {
Subroutine sourceSentence: not called CC=1 SCAR=6.9
Pod undocumented
275 0 my $al = shift;
276 0 my @sentence=@{$al->{sourceWords}};
277 0 shift @sentence;
278 0 return join " ",@sentence;
279 }
280  
281 # TargetSentence: returns the target sentence tokens without NULL word (separated by " "), by parsing the alignment object
282 0 ▶sub targetSentence {
Subroutine targetSentence: not called CC=1 SCAR=6.9
Pod undocumented
283 0 my $al = shift;
284 0 my @sentence=@{$al->{targetWords}};
285 0 shift @sentence;
286 0 return join " ",@sentence;
287 }
288  
289 # Remove links to NULL.
290 # Note: to do this we need the alignment to be loaded so we do it in a separate function
291 20 ▶sub forceNoNullAlign {
Subroutine forceNoNullAlign: called CC=7 SCAR=26.8
Pod undocumented
292 20 my $al = shift;
293 20 my ($j,$i);
294 20 my $continue;
295 20 my $source;
296 20 my @sides=("source","target");
297
298 20 foreach $source (@sides){
299 40 $al->{$source."Al"}[0]=[];
300 40 ▶ for ($j=1;$j<@{$al->{$source."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$source . 'Al'};}) 132 40 172
301 132 ▶ if ($al->isIn($source."Al",$j,0)){
Branch: 1 branch
BranchTrueFalseTotal
if ($al->isIn($source . 'Al', $j, 0)) 0 132 132
302 0 $continue=1;
303 0 ▶ for ($i=0;$i<@{$al->{$source."Al"}[$j]} && $continue;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$$al{$source . 'Al'}[$j];} and $continue) 0 0 0
Condition0%0/3
$i < @{$$al{$source . 'Al'}[$j];} and $continue
!ll&&!rl&&r
000
MC/DC0%0/2
$i < @{$$al{$source . 'Al'}[$j];} and $continue
$i < @{$$al{$source . 'Al'}[$j];} $continue
Truth table0%0/3
A and B
A= $i < @{$$al{$source . 'Al'}[$j];}B= $continue
ABresult
0X0
100
111
304 0 ▶ if ($al->{$source."Al"}[$j][$i]==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($$al{$source . 'Al'}[$j][$i] == 0) 0 0 0
305 0 splice(@{$al->{$source."Al"}[$j]}, $i, 1);
306 0 $continue=0;
307 }
308 }
309 }
310 }
311 } #foreach
312 }
313  
314 # Link to NULL with a P (Possible) alignment all words that are not linked to anything
315 0 ▶sub forceNullAlign {
Subroutine forceNullAlign: not called CC=9 SCAR=45.0
Pod undocumented
316 0 my $al = shift;
317 0 my ($j,$i);
318 0 my @reverseAl;
319 0 my $source;
320 0 my @sides=("source","target");
321
322 0 foreach $source (@sides){
323 0 @reverseAl = ();
324 0 ▶ for ($j=1;$j<@{$al->{$source."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$source . 'Al'};}) 0 0 0
325 0 ▶ if (@{$al->{$source."Al"}[$j]}==0){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$al{$source . 'Al'}[$j];} == 0) { } 0 0 0
326 0 push @{$al->{$source."Al"}[$j]},0;
327 0 $al->{$source."Links"}->{"$j 0"}= ["P"];
328 }else{
329 0 foreach $i (@{$al->{$source."Al"}[$j]}){
330 0 push @{$reverseAl[$i]},$j;
331 }
332 }
333 }
334 0 ▶ for ($i=1;$i<@reverseAl;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @reverseAl) 0 0 0
335 0 ▶ if (!defined($reverseAl[$i]) || @{$reverseAl[$i]}==0){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $reverseAl[$i] or @{$reverseAl[$i];} == 0) 0 0 0
Condition0%0/3
not defined $reverseAl[$i] or @{$reverseAl[$i];} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $reverseAl[$i] or @{$reverseAl[$i];} == 0
not defined $reverseAl[$i] @{$reverseAl[$i];} == 0
Truth table0%0/3
not(A) or B
A= not defined $reverseAl[$i]B= @{$reverseAl[$i];} == 0
ABresult
1X1
011
000
336 0 ▶ if (!$al->isIn($source."Al",0,$i)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($al->isIn($source . 'Al', 0, $i)) 0 0 0
337 0 push @{$al->{$source."Al"}[0]},$i;
338 0 $al->{$source."Links"}->{"0 $i"}= ["P"];
339 }
340 }
341 }
342 } #foreach
343 }
344  
345 20 ▶sub writeToBlinker{
Subroutine writeToBlinker: called CC=5 SCAR=16.4
Pod undocumented
346 20 my $al = shift;
347 20 my $side = shift; #optional; default:"source";
348 20 ▶ if (!defined($side)){$side="source"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $side) 20 0 20
349 20 my @lines = ();
350 20 my ($i,$j);
351  
352 20 ▶ for ($j=0;$j<@{$al->{$side."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$side . 'Al'};}) 152 20 172
353 152 foreach $i (@{$al->{$side."Al"}[$j]}){
354 149 ▶ if (${$al->{$side."Links"}}{"$j $i"}){
Branch: 1 branch
BranchTrueFalseTotal
if (${$$al{$side . 'Links'};}{"$j $i"}) { } 0 149 149
355 0 push @lines,"$j $i ".join(" ",@{$al->{$side."Links"}{"$j $i"}});
356 }else{
357 149 push @lines,"$j $i";
358 }
359 }
360 }
361 20 return \@lines;
362 }
363  
364 0 ▶sub writeToGiza{
Subroutine writeToGiza: not called CC=5 SCAR=34.0
Pod undocumented
365 0 my $al = shift;
366 0 my $side = shift; #optional; default:"source";
367  
368 # first line
369 0 my @lines = ();
370 0 push @lines,"#\n";
371  
372 # second line
373 0 my $invSide;
374 0 ▶ if (!defined($side)){$side="source"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $side) 0 0 0
375 0 ▶ if ($side eq "source"){
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq 'source') { } 0 0 0
376 0 $invSide="target";
377 0 push @lines,$al->targetSentence."\n";
378 }else{
379 0 $invSide="source";
380 0 push @lines,$al->sourceSentence."\n";
381 }
382
383 # third line
384 0 my $linksStr="";
385 0 ▶ for (my $j=0;$j<@{$al->{$side."Words"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$side . 'Words'};}) 0 0 0
386 0 $linksStr.=$al->{$side."Words"}->[$j].' ({ ';
387 0 foreach my $i (@{$al->{$side."Al"}[$j]}){
388 0 $linksStr.="$i ";
389 }
390 0 $linksStr.='}) ';
391 }
392 0 $linksStr =~ s/\s+$//;
393 0 $linksStr.="\n";
394 0 push @lines,$linksStr;
395 # print "GIZA OUTPUT:\n",join("\n",@lines);
396 0 return join("",@lines);
397 }
398  
399 0 ▶sub writeToTalp{
Subroutine writeToTalp: not called CC=6 SCAR=37.4
Pod undocumented
400 0 my $al = shift;
401 0 my $side = shift; #optional; default:"source";
402 0 ▶ if (!defined($side)){$side="source"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $side) 0 0 0
403 0 my @lines = ();
404 0 my ($i,$j);
405
406 0 ▶ for ($j=0;$j<@{$al->{$side."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$side . 'Al'};}) 0 0 0
407 0 foreach $i (@{$al->{$side."Al"}[$j]}){
408 0 ▶ if (${$al->{$side."Links"}}{"$j $i"}){
Branch: 1 branch
BranchTrueFalseTotal
if (${$$al{$side . 'Links'};}{"$j $i"}) { } 0 0 0
409 0 my $lk="$j".lc(${$al->{$side."Links"}{"$j $i"}}[0])."$i";
410 0 ▶ for (my $k=1;$k<@{$al->{$side."Links"}{"$j $i"}};$k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @{$$al{$side . 'Links'}{"$j $i"};}) 0 0 0
411 0 $lk.=":".${$al->{$side."Links"}{"$j $i"}}[$k];
412 }
413 0 push @lines,$lk;
414 }else{
415 0 push @lines,$j."-".$i;
416 }
417 }
418 }
419 0 return join(" ",@lines);
420 }
421  
422 0 ▶sub output {
Subroutine output: not called CC=23 SCAR=63.1
Pod undocumented
423 0 my ($al,$FH,$newFormat,$newFH,$newLocation,$internalSentPairNum)=@_;
424 0 my $dumper = new Dumpvalue;
425 0 ▶ if ($newFormat eq "TALP"){
Branch: 4 branches
BranchTrueFalseTotal
if ($newFormat eq 'TALP') { } 0 0 0
elsif ($newFormat eq 'NAACL') { } 0 0 0
elsif ($newFormat eq 'GIZA') { } 0 0 0
elsif ($newFormat eq 'BLINKER') { } 0 0 0
426 0 ▶ if ($newFH->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"source"}) 0 0 0
427 0 $newFH->{source}->print($al->sourceSentence."\n");
428 }
429 0 ▶ if ($newFH->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"target"}) 0 0 0
430 0 $newFH->{target}->print($al->targetSentence."\n");
431 }
432 0 ▶ if ($newFH->{sourceToTarget}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"sourceToTarget"}) 0 0 0
433 0 $newFH->{sourceToTarget}->print($al->writeToTalp("source")."\n");
434 }
435 0 ▶ if ($newFH->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"targetToSource"}) 0 0 0
436 0 $newFH->{targetToSource}->print($al->writeToTalp("target")."\n");
437 }
438 }elsif ($newFormat eq "NAACL"){
439 0 ▶ if ($newFH->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"source"}) 0 0 0
440 0 $newFH->{source}->print("<s snum=$internalSentPairNum> ".$al->sourceSentence." </s>\n");
441 }
442 0 ▶ if ($newFH->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"target"}) 0 0 0
443 0 $newFH->{target}->print("<s snum=$internalSentPairNum> ".$al->targetSentence." </s>\n");
444 }
445 0 my $lines = $al->writeToBlinker("source");
446 0 foreach my $line (@$lines){
447 0 $newFH->{sourceToTarget}->print("$internalSentPairNum $line\n");
448 }
449 0 ▶ if ($newFH->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"targetToSource"}) 0 0 0
450 0 $lines = $al->writeToBlinker("target");
451 0 foreach my $line (@$lines){
452 0 $newFH->{targetToSource}->print("$internalSentPairNum $line\n");
453 }
454 }
455 }elsif ($newFormat eq "GIZA"){
456 0 ▶ if (exists($newFH->{sourceToTarget})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $newFH->{"sourceToTarget"}) 0 0 0
457 0 $newFH->{sourceToTarget}->print("".$al->writeToGiza("source"));
458 }
459 0 ▶ if (exists($newFH->{targetToSource})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $newFH->{"targetToSource"}) 0 0 0
460 0 $newFH->{targetToSource}->print("".$al->writeToGiza("target"));
461 }
462 }elsif ($newFormat eq "BLINKER"){
463 0 ▶ if ($newFH->{source}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"source"}) 0 0 0
464 0 $newFH->{source}->print($al->sourceSentence."\n");
465 }
466 0 ▶ if ($newFH->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newFH->{"target"}) 0 0 0
467 0 $newFH->{target}->print($al->targetSentence."\n");
468 }
469 0 my $blinkerFile = $newLocation->{sourceToTarget}."/samp".$newLocation->{sampleNum}.".SentPair".($internalSentPairNum-1);
470 0 ▶ open BLINKER, ">$blinkerFile" || die "Blinker file $blinkerFile opening problem:$!";
Condition0%0/2
">$blinkerFile" || die("Blinker file $blinkerFile opening problem:$!")
l!l
00
MC/DC0%0/1
">$blinkerFile" || die("Blinker file $blinkerFile opening problem:$!")
">$blinkerFile"
Truth table0%0/2
A
A= ">$blinkerFile"
Aresult
11
00
471 0 my $lines = $al->writeToBlinker("source");
472 0 foreach my $line (@$lines){
473 0 print BLINKER "$line\n";
474 }
475 0 close BLINKER;
476 0 ▶ if ($newLocation->{targetToSource}){
Branch: 1 branch
BranchTrueFalseTotal
if ($newLocation->{"targetToSource"}) 0 0 0
477 0 $blinkerFile = $newLocation->{targetToSource}."/samp".$newLocation->{sampleNum}.".SentPair".($internalSentPairNum-1);
478 0 ▶ open BLINKER, ">$blinkerFile" || die "Blinker file $blinkerFile opening problem:$!";
Condition0%0/2
">$blinkerFile" || die("Blinker file $blinkerFile opening problem:$!")
l!l
00
MC/DC0%0/1
">$blinkerFile" || die("Blinker file $blinkerFile opening problem:$!")
">$blinkerFile"
Truth table0%0/2
A
A= ">$blinkerFile"
Aresult
11
00
479 0 my $lines = $al->writeToBlinker("target");
480 0 foreach my $line (@$lines){
481 0 print BLINKER "$line\n";
482 }
483 0 close BLINKER;
484 }
485 }else {
486 0 die "Output to format $newFormat is not implemented yet.";
487 }
488 }
489  
490 0 ▶sub displayAsLinkEnumeration {
Subroutine displayAsLinkEnumeration: not called CC=10 SCAR=47.0
Pod undocumented
491 0 my ($al,$format,$latex) = @_;
492 0 my $lines="";
493
494
495 0 ▶ if ($format eq "text"){
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'text') { } 0 0 0
elsif ($format eq 'latex') { } 0 0 0
496 0 my ($correspPosition,$wordPosition);
497
498 0 $lines.= join(" ",@{$al->{sourceWords}})."\n";
499 0 $lines.= join(" ",@{$al->{targetWords}})."\n\n";
500
501 0 ▶ for ($wordPosition=0;$wordPosition<@{$al->{sourceWords}};$wordPosition++){
Branch: 1 branch
BranchTrueFalseTotal
if ($wordPosition < @{$al->{"sourceWords"};}) 0 0 0
502 0 $lines.= @{$al->{sourceWords}}[$wordPosition]." <- ";
503 0 foreach $correspPosition (@{$al->{sourceAl}[$wordPosition]}){
504 0 $lines.= $al->{targetWords}[$correspPosition]." ";
505 }
506 0 $lines.= "\n";
507 }
508 0 $lines.="\n\n";
509 }elsif ($format eq "latex"){
510 0 my $numRowTokens = @{$al->{sourceWords}};
511 0 my $numColTokens = @{$al->{targetWords}};
512 0 my ($i,$j,$elt);
513 0 my ($j_partOf_Bi,$i_partOf_Bj);
514 0 my ($targetWord,$sourceWord);
515
516 0 $lines.= $latex->fromText("\n".join(" ",@{$al->{sourceWords}})."\n");
517 0 $lines.= $latex->fromText(join(" ",@{$al->{targetWords}})."\n\n").'\vspace{5mm}'."\n";
518
519 0 ▶ for ($j=0; $j<$numRowTokens;$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $numRowTokens) 0 0 0
520 0 ▶ for ($i=0;$i<$numColTokens;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numColTokens) 0 0 0
521 0 $targetWord = $latex->fromText($al->{targetWords}[$i]);
522 0 $sourceWord = $latex->fromText($al->{sourceWords}[$j]);
523 0 $i_partOf_Bj = $al->isIn("sourceAl",$j,$i);
524 0 $j_partOf_Bi = $al->isIn("targetAl",$i,$j);
525 0 ▶ if ($i_partOf_Bj > 0) { #ie i=aj
Branch: 1 branch
BranchTrueFalseTotal
if ($i_partOf_Bj > 0) { } 0 0 0
526 0 ▶ if ($j_partOf_Bi > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($j_partOf_Bi > 0) { } 0 0 0
527 0 $lines.= $sourceWord.' \boldmath $\leftrightarrow$ '.$targetWord." \n\n";
528 }else{
529 0 $lines.= $sourceWord.' \boldmath $\leftarrow$ '.$targetWord." \n\n";
530 }
531 }else{
532 0 ▶ if ($j_partOf_Bi > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($j_partOf_Bi > 0) { } 0 0 0
533 0 $lines.= $sourceWord.' \boldmath $\rightarrow$ '.$targetWord." \n\n";
534 }else{
535 }
536 }
537 }
538 }
539 0 $lines.= "\n\n".'\vspace{7mm}';
540 } #elsif $format eq latex
541 0 return $lines;
542 }
543  
544 0 ▶sub displayAsMatrix {
Subroutine displayAsMatrix: not called CC=22 SCAR=62.3
Pod undocumented
545 0 my ($al,$latex,$mark,$maxRows,$maxCols)= @_;
546 0 my $matrix = "";
547 0 my ($mark_ji,$mark_ij);
548 0 my $mark_ji_cross='\boldmath $-$';
549 0 my $numRowTokens = @{$al->{sourceWords}};
550 0 my $numColTokens = @{$al->{targetWords}};
551 0 my ($i,$j,$elt);
552 0 my ($j_partOf_Bi,$i_partOf_Bj);
553 0 my $offset;
554  
555 0 ▶ if ($numRowTokens>$maxRows){return $al->displayAsLinkEnumeration("latex",$latex)}
Branch: 1 branch
BranchTrueFalseTotal
if ($numRowTokens > $maxRows) 0 0 0
556
557 0 $matrix.= $latex->fromText("\n".join(" ",@{$al->{sourceWords}})."\n");
558 0 $matrix.= $latex->fromText(join(" ",@{$al->{targetWords}})."\n\n").'\vspace{5mm}';
559  
560 0 ▶ for ($offset=0;$offset<$numColTokens;$offset+=$maxCols){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $numColTokens) 0 0 0
561 0 $matrix.= "\n".'\begin{tabular}{l'."c" x $numColTokens.'}';
562 0 ▶ for ($j=$numRowTokens-1;$j>=0;$j--){
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 0 0 0
563 0 $matrix.= "\n".$latex->fromText($al->{sourceWords}[$j]);
564 0 ▶ for ($i=$offset;$i<$numColTokens && $i<($offset+$maxCols);$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numColTokens and $i < $offset + $maxCols) 0 0 0
Condition0%0/3
$i < $numColTokens and $i < $offset + $maxCols
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $numColTokens and $i < $offset + $maxCols
$i < $numColTokens $i < $offset + $maxCols
Truth table0%0/3
A and B
A= $i < $numColTokensB= $i < $offset + $maxCols
ABresult
0X0
100
111
565 0 $i_partOf_Bj = $al->isIn("sourceAl",$j,$i);
566 0 $j_partOf_Bi = $al->isIn("targetAl",$i,$j);
567 0 ▶ if ($mark eq "cross"){$mark_ji=$mark_ji_cross}
Branch: 3 branches
BranchTrueFalseTotal
if ($mark eq 'cross') { } 0 0 0
elsif ($mark eq 'ambiguity') { } 0 0 0
elsif ($mark eq 'confidence') { } 0 0 0
568 elsif ($mark eq "ambiguity"){
569 0 ▶ if (length($al->{sourceLinks}->{"$j $i"}[0])>0){$mark_ji=$al->{sourceLinks}->{"$j $i"}[0]}
Branch: 1 branch
BranchTrueFalseTotal
if (length $al->{'sourceLinks'}{"$j $i"}[0] > 0) { } 0 0 0
570 0 else {$mark_ji = $mark_ji_cross}
571 }
572 elsif ($mark eq "confidence"){
573 0 ▶ if (length($al->{sourceLinks}->{"$j $i"}[1])>0){$mark_ji=$al->{sourceLinks}->{"$j $i"}[1]}
Branch: 1 branch
BranchTrueFalseTotal
if (length $al->{'sourceLinks'}{"$j $i"}[1] > 0) { } 0 0 0
574 0 else {$mark_ji = $mark_ji_cross}
575 }
576 0 else {$mark_ji = $mark}
577 0 ▶ if ($mark eq "ambiguity"){
Branch: 2 branches
BranchTrueFalseTotal
if ($mark eq 'ambiguity') { } 0 0 0
elsif ($mark eq 'confidence') { } 0 0 0
578 0 ▶ if (length($al->{targetLinks}->{"$i $j"}[0])>0){$mark_ij='\ver{'.$al->{targetLinks}->{"$i $j"}[0].'}'}
Branch: 1 branch
BranchTrueFalseTotal
if (length $al->{'targetLinks'}{"$i $j"}[0] > 0) { } 0 0 0
579 0 else {$mark_ij = '\ver{'.$mark_ji_cross.'}'}
580 }elsif ($mark eq "confidence"){
581 0 ▶ if (length($al->{targetLinks}->{"$i $j"}[1])>0){$mark_ij='\ver{'.$al->{targetLinks}->{"$i $j"}[1].'}'}
Branch: 1 branch
BranchTrueFalseTotal
if (length $al->{'targetLinks'}{"$i $j"}[1] > 0) { } 0 0 0
582 0 else {$mark_ij = '\ver{'.$mark_ji_cross.'}'}
583 0 }else{$mark_ij = '\ver{'.$mark_ji.'}'}
584
585 0 $matrix.= "&";
586 0 ▶ if ($i_partOf_Bj > 0) { #ie i=aj
Branch: 1 branch
BranchTrueFalseTotal
if ($i_partOf_Bj > 0) { } 0 0 0
587 0 ▶ if ($j_partOf_Bi > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($j_partOf_Bi > 0) { } 0 0 0
588 0 ▶ if ($mark_ji eq '\boldmath $-$' && $mark_ij eq '\ver{\boldmath $-$}'){
Branch: 1 branch
BranchTrueFalseTotal
if ($mark_ji eq '\\boldmath $-$' and $mark_ij eq '\\ver{\\boldmath $-$}') { } 0 0 0
Condition0%0/3
$mark_ji eq "\\boldmath \$-\$" and $mark_ij eq "\\ver{\\boldmath \$-\$}"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark_ji eq "\\boldmath \$-\$" and $mark_ij eq "\\ver{\\boldmath \$-\$}"
$mark_ji eq "\\boldmath \$-\$" $mark_ij eq "\\ver{\\boldmath \$-\$}"
Truth table0%0/3
A and B
A= $mark_ji eq "\\boldmath \$-\$"B= $mark_ij eq "\\ver{\\boldmath \$-\$}"
ABresult
0X0
100
111
589 0 $matrix.= ' \boldmath ${+}$ ';
590 }else{
591 0 $matrix.= " $mark_ji$mark_ij ";
592 }
593 }else{
594 0 $matrix.= " $mark_ji ";
595 }
596 }else{
597 0 ▶ if ($j_partOf_Bi > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($j_partOf_Bi > 0) { } 0 0 0
598 0 $matrix.= " $mark_ij ";
599 }else{
600 0 $matrix.= ' . ';
601 }
602 }
603 } #for j=...
604 0 $matrix.= ' \\\\';
605 } #for i=...
606 # last line
607 0 $matrix.= "\n ";
608 0 ▶ for ($i=$offset;$i<$numColTokens && $i<($offset+$maxCols);$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numColTokens and $i < $offset + $maxCols) 0 0 0
Condition0%0/3
$i < $numColTokens and $i < $offset + $maxCols
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $numColTokens and $i < $offset + $maxCols
$i < $numColTokens $i < $offset + $maxCols
Truth table0%0/3
A and B
A= $i < $numColTokensB= $i < $offset + $maxCols
ABresult
0X0
100
111
609 0 $matrix.= ' & '.'\ver{'.$latex->fromText($al->{targetWords}[$i]).'}';
610 }
611 0 $matrix.= ' \\\\';
612 0 $matrix.= "\n".'\end{tabular}'."\n\n".'\vspace{7mm}';
613 } # loop on number of matrices
614
615 0 return $matrix;
616 }
617  
618  
619 # prohibits situations of the type: if linked(e,f) and linked(e',f) and linked(e',f') but not linked(e,f')
620 # in this case the function links e and f'.
621 0 ▶sub forceGroupConsistency {
Subroutine forceGroupConsistency: not called CC=17 SCAR=57.2
Pod undocumented
622 0 my ($al,$mode,$lex1,$lex2) = @_;
623 #defaults:
624 0 ▶ if (!defined($mode)){$mode=""}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mode) 0 0 0
625 0 my $dumper = new Dumpvalue;
626 0 my $cloneAl = {};
627 0 foreach my $source (("source","target")){
628 # SELECT ONLY S LINKS
629 0 my $sal = $al->SLinks();
630 #first we divide the alignment in clusters of positions linked between each other
631 0 my $groups=$sal->getAlClusters($source);
632
633 #delete alignment
634 0 ▶ if (defined($sal->{$source."Al"}) && @{$sal->{$source."Al"}}>0){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$sal{$source . 'Al'} and @{$$sal{$source . 'Al'};} > 0) 0 0 0
Condition0%0/3
defined $$sal{$source . 'Al'} and @{$$sal{$source . 'Al'};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$sal{$source . 'Al'} and @{$$sal{$source . 'Al'};} > 0
defined $$sal{$source . 'Al'} @{$$sal{$source . 'Al'};} > 0
Truth table0%0/3
A and B
A= defined $$sal{$source . 'Al'}B= @{$$sal{$source . 'Al'};} > 0
ABresult
0X0
100
111
635 0 ▶ for (my $j=0;$j<@{$sal->{$source."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$sal{$source . 'Al'};}) 0 0 0
636 0 $sal->{$source."Al"}[$j]=[];
637 }
638 }
639  
640 # print "BEFORE alignment:\n";
641 # print $dumper->dumpValue($al->{$source."Al"});
642 # print "CLUSTERS:\n";
643 # print $dumper->dumpValue($groups);
644
645 #then we check that all the links within each cluster exist, and create them if they don't
646 0 my $g;
647 0 ▶ for ($g=0;$g<@$groups;$g++){
Branch: 1 branch
BranchTrueFalseTotal
if ($g < @$groups) 0 0 0
648 0 ▶ if ($mode eq "contiguous"){
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "contiguous") 0 0 0
649 0 my $sContiguousSeqs=Lingua::AlSetLib::getContiguousSequences ($groups->[$g]{source});
650 0 my $tContiguousSeqs=Lingua::AlSetLib::getContiguousSequences ($groups->[$g]{target});
651 0 ▶ if (@$sContiguousSeqs > 1 || @$tContiguousSeqs > 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@$sContiguousSeqs > 1 or @$tContiguousSeqs > 1) 0 0 0
Condition0%0/3
@$sContiguousSeqs > 1 or @$tContiguousSeqs > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
@$sContiguousSeqs > 1 or @$tContiguousSeqs > 1
@$sContiguousSeqs > 1 @$tContiguousSeqs > 1
Truth table0%0/3
A or B
A= @$sContiguousSeqs > 1B= @$tContiguousSeqs > 1
ABresult
1X1
011
000
652 # print "CLUSTER:\n";
653 # print $dumper->dumpValue($groups->[$g]);
654 0 my ($bestIbm1Prob,$bestSourceSeq,$bestTargetSeq)=(0,0,0);
655 0 ▶ for (my $sc=0;$sc<@$sContiguousSeqs;$sc++){
Branch: 1 branch
BranchTrueFalseTotal
if ($sc < @$sContiguousSeqs) 0 0 0
656 0 my $sPhrase = $sal->printPhrase("source",$sContiguousSeqs->[$sc]);
657 0 ▶ for (my $tc=0;$tc<@$tContiguousSeqs;$tc++){
Branch: 1 branch
BranchTrueFalseTotal
if ($tc < @$tContiguousSeqs) 0 0 0
658 0 my $tPhrase = $sal->printPhrase("target",$tContiguousSeqs->[$tc]);
659 0 my $ibm1t_s = Lingua::AlSetLib::ibm1Prob ($sPhrase,$tPhrase,$lex1);
660 0 my $ibm1s_t;
661 0 ▶ if (defined($lex2)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lex2) { } 0 0 0
662 0 $ibm1s_t = Lingua::AlSetLib::ibm1Prob ($tPhrase,$sPhrase,$lex2);
663 }else{
664 0 $ibm1s_t = $ibm1t_s;
665 }
666 0 my $ibm1 = 0.5*($ibm1t_s+$ibm1s_t);
667 # print "$sPhrase ||| $tPhrase ||| $ibm1t_s -- $ibm1s_t ==> $ibm1\n";
668 0 ▶ if ($ibm1 > $bestIbm1Prob){
Branch: 1 branch
BranchTrueFalseTotal
if ($ibm1 > $bestIbm1Prob) 0 0 0
669 0 $bestIbm1Prob=$ibm1;
670 0 $bestSourceSeq=$sc;
671 0 $bestTargetSeq=$tc;
672 }
673 }
674 }
675  
676 0 @{$groups->[$g]{source}}=@{$sContiguousSeqs->[$bestSourceSeq]};
677 0 @{$groups->[$g]{target}}=@{$tContiguousSeqs->[$bestTargetSeq]};
678  
679 # print " contiguous CLUSTER:\n";
680 # print $dumper->dumpValue($groups->[$g]);
681 # print "best: ".$al->printPhrase('source',$groups->[$g]{source})." | ".$al->printPhrase('target',$groups->[$g]{target})."\n";
682 }
683 }
684 0 foreach my $j (@{$groups->[$g]{source}}){
685 0 foreach my $i (@{$groups->[$g]{target}}){
686 0 ▶ if (!$al->isIn($source."Al",$j,$i)){
Branch: 1 branch
BranchTrueFalseTotal
if (not $al->isIn($source . 'Al', $j, $i)) { } 0 0 0
687 0 push @{$al->{$source."Al"}[$j]},$i;
688 }else{ # move from P to S links
689 0 @{$al->{$source."Links"}->{"$j $i"}}[0]="";
690 }
691 }
692 }
693 }
694 # print "CLUSTERS after:\n";
695 # print $dumper->dumpValue($groups);
696 # print "alignment AFTER:\n";
697 # print $dumper->dumpValue($al->{$source."Al"});
698 } #foreach $side
699 }
700  
701 #####################################################
702 ### SYMMETRIZATION SUBS ###
703 #####################################################
704 # input: alignment object
705 # output: intersection of source and target alignments of this object
706 0 ▶sub intersect {
Subroutine intersect: not called CC=7 SCAR=40.3
Pod undocumented
707 0 my $al = shift;
708 0 my $intersectSourceAl=[];
709 0 my $intersectTargetAl=[];
710 0 my ($i,$j,$ind);
711
712 0 ▶ if (@{$al->{targetAl}}>0 && @{$al->{sourceAl}}>0){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$al->{"targetAl"};} > 0 and @{$al->{"sourceAl"};} > 0) 0 0 0
Condition0%0/3
@{$al->{"targetAl"};} > 0 and @{$al->{"sourceAl"};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
@{$al->{"targetAl"};} > 0 and @{$al->{"sourceAl"};} > 0
@{$al->{"targetAl"};} > 0 @{$al->{"sourceAl"};} > 0
Truth table0%0/3
A and B
A= @{$al->{"targetAl"};} > 0B= @{$al->{"sourceAl"};} > 0
ABresult
0X0
100
111
713 #for each link in sourceAl, look if it's present in targetAl
714 0 ▶ for ($j=0;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
715 0 ▶ if (defined($al->{sourceAl}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"sourceAl"}[$j]) 0 0 0
716 0 foreach $i (@{$al->{sourceAl}[$j]}){
717 0 ▶ if ($al->isIn("targetAl",$i,$j)){
Branch: 1 branch
BranchTrueFalseTotal
if ($al->isIn("targetAl", $i, $j)) 0 0 0
718 0 push @{$intersectSourceAl->[$j]},$i;
719 0 push @{$intersectTargetAl->[$i]},$j;
720 }
721 }
722 } #if defined
723 }
724 } #if targetAl is an empty array, then from the intersection sourceAl remains empty
725 0 @{$al->{sourceAl}}=@{$intersectSourceAl};
726 0 @{$al->{targetAl}}=@{$intersectTargetAl};
727 }
728  
729 # input: alignment object
730 # output: union of source and target alignments of this object
731 0 ▶sub getUnion {
Subroutine getUnion: not called CC=9 SCAR=45.0
Pod undocumented
732 0 my $al=shift;
733 0 my %union;
734 0 $union{sourceAl}=[];
735 0 $union{targetAl}=[];
736 0 my ($j,$i,$ind);
737 0 my %side=("source"=>"target","target"=>"source");
738 0 my ($source,$target);
739
740 0 ▶ if (@{$al->{targetAl}}>0 && @{$al->{sourceAl}}>0){
Branch: 2 branches
BranchTrueFalseTotal
if (@{$al->{'targetAl'};} > 0 and @{$al->{'sourceAl'};} > 0) { } 0 0 0
elsif (@{$al->{'sourceAl'};} > 0) { } 0 0 0
Condition0%0/3
@{$al->{"targetAl"};} > 0 and @{$al->{"sourceAl"};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
@{$al->{"targetAl"};} > 0 and @{$al->{"sourceAl"};} > 0
@{$al->{"targetAl"};} > 0 @{$al->{"sourceAl"};} > 0
Truth table0%0/3
A and B
A= @{$al->{"targetAl"};} > 0B= @{$al->{"sourceAl"};} > 0
ABresult
0X0
100
111
741 0 ▶ while (($source,$target)= each(%side)){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %side) 0 0 0
742 0 ▶ for ($j=0;$j<@{$al->{$source."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$source . 'Al'};}) 0 0 0
743 0 ▶ if (defined($al->{$source."Al"}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$al{$source . 'Al'}[$j]) 0 0 0
744 0 foreach $i (@{$al->{$source."Al"}[$j]}){
745 0 push @{$union{$source."Al"}->[$j]},$i;
746 0 ▶ if (!$al->isIn($target."Al",$i,$j)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($al->isIn($target . 'Al', $i, $j)) 0 0 0
747 0 push @{$union{$target."Al"}->[$i]},$j;
748 }
749 } #foreach
750 }
751 } #for
752 }
753 0 }elsif (@{$al->{sourceAl}}>0){
754 0 @{$union{sourceAl}}=@{$al->{sourceAl}};
755 }else{
756 0 @{$union{targetAl}}=@{$al->{targetAl}};
757 }
758 0 @{$al->{sourceAl}}=@{$union{sourceAl}};
759 0 @{$al->{targetAl}}=@{$union{targetAl}};
760 }
761  
762 # input: alignment object
763 # output: this object where only the links of the side (source or target) with most links are selected
764 0 ▶sub selectSideWithLinks{
Subroutine selectSideWithLinks: not called CC=24 SCAR=64.0
Pod undocumented
765 0 my ($al,$criterion,$dontCountNull)=@_;
766 #defaults
767 0 ▶ if (!defined($criterion)){$criterion="most"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $criterion) 0 0 0
768 0 ▶ if (!defined($dontCountNull)){$dontCountNull=1}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dontCountNull) 0 0 0
769 0 my ($j,$i,$firstInd);
770 0 my ($numSource,$numTarget)=(0,0);
771 0 my $sourceAl=[];
772 0 my $targetAl=[];
773
774 0 ▶ if ($dontCountNull){$firstInd=1}
Branch: 1 branch
BranchTrueFalseTotal
if ($dontCountNull) { } 0 0 0
775 0 else {$firstInd=0}
776 #count links
777 0 ▶ for ($j=$firstInd;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
778 0 ▶ if (defined($al->{sourceAl}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"sourceAl"}[$j]) 0 0 0
779 0 ▶ if (!$dontCountNull){
Branch: 1 branch
BranchTrueFalseTotal
if (not $dontCountNull) { } 0 0 0
780 0 $numSource+=@{$al->{sourceAl}[$j]};
781 }else{
782 0 foreach $i (@{$al->{sourceAl}[$j]}){
783 0 ▶ if ($i!=0){$numSource++}
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0) 0 0 0
784 }
785 }
786 }
787 }
788 0 ▶ for ($i=$firstInd;$i<@{$al->{targetAl}};$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$al->{"targetAl"};}) 0 0 0
789 0 ▶ if (defined($al->{targetAl}[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"targetAl"}[$i]) 0 0 0
790 0 ▶ if (!$dontCountNull){
Branch: 1 branch
BranchTrueFalseTotal
if (not $dontCountNull) { } 0 0 0
791 0 $numTarget+=@{$al->{targetAl}[$i]};
792 }else{
793 0 foreach $j (@{$al->{targetAl}[$i]}){
794 0 ▶ if ($j!=0){$numTarget++}
Branch: 1 branch
BranchTrueFalseTotal
if ($j != 0) 0 0 0
795 }
796 }
797 }
798 }
799 #select side with (most,least) links
800 0 ▶ if ( ($numSource>=$numTarget && $criterion eq "most") || ($numSource<$numTarget && $criterion ne "most")){ #select sourceAl
Branch: 1 branch
BranchTrueFalseTotal
if ($numSource >= $numTarget and $criterion eq 'most' or $numSource < $numTarget and $criterion ne 'most') { } 0 0 0
Condition0%0/9
$numSource >= $numTarget and $criterion eq "most" or $numSource < $numTarget and $criterion ne "most"
l!l&&r!l&&!r
000
$numSource >= $numTarget and $criterion eq "most"
!ll&&!rl&&r
000
$numSource < $numTarget and $criterion ne "most"
!ll&&!rl&&r
000
MC/DC0%0/4
$numSource >= $numTarget and $criterion eq "most" or $numSource < $numTarget and $criterion ne "most"
$numSource >= $numTarget $criterion eq "most" $numSource < $numTarget $criterion ne "most"
Truth table0%0/7
A and B or C and D
A= $numSource >= $numTargetB= $criterion eq "most"C= $numSource < $numTargetD= $criterion ne "most"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
801 0 ▶ for ($j=0;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
802 0 ▶ if (defined($al->{sourceAl}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"sourceAl"}[$j]) 0 0 0
803 0 foreach $i (@{$al->{sourceAl}[$j]}){
804 0 push @{$sourceAl->[$j]},$i;
805 0 push @{$targetAl->[$i]},$j;
806 }
807 }
808 }
809 }else{ #select targetAl
810 0 ▶ for ($i=0;$i<@{$al->{targetAl}};$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$al->{"targetAl"};}) 0 0 0
811 0 ▶ if (defined($al->{targetAl}[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"targetAl"}[$i]) 0 0 0
812 0 foreach $j (@{$al->{targetAl}[$i]}){
813 0 push @{$sourceAl->[$j]},$i;
814 0 push @{$targetAl->[$i]},$j;
815 }
816 }
817 }
818 }
819 0 @{$al->{sourceAl}}=@$sourceAl;
820 0 @{$al->{targetAl}}=@$targetAl;
821 }
822  
823 0 ▶sub selectSideWithMostLinks{
Subroutine selectSideWithMostLinks: not called CC=1 SCAR=6.9
Pod undocumented
824 0 my $al=shift;
825 0 return $al->selectSideWithLinks("most");
826 }
827 0 ▶sub selectSideWithLeastLinks{
Subroutine selectSideWithLeastLinks: not called CC=1 SCAR=6.9
Pod undocumented
828 0 my $al=shift;
829 0 return $al->selectSideWithLinks("least");
830 }
831  
832 # input: alignment object
833 # output: alignment object where source and target have been swapped
834 0 ▶sub swapSourceTarget{
Subroutine swapSourceTarget: not called CC=7 SCAR=40.3
Pod undocumented
835 0 my $al=shift;
836 0 my ($link,$ref,$j,$i,$source);
837 0 my @st;
838 0 my @sides=("source","target");
839 my $swappedAl={ "sourceAl"=>[],
840 "targetAl"=>[],
841 "sourceWords"=>$al->{targetWords},
842 "targetWords"=>$al->{sourceWords},
843 0 "sourceLinks"=>{},
844 "targetLinks"=>{}};
845
846 0 foreach $source (@sides){
847 0 ▶ for ($j=0;$j<@{$al->{$source."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$source . 'Al'};}) 0 0 0
848 0 foreach $i (@{$al->{$source."Al"}[$j]}){
849 0 push @{$swappedAl->{$source."Al"}[$i]},$j;
850 }
851 }
852 #insert ref to empty array instead of undef entries
853 0 ▶ for ($j=0;$j<@{$swappedAl->{$source."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$swappedAl{$source . 'Al'};}) 0 0 0
854 0 ▶ if (!defined($swappedAl->{$source."Al"}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$swappedAl{$source . 'Al'}[$j]) 0 0 0
855 0 $swappedAl->{$source."Al"}[$j]=[];
856 }
857 }
858 # and now the sourceLinks
859 0 ▶ while (($link,$ref)=each(%{$al->{$source."Links"}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$al{$source . 'Links'};}) 0 0 0
860 0 @st=split(" ",$link);
861 0 $swappedAl->{$source."Links"}{"$st[1] $st[0]"}=$ref;
862 }
863 }
864 0 %$al=%$swappedAl;
865 }
866  
867  
868  
869 # input: al object, offset, length, side (src or trg), ref to word list to be added, ref to a list of positions of the other side (to which all added words will be linked).
870 # output: Alignment object where given positions are sustituted by the words
871 #
872 # notes: 1) in case of deleting various words:
873 # - all added words are linked to all positions to which deleted words were linked (except if you provided a list of positions of the other side, in which case all added words are linked to those positions).
874 # - $al->{sourceLinks} information can be lost for these words.
875 # 2) Does not work for targetAl alignment
876 # 3) more efficient in "source" side than in "target"
877  
878 0 ▶sub splice {
Subroutine splice: not called CC=17 SCAR=57.2
Pod undocumented
879 0 my ($al,$side,$offset,$length,$refToWordsToAdd,$refToOtherSidePosi)=@_;
880 0 my $dumper = new Dumpvalue;
881
882 0 ▶ if (!defined($refToWordsToAdd)){$refToWordsToAdd=[];}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $refToWordsToAdd) 0 0 0
883 0 ▶ if (!defined($refToOtherSidePosi)){$refToOtherSidePosi=[];}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $refToOtherSidePosi) 0 0 0
884 0 my $numToDelete=$length;
885 0 my $firstPos=$offset;
886 0 my $lastPos=$offset+$length-1;
887 0 my $numList = scalar(@$refToOtherSidePosi);
888 # print $al->displayAsLinkEnumeration("text");
889 # print "splice $side off:$offset len:$length add:",join(" ",@$refToWordsToAdd),"\n";
890 # print "before:",join(" ",@{$al->{$side."Words"}}),"\n";
891  
892 # MODIFY WORDS ARRAY
893 0 splice(@{$al->{$side."Words"}},$offset,$length,@$refToWordsToAdd);
894 # print "after:",join(" ",@{$al->{$side."Words"}}),"\n";
895
896 # MODIFY LINKS
897 0 my $numToAdd=scalar(@$refToWordsToAdd);
898 0 my $diff=$numToAdd-$numToDelete;
899 0 my @modified;
900 my %modifs;
901 0 my %links;
902  
903 0 ▶ if ($side eq "target"){
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq "target") 0 0 0
904 0 $al->swapSourceTarget;
905 }
906 #initialize modified array
907 0 ▶ for (my $j=0;$j<@{$al->{sourceAl}}+$diff;$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};} + $diff) 0 0 0
908 0 push @modified,[];
909 }
910 #print "ANTES:\n";
911 #print $al->displayAsLinkEnumeration("text");
912
913 #fill modified array with existing links
914 0 ▶ for (my $j=0;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
915 0 ▶ if (defined($al->{sourceAl}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"sourceAl"}[$j]) 0 0 0
916 0 foreach my $i (@{$al->{sourceAl}[$j]}){
917 #print "i $i j $j firstPos $firstPos\n";
918 0 ▶ if ($j<$firstPos){
Branch: 2 branches
BranchTrueFalseTotal
if ($j < $firstPos) { } 0 0 0
elsif ($j >= $firstPos and $j <= $lastPos) { } 0 0 0
919 0 push @{$modified[$j]},$i;
920 0 $links{"$j $i"}=$al->{sourceLinks}{"$j $i"};
921 ▶ }elsif ($j>=$firstPos && $j<=$lastPos){
Condition0%0/3
$j >= $firstPos and $j <= $lastPos
!ll&&!rl&&r
000
MC/DC0%0/2
$j >= $firstPos and $j <= $lastPos
$j >= $firstPos $j <= $lastPos
Truth table0%0/3
A and B
A= $j >= $firstPosB= $j <= $lastPos
ABresult
0X0
100
111
922 0 ▶ if ($numList==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($numList == 0) 0 0 0
923 #link added words to positions to which were linked the deleted words
924 0 ▶ for (my $p=$firstPos;$p<$firstPos+$numToAdd;$p++){
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $firstPos + $numToAdd) 0 0 0
925 0 ▶ if (!exists($modifs{$p}{$i})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $modifs{$p}{$i}) 0 0 0
926 0 push @{$modified[$p]},$i;
927 0 $links{"$p $i"}=$al->{sourceLinks}{"$j $i"};
928 0 $modifs{$p}{$i}=1;
929 }
930 }
931 }
932 }else{
933 0 push @{$modified[$j+$diff]},$i;
934 0 $links{($j+$diff)." $i"}=$al->{sourceLinks}{"$j $i"};
935 }
936 }
937 } #if defined
938 }
939 # insert provided links
940 0 ▶ for (my $p=$firstPos;$p<$firstPos+$numToAdd;$p++){
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $firstPos + $numToAdd) 0 0 0
941 0 foreach my $i (@$refToOtherSidePosi){
942 0 push @{$modified[$p]},$i;
943 }
944 }
945
946 0 @{$al->{sourceAl}}=@modified;
947 0 ▶ if ($side eq "target"){
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq "target") 0 0 0
948 0 $al->swapSourceTarget;
949 }
950 #print "DESPUES:\n";
951 #print $al->displayAsLinkEnumeration("text");
952 }
953  
954  
955 # INPUT: string (regexp) to be replaced, string (regexp) to replace it, side ("source" or "target")
956 # NOTES: 1) in case of deleting various words, all added words are linked to all positions to which deleted words were linked. $al->{sourceLinks} information can be lost for replaced words.
957 # 2) Does not work for targetAl alignment
958 # 3) more efficient in "source" side than in "target"
959 0 ▶sub regexpReplace {
Subroutine regexpReplace: not called CC=9 SCAR=45.0
Pod undocumented
960 0 my ($al,$regToDelete,$regToReplace,$side)=@_;
961 0 my $dumper=new Dumpvalue;
962 #print STDERR "s/$regToDelete/$regToReplace/\n";
963 0 my $sentence;
964 0 ▶ if ($side eq "source"){$sentence=$al->sourceSentence;}
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq 'source') { } 0 0 0
965 0 else {$sentence=$al->targetSentence;}
966 0 my $newSentence=$sentence;
967 0 $newSentence =~ s/$regToDelete/$regToReplace/og;
968 #print $al->sourceSentence."\n";
969 #print $al->targetSentence."\n";
970 #print $newSentence."\n";
971 0 my @words = split / /,$sentence;
972 0 my $nums = scalar(@words);
973 0 my @newWords = split / /,$newSentence;
974 0 my @diffs = Lingua::AlSetLib::diff( \@words, \@newWords );
975
976 # parse output of diff function
977 0 my @updatedPosi; #array: orig posis -> updated posis
978 my %reversePosi; #hash: updated posis -> orig posis
979 0 ▶ for (my $i=0;$i<=$nums;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nums) 0 0 0
980 0 $updatedPosi[$i]=$i;
981 0 $reversePosi{$i}=$i;
982 }
983
984 #$dumper->dumpValue(\@diffs);
985 0 foreach my $hunk (@diffs){
986 0 my @delPosi;
987 my @del;
988 0 my @addPosi;
989 0 my @add;
990 0 foreach my $change (@$hunk) {
991 0 ▶ if ($change->[0] eq '-'){
Branch: 1 branch
BranchTrueFalseTotal
if ($change->[0] eq '-') { } 0 0 0
992 0 push @delPosi,$change->[1]+1;
993 0 push @del,$change->[2];
994 }else{
995 0 push @addPosi,$change->[1]+1;
996 0 push @add,$change->[2];
997 }
998 }
999 0 my $numDel=scalar(@delPosi);
1000 0 my $numAdd=scalar(@addPosi);
1001  
1002 # del posis are relative to first array (@words) => update posis
1003 # add posis are relative to second array (@newWords) => don't update posis
1004 0 ▶ if ($numDel==0){ #insertion
Branch: 1 branch
BranchTrueFalseTotal
if ($numDel == 0) { } 0 0 0
1005 0 $al->splice("$side",$addPosi[0],0,\@add);
1006 #print "insert '",join(" ",@add),"' at position { ",$addPosi[0]," }\n";
1007 #update updatedPosi array
1008 0 ▶ for (my $i=$reversePosi{"$addPosi[0]"};$i<=$nums;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nums) 0 0 0
1009 0 $updatedPosi[$i]+=$numAdd;
1010 0 $reversePosi{"$updatedPosi[$i]"}=$i;
1011 }
1012 }else{ # substitution or deletion
1013 0 $al->splice("$side",$updatedPosi[$delPosi[0]],$numDel,\@add);
1014 #print "substitute '",join(" ",@del),"' at positions { ",join(" ",@delPosi)," } by '",join(" ",@add),"'\n";
1015 #update updatedPosi array
1016 0 ▶ for (my $i=$delPosi[0]+$numDel;$i<=$nums;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nums) 0 0 0
1017 0 $updatedPosi[$i]+=$numAdd-$numDel;
1018 0 $reversePosi{"$updatedPosi[$i]"}=$i;
1019 }
1020 }
1021 }
1022 }
1023  
1024 # eliminates any given WORD from the source or target file corpus and updates the alignment
1025 # input: $al (current Alignment object),$word (word RegExp to eliminate), $wordSide (from which side: source or target)
1026 # kept for compatibility with previous versions (regexpReplace or replaceWords should be used instead)
1027 0 ▶sub eliminateWord {
Subroutine eliminateWord: not called CC=1 SCAR=6.9
Pod undocumented
1028 0 my ($al,$word,$wordSide)= @_;
1029 0 return $al->replaceWords($word,'',$wordSide);
1030 }
1031  
1032 # INPUT: string to be replaced, string to replace it, side ("source" or "target")
1033 # NOTES: 1) in case of deleting various words, all added words are linked to all positions to which deleted words were linked. $al->{sourceLinks} information can be lost for replaced words.
1034 # 2) Does not work for targetAl alignment
1035 # 3) more efficient in "source" side than in "target"
1036 0 ▶sub replaceWords {
Subroutine replaceWords: not called CC=3 SCAR=24.8
Pod undocumented
1037 0 my ($al,$stToDelete,$stToReplace,$side)=@_;
1038 0 my $dumper=new Dumpvalue;
1039 0 $stToDelete =~ s/(^\s|\s$)//g;
1040 0 $stToDelete =~ s/\s+/ /g;
1041 0 $stToReplace =~ s/(^\s|\s$)//g;
1042 0 $stToReplace =~ s/\s+/ /g;
1043
1044 0 my @wToDel=split / /,$stToDelete;
1045 0 my $numToDel = scalar(@wToDel);
1046 0 my @toAdd=split(/ /,$stToReplace);
1047 0 my $numToAdd=scalar(@toAdd);
1048 0 my $diff=$numToAdd-$numToDel;
1049 # print $al->displayAsLinkEnumeration("text"),"\n";
1050 #list of positions where string to be deleted starts in @sourceWords (or target) array
1051 0 my @startToDelInAl=Lingua::AlSetLib::findArrayInAnother(\@wToDel,$al->{$side."Words"});
1052 0 my $offset=0;
1053 0 foreach my $startPosi (@startToDelInAl){
1054 0 my @posis;
1055 0 ▶ for (my $i=0;$i<$numToDel;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numToDel) 0 0 0
1056 0 push @posis,$startPosi+$i+$offset;
1057 }
1058 #print "positions:",join(" ",@posis),"\n";
1059 #$al->substitutePositions(\@posis,$side,$stToReplace);
1060 0 $al->splice($side,$posis[0],scalar(@posis),\@toAdd);
1061 0 $offset+=$diff;
1062 }
1063 # print $al->displayAsLinkEnumeration("text"),"\n";
1064 }
1065  
1066  
1067 # introduces underscore between links of many-to-many groups in source to target alignment
1068 # WARNING: THIS SUB FOR NOW ONLY CHANGES WORDS FILES, NOT THE LINKS FILE
1069 0 ▶sub manyToMany2joined {
Subroutine manyToMany2joined: not called CC=9 SCAR=45.0
Pod undocumented
1070 0 my $al=shift;
1071 0 my $new;
1072 0 @{$new->{source}} = @{$al->{sourceWords}};
1073 0 @{$new->{target}} = @{$al->{targetWords}};
1074
1075 0 my @sides=("source","target");
1076
1077  
1078 # group many-to-many linked phrases in clusters
1079 0 my $clusters=$al->getAlClusters;
1080 0 my $dumper = new Dumpvalue;
1081 # print "\n";
1082 # print $al->sourceSentence."\n";
1083 # print $al->targetSentence."\n";
1084  
1085 # print "CLUSTERS:\n";
1086 # print $dumper->dumpValue($clusters);
1087 0 foreach my $source (@sides){
1088 #sort clusters
1089 0 my %firstClustPos;
1090 0 ▶ for (my $c=0;$c<@$clusters;$c++){
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @$clusters) 0 0 0
1091 0 @{$clusters->[$c]{$source}} = sort { $a <=> $b; } @{$clusters->[$c]{$source}};
1092 }
1093 0 @$clusters = sort {$a->{$source}[0] <=> $b->{$source}[0]} @$clusters;
1094
1095 0 my $offset=0;
1096 0 foreach my $clust (@$clusters){
1097 0 ▶ if ( @{$clust->{$source}} >1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$clust->{$source};} > 1) 0 0 0
1098 #check that cluster is contiguous
1099 0 my $contiguous=1;
1100 0 ▶ for (my $c=1;$c<@{$clust->{$source}};$c++){
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @{$clust->{$source};}) 0 0 0
1101 0 ▶ if ($clust->{$source}[$c] != ($clust->{$source}[$c-1]+1) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($clust->{$source}[$c] != $clust->{$source}[$c - 1] + 1) 0 0 0
1102 0 $contiguous=0;
1103 0 last;
1104 }
1105 }
1106 0 ▶ if ($contiguous){
Branch: 1 branch
BranchTrueFalseTotal
if ($contiguous) { } 0 0 0
1107 # introduce underscore
1108 0 my $numWords = @{$clust->{$source}};
1109 0 my $newWord=$al->{$source."Words"}[$clust->{$source}[0]];
1110 0 ▶ for (my $c=1;$c<$numWords;$c++){
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $numWords) 0 0 0
1111 0 $newWord=$newWord."_".$al->{$source."Words"}[$clust->{$source}[$c]];
1112 }
1113 # print "new word: $newWord\n";
1114 0 splice(@{$new->{$source}},$clust->{$source}[0]-$offset,$numWords,$newWord);
1115  
1116 0 $offset+=$numWords-1;
1117 }else{
1118 0 print STDERR "not contiguous\n";
1119 }
1120 } #if
1121 }
1122 # print "\n";
1123 } #foreach $source
1124 # print "new source:",join(" ",@{$new->{source}}),"\n";
1125 # print "new target:",join(" ",@{$new->{target}}),"\n";
1126 0 @{$al->{sourceWords}}=@{$new->{source}};
1127 0 @{$al->{targetWords}}=@{$new->{target}};
1128 }
1129  
1130  
1131 # recreates links of words linked by underscore and removes underscores
1132 # ONLY WORKS WITH SOURCE2TARGET AlIGNMENT
1133 0 ▶sub joined2ManyToMany {
Subroutine joined2ManyToMany: not called CC=9 SCAR=45.0
Pod undocumented
1134 0 my $al=shift;
1135 # print $al->sourceSentence."\n";
1136 # print $al->targetSentence."\n";
1137  
1138 0 my @sides=("source","target");
1139 0 foreach my $source (@sides){
1140 0 my %joined;
1141 0 ▶ for (my $j=1;$j<@{$al->{$source."Words"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$source . 'Words'};}) 0 0 0
1142 0 ▶ if ($al->{$source."Words"}[$j] =~ /@@@/){
Branch: 1 branch
BranchTrueFalseTotal
if ($$al{$source . 'Words'}[$j] =~ /\@\@@/) 0 0 0
1143 0 $joined{$j}=1;
1144 }
1145 }
1146 0 my @sortedJoined = sort { $a <=> $b } keys(%joined);
1147 0 my $offset=0;
1148 0 foreach my $pos (@sortedJoined){
1149 # insert new words
1150 0 my $joinedWords = $al->{$source."Words"}[$pos+$offset];
1151 0 my @newWords = split(/@@@/,$joinedWords);
1152 0 my $firstWord = shift @newWords;
1153 0 $al->{$source."Words"}[$pos+$offset]=$firstWord;
1154  
1155 # insert new words to alignment, all linked to the same target words as the old (joined) token
1156 0 ▶ if ($source eq "source"){
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'source') { } 0 0 0
1157 0 $al->splice($source,$pos+1+$offset,0,\@newWords,$al->{sourceAl}[$pos+$offset]);
1158 }else{
1159 # look for links aligned to $pos+$offset
1160 0 my @alignedPos;
1161 # print "pos offset:".($pos+$offset)."\n";
1162 0 ▶ for (my $j=0;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
1163 0 foreach my $i (@{$al->{sourceAl}[$j]}){
1164 0 ▶ if ($i == ($pos+$offset)){
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $pos + $offset) 0 0 0
1165 0 push @alignedPos,$j;
1166 }
1167 }
1168 }
1169 0 $al->splice($source,$pos+1+$offset,0,\@newWords,\@alignedPos);
1170 }
1171 0 $offset += @newWords;
1172 }
1173 }
1174 }
1175  
1176 #input: (source,target) link
1177 #output: true if the link is reciprocal (or "cross link"), false otherwise
1178 0 ▶sub isCrossLink {
Subroutine isCrossLink: not called CC=2 SCAR=17.9
Pod undocumented
1179 0 my ($al,$j,$i)=@_;
1180 # print "s $j $i:",$al->isIn("sourceAl",$j,$i)," t $i $j:",$al->isIn("targetAl",$i,$j),"\n";
1181 0 ▶ return ( $al->isIn("sourceAl",$j,$i) && $al->isIn("targetAl",$i,$j) );
Condition0%0/3
$al->isIn('sourceAl', $j, $i) && $al->isIn('targetAl', $i, $j)
!ll&&!rl&&r
000
MC/DC0%0/2
$al->isIn('sourceAl', $j, $i) && $al->isIn('targetAl', $i, $j)
$al->isIn('sourceAl', $j, $i) $al->isIn('targetAl', $i, $j)
Truth table0%0/3
A && B
A= $al->isIn('sourceAl', $j, $i)B= $al->isIn('targetAl', $i, $j)
ABresult
0X0
100
111
1182 }
1183  
1184 0 ▶sub isAnchor{
Subroutine isAnchor: not called CC=7 SCAR=40.3
Pod undocumented
1185 0 my ($al,$j,$side)=@_;
1186 0 my ($reverseSide,$i);
1187  
1188 0 ▶ if ($side eq "source"){$reverseSide="target"}
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq 'source') { } 0 0 0
1189 0 else {$reverseSide = "source"}
1190 0 ▶ if (defined($al->{$side."Al"}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$al{$side . 'Al'}[$j]) 0 0 0
1191 0 ▶ if (@{$al->{$side."Al"}[$j]}==1){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$al{$side . 'Al'}[$j];} == 1) 0 0 0
1192 0 $i = $al->{$side."Al"}[$j][0];
1193 0 ▶ if (defined($al->{$reverseSide."Al"}[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$al{$reverseSide . 'Al'}[$i]) 0 0 0
1194 0 ▶ if (@{$al->{$reverseSide."Al"}[$i]}==1 && $al->{$reverseSide."Al"}[$i][0]==$j){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$al{$reverseSide . 'Al'}[$i];} == 1 and $$al{$reverseSide . 'Al'}[$i][0] == $j) 0 0 0
Condition0%0/3
@{$$al{$reverseSide . 'Al'}[$i];} == 1 and $$al{$reverseSide . 'Al'}[$i][0] == $j
!ll&&!rl&&r
000
MC/DC0%0/2
@{$$al{$reverseSide . 'Al'}[$i];} == 1 and $$al{$reverseSide . 'Al'}[$i][0] == $j
@{$$al{$reverseSide . 'Al'}[$i];} == 1 $$al{$reverseSide . 'Al'}[$i][0] == $j
Truth table0%0/3
A and B
A= @{$$al{$reverseSide . 'Al'}[$i];} == 1B= $$al{$reverseSide . 'Al'}[$i][0] == $j
ABresult
0X0
100
111
1195 0 return 1;
1196 }
1197 }
1198 }
1199 }
1200 0 return 0;
1201 }
1202  
1203 #mode: "noAnchors" cuts zones between 2 anchors and cannot include an anchor point
1204 # "anchors" cuts zone established by coordinates and doesn't look more
1205 0 ▶sub cut {
Subroutine cut: not called CC=35 SCAR=71.4
Pod undocumented
1206 0 my ($al,$startPointSource,$startPointTarget,$endPointSource,$endPointTarget,$mode)=@_;
1207 0 ▶ if (!defined($mode)){$mode="noAnchors"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mode) 0 0 0
1208 0 my ($j,$i,$ind);
1209 0 my %sourceInGap=();
1210 0 my %targetInGap=();
1211 0 my @sortedSourceInGap=();
1212 0 my @sortedTargetInGap=();
1213 0 my %sourceToNull=();
1214 0 my %targetToNull=();
1215 0 my $gap = Lingua::AlignmentSlice->new($al);
1216 0 my @linked=();
1217 0 my ($zeroSource,$zeroTarget,$numSource,$numTarget);
1218 0 my ($oldNumInGap,$newNumInGap);
1219 0 ▶ for ($j=$startPointSource+1;$j<$endPointSource;$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $endPointSource) 0 0 0
1220 0 $sourceInGap{$j}=1;
1221 }
1222 0 ▶ for ($i=$startPointTarget+1;$i<$endPointTarget;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $endPointTarget) 0 0 0
1223 0 ▶ if ($mode eq "noAnchors"){
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'noAnchors') { } 0 0 0
1224 0 ▶ if (!$al->isAnchor($i,"target")){
Branch: 1 branch
BranchTrueFalseTotal
unless ($al->isAnchor($i, "target")) 0 0 0
1225 0 $targetInGap{$i}=1;
1226 }
1227 }else{
1228 0 $targetInGap{$i}=1;
1229 }
1230 }
1231 # print "\n($startPointSource,$startPointTarget,$endPointSource,$endPointTarget)\n";
1232 # print "source in gap 1:".join(" ",keys %sourceInGap)."\n";
1233 # print "target in gap 1:".join(" ",keys %targetInGap)."\n";
1234
1235 #look at linked words situated outside the gap square:
1236 0 $oldNumInGap=0;
1237 0 $newNumInGap=scalar(keys %sourceInGap)+scalar(keys %targetInGap);
1238 0 ▶ while ($oldNumInGap != $newNumInGap){
Branch: 1 branch
BranchTrueFalseTotal
if ($oldNumInGap != $newNumInGap) 0 0 0
1239 0 foreach $i (keys %targetInGap){
1240 0 foreach $j (@{$al->{targetAl}[$i]}){
1241 0 ▶ if ($j!=0){
Branch: 1 branch
BranchTrueFalseTotal
if ($j != 0) { } 0 0 0
1242 0 $sourceInGap{$j}=1;
1243 }
1244 0 else {$targetToNull{$i}=1};
1245 }
1246 0 ▶ for ($j=1;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
1247 0 ▶ if ($al->isIn("sourceAl",$j,$i)){
Branch: 1 branch
BranchTrueFalseTotal
if ($al->isIn("sourceAl", $j, $i)) 0 0 0
1248 0 $sourceInGap{$j}=1;
1249 }
1250 }
1251 }
1252 0 foreach $j (keys %sourceInGap){
1253 0 foreach $i (@{$al->{sourceAl}[$j]}){
1254 0 ▶ if ($i!=0){
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0) { } 0 0 0
1255 0 $targetInGap{$i}=1;
1256 }
1257 0 else {$sourceToNull{$j}=1};
1258 }
1259 0 ▶ for ($i=1;$i<@{$al->{targetAl}};$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$al->{"targetAl"};}) 0 0 0
1260 0 ▶ if ($al->isIn("targetAl",$i,$j)){
Branch: 1 branch
BranchTrueFalseTotal
if ($al->isIn("targetAl", $i, $j)) 0 0 0
1261 0 $targetInGap{$i}=1;
1262 }
1263 }
1264 }
1265 0 $oldNumInGap=$newNumInGap;
1266 0 $newNumInGap=scalar(keys %sourceInGap)+scalar(keys %targetInGap);
1267 }
1268 0 foreach $i (@{$al->{sourceAl}[0]}){
1269 0 ▶ if ($targetInGap{$i}){$targetToNull{$i}=1}
Branch: 1 branch
BranchTrueFalseTotal
if ($targetInGap{$i}) 0 0 0
1270 }
1271 0 foreach $j (@{$al->{targetAl}[0]}){
1272 0 ▶ if ($sourceInGap{$j}){$sourceToNull{$j}=1}
Branch: 1 branch
BranchTrueFalseTotal
if ($sourceInGap{$j}) 0 0 0
1273 }
1274  
1275 0 @sortedSourceInGap = sort { $a <=> $b; } keys %sourceInGap;
1276 0 @sortedTargetInGap = sort { $a <=> $b; } keys %targetInGap;
1277  
1278 # print "source in gap 2:",join(" ",keys %sourceInGap)."\n";
1279 # print "target in gap 2:",join(" ",keys %targetInGap)."\n";
1280 # print "source sorted:",join(" ",@sortedSourceInGap)."\n";
1281 # print "target sorted:",join(" ",@sortedTargetInGap)."\n";
1282 # print "target to null:",join(" ",keys %targetToNull)."\n";
1283 # print "source to null:",join(" ",keys %sourceToNull)."\n";
1284  
1285 0 ▶ if (@sortedSourceInGap==0){
Branch: 1 branch
BranchTrueFalseTotal
if (@sortedSourceInGap == 0) { } 0 0 0
1286 0 $zeroSource=0;
1287 0 $numSource=0;
1288 }else{
1289 0 $zeroSource=$sortedSourceInGap[0]-1;
1290 0 $numSource=$sortedSourceInGap[@sortedSourceInGap-1]-$sortedSourceInGap[0]+1;
1291 }
1292 0 ▶ if (@sortedTargetInGap==0){
Branch: 1 branch
BranchTrueFalseTotal
if (@sortedTargetInGap == 0) { } 0 0 0
1293 0 $zeroTarget=0;
1294 0 $numTarget=0;
1295 }else{
1296 0 $zeroTarget=$sortedTargetInGap[0]-1;
1297 0 $numTarget=$sortedTargetInGap[@sortedTargetInGap-1]-$sortedTargetInGap[0]+1;
1298 }
1299
1300 #Actualize AlignmentSlice attributes
1301 0 $gap->setZero($zeroSource,$zeroTarget);
1302 0 foreach $j (keys %sourceInGap){
1303 0 $gap->{sourceIndices}{$j-$zeroSource}=1;
1304 }
1305 0 ▶ if (scalar (keys %targetToNull)>0){$gap->{sourceIndices}{0}=1};
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %targetToNull > 0) 0 0 0
1306 0 foreach $i (keys %targetInGap){
1307 0 $gap->{targetIndices}{$i-$zeroTarget}=1;
1308 }
1309 0 ▶ if (scalar (keys %sourceToNull)>0){$gap->{targetIndices}{0}=1};
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %sourceToNull > 0) 0 0 0
1310
1311 # print "zero s t:",$zeroSource," ",$zeroTarget,"\n";
1312 # print "num s t:",$numSource," ",$numTarget,"\n";
1313  
1314 ## LOAD GAP
1315 # 1. insert NULL word and select only words linked to NULL that belong to the gap
1316 0 push @{$gap->{sourceWords}},'NULL';
1317 0 foreach $i (keys %targetToNull){push @linked,$i-$gap->{zeroTarget}}
1318 0 push @{$gap->{sourceAl}},[@linked];
1319 0 push @{$gap->{targetWords}},'NULL';
1320 0 @linked=();
1321 0 foreach $j (keys %sourceToNull){push @linked,$j-$gap->{zeroSource}}
1322 0 push @{$gap->{targetAl}},[@linked];
1323 # 2. Add non-NULL words and alignments
1324 0 ▶ for ($ind=1;$ind<=$numSource;$ind++){
Branch: 1 branch
BranchTrueFalseTotal
if ($ind <= $numSource) 0 0 0
1325 0 $j=$ind+$gap->{zeroSource};
1326 0 $gap->{sourceWords}[$ind]=$al->{sourceWords}[$j];
1327 0 ▶ if ($sourceInGap{$j}){
Branch: 1 branch
BranchTrueFalseTotal
if ($sourceInGap{$j}) 0 0 0
1328 0 @linked=();
1329 0 foreach $i (@{$al->{sourceAl}[$j]}){
1330 #if ($targetInGap{$i}){ #useless:de facto included in the zone
1331 push @linked,$i-$gap->{zeroTarget}
1332 #}
1333 0 }
1334 0 $gap->{sourceAl}[$ind]=[@linked];
1335 }
1336 }
1337 0 ▶ for ($ind=1;$ind<=$numTarget;$ind++){
Branch: 1 branch
BranchTrueFalseTotal
if ($ind <= $numTarget) 0 0 0
1338 0 $i = $ind+$gap->{zeroTarget};
1339 0 $gap->{targetWords}[$ind]=$al->{targetWords}[$i];
1340 0 ▶ if ($targetInGap{$i}){
Branch: 1 branch
BranchTrueFalseTotal
if ($targetInGap{$i}) 0 0 0
1341 0 @linked=();
1342 0 foreach $j (@{$al->{targetAl}[$i]}){
1343 #if ($sourceInGap{$j}) { #useless:de facto included in the zone
1344 push @linked,$j-$gap->{zeroSource}
1345 #}
1346 0 }
1347 0 $gap->{targetAl}[$ind]=[@linked];
1348 }
1349 }
1350 0 return $gap;
1351 }
1352  
1353 #####################################################
1354 ### PRIVATE SUBS ###
1355 #####################################################
1356  
1357 # Returns the number of times the link ($ind1,$ind2) is present in the $side alignment
1358 132 ▶sub isIn {
Subroutine isIn: called CC=2 SCAR=8.3
Pod undocumented
1359 132 my ($al,$side,$ind1,$ind2) = @_;
1360 132 ▶ if ($side eq "sourceAl"){
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq 'sourceAl') { } 132 0 132
1361 # returns >0 if the link (j,i) is present in sourceAl (ie if i_partOf_Bj), 0 otherwise
1362 132 my ($j,$i) = ($ind1,$ind2);
1363 132 my $i_partOf_Bj=grep /^$i$/, @{$al->{sourceAl}[$j]};
1364 132 return $i_partOf_Bj;
1365 }else{
1366 # returns >0 if the link (i,j) is present in targetAl (ie if j_partOf_Bi), 0 otherwise
1367 0 my ($i,$j)=($ind1,$ind2);
1368 0 my $j_partOf_Bi = grep /^$j$/, @{$al->{targetAl}[$i]};
1369 0 return $j_partOf_Bi;
1370 }
1371 }
1372  
1373 # returns an object with same content as the input object
1374 0 ▶sub clone {
Subroutine clone: not called CC=5 SCAR=34.0
Pod undocumented
1375 0 my $al = shift;
1376 0 my $clone = Lingua::Alignment->new;
1377 0 my ($i,$j);
1378 0 @{$clone->{sourceWords}}=@{$al->{sourceWords}};
1379 0 @{$clone->{targetWords}}=@{$al->{targetWords}};
1380 0 ▶ for ($j=0;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
1381 0 ▶ if (defined($al->{sourceAl}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"sourceAl"}[$j]) 0 0 0
1382 0 push @{$clone->{sourceAl}},[@{$al->{sourceAl}[$j]}];
1383 }
1384 }
1385 0 ▶ for ($i=0;$i<@{$al->{targetAl}};$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$al->{"targetAl"};}) 0 0 0
1386 0 ▶ if (defined($al->{targetAl}[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"targetAl"}[$i]) 0 0 0
1387 0 push @{$clone->{targetAl}},[@{$al->{targetAl}[$i]}];
1388 }
1389 }
1390 0 %{$clone->{sourceLinks}}=%{$al->{sourceLinks}};
1391 0 %{$clone->{targetLinks}}=%{$al->{targetLinks}};
1392  
1393 0 return $clone;
1394 }
1395 0 ▶sub clear {
Subroutine clear: not called CC=5 SCAR=34.0
Pod undocumented
1396 0 my $al = shift;
1397 0 my ($i,$j);
1398 0 ▶ for ($j=0;$j<@{$al->{sourceAl}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$al->{"sourceAl"};}) 0 0 0
1399 0 ▶ if (defined($al->{sourceAl}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"sourceAl"}[$j]) 0 0 0
1400 0 @{$al->{sourceAl}[$j]} = ();
1401 }
1402 }
1403 0 ▶ for ($i=0;$i<@{$al->{targetAl}};$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$al->{"targetAl"};}) 0 0 0
1404 0 ▶ if (defined($al->{targetAl}[$i])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $al->{"targetAl"}[$i]) 0 0 0
1405 0 @{$al->{targetAl}[$i]} = ();
1406 }
1407 }
1408 0 %{$al->{sourceLinks}} = ();
1409 0 %{$al->{targetLinks}} = ();
1410 }
1411  
1412  
1413 # gets the alignment as clusters of positions aligned together
1414 # input: $al, $direction ("source" for "sourceAl" or "target" for "targetAl")
1415 0 ▶sub getAlClusters {
Subroutine getAlClusters: not called CC=20 SCAR=60.4
Pod undocumented
1416 0 my ($al,$direction)=@_;
1417 #default:
1418 0 ▶ if (!defined($direction)){$direction="source"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $direction) 0 0 0
1419  
1420 0 my $dumper = new Dumpvalue;
1421 # group many-to-many linked phrased in clusters
1422 0 my %scomp; #stores in which cluster is each source word position
1423 my %tcomp;
1424 0 my @clusters;
1425 0 my $alClusters={};
1426 0 my $numClusters=0;
1427
1428 0 ▶ for (my $j=1;$j<@{$al->{$direction."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$direction . 'Al'};}) 0 0 0
1429 0 ▶ if (defined($al->{$direction."Al"}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$al{$direction . 'Al'}[$j]) 0 0 0
1430 0 foreach my $i (@{$al->{$direction."Al"}[$j]}){
1431 0 ▶ if ($i>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
1432 # print "j: $j i: $i\n";
1433 0 ▶ if (exists($scomp{$j}) || exists($tcomp{$i})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $scomp{$j} or exists $tcomp{$i}) { } 0 0 0
Condition0%0/3
exists $scomp{$j} or exists $tcomp{$i}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $scomp{$j} or exists $tcomp{$i}
exists $scomp{$j} exists $tcomp{$i}
Truth table0%0/3
A or B
A= exists $scomp{$j}B= exists $tcomp{$i}
ABresult
1X1
011
000
1434 0 my ($clustIndex1,$clustIndex2);
1435 0 ▶ if (exists($scomp{$j}) && exists($tcomp{$i})){
Branch: 3 branches
BranchTrueFalseTotal
if (exists $scomp{$j} and exists $tcomp{$i}) { } 0 0 0
elsif (exists $scomp{$j}) { } 0 0 0
elsif (exists $tcomp{$i}) { } 0 0 0
Condition0%0/3
exists $scomp{$j} and exists $tcomp{$i}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $scomp{$j} and exists $tcomp{$i}
exists $scomp{$j} exists $tcomp{$i}
Truth table0%0/3
A and B
A= exists $scomp{$j}B= exists $tcomp{$i}
ABresult
0X0
100
111
1436 0 ▶ if ($tcomp{$i} != $scomp{$j}){
Branch: 1 branch
BranchTrueFalseTotal
if ($tcomp{$i} != $scomp{$j}) 0 0 0
1437 # merge clusters:
1438 0 ▶ if ($scomp{$j}<$tcomp{$i}){
Branch: 1 branch
BranchTrueFalseTotal
if ($scomp{$j} < $tcomp{$i}) { } 0 0 0
1439 0 $clustIndex1=$scomp{$j};
1440 0 $clustIndex2=$tcomp{$i};
1441 }else{
1442 0 $clustIndex1=$tcomp{$i};
1443 0 $clustIndex2=$scomp{$j};
1444 }
1445 # print "clusters: $clustIndex1 $clustIndex2 :\n";
1446 0 push @{$clusters[$clustIndex1]->{source}},@{$clusters[$clustIndex2]->{source}};
1447 0 push @{$clusters[$clustIndex1]->{target}},@{$clusters[$clustIndex2]->{target}};
1448
1449 0 ▶ while ( my ($key,$val)=each (%scomp) ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %scomp) 0 0 0
1450 0 ▶ if ($val == $clustIndex2){$scomp{$key}=$clustIndex1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($val == $clustIndex2) 0 0 0
1451 0 ▶ if ($val > $clustIndex2) {$scomp{$key}=$scomp{$key}-1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $clustIndex2) 0 0 0
1452 }
1453 0 ▶ while ( my ($key,$val)=each (%tcomp) ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %tcomp) 0 0 0
1454 0 ▶ if ($val == $clustIndex2){$tcomp{$key}=$clustIndex1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($val == $clustIndex2) 0 0 0
1455 0 ▶ if ($val > $clustIndex2) {$tcomp{$key}=$tcomp{$key}-1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $clustIndex2) 0 0 0
1456 }
1457 0 splice @clusters,$clustIndex2,1;
1458 0 $numClusters--;
1459 }
1460 }elsif (exists($scomp{$j})){
1461 0 $clustIndex1=$scomp{$j};
1462 0 $tcomp{$i}=$clustIndex1;
1463 0 push @{$clusters[$clustIndex1]->{target}},$i;
1464 }elsif (exists($tcomp{$i})){
1465 0 $clustIndex1=$tcomp{$i};
1466 0 $scomp{$j}=$clustIndex1;
1467 0 push @{$clusters[$clustIndex1]->{source}},$j;
1468 }
1469 }else{
1470 0 push @clusters,{source=>[$j],target=>[$i]};
1471 0 $scomp{$j}=$numClusters;
1472 0 $tcomp{$i}=$numClusters;
1473 0 $numClusters++;
1474 }
1475 } #if $i>0
1476 # print "scomp:\n";
1477 # print $dumper->dumpValue(\%scomp);
1478 # print "tcomp:\n";
1479 # print $dumper->dumpValue(\%tcomp);
1480
1481 # print $dumper->dumpValue(\@clusters);
1482 }
1483 }
1484 }
1485 0 return \@clusters;
1486 }
1487  
1488 # prints a phrase given the side of alignment (source or target) and an array of positions of the phrase words
1489 0 ▶sub printPhrase {
Subroutine printPhrase: not called CC=2 SCAR=17.9
Pod undocumented
1490 0 my ($al,$source,$posArray)=@_;
1491 0 my @words;
1492 0 foreach my $pos (@$posArray){
1493 0 push @words,$al->{$source."Words"}[$pos];
1494 }
1495 0 return join(" ",@words);
1496 }
1497  
1498 # SELECT ONLY S LINKS
1499 0 ▶sub SLinks {
Subroutine SLinks: not called CC=7 SCAR=40.3
Pod undocumented
1500 0 my $al=shift;
1501 0 my $sal = Lingua::Alignment->new;
1502 0 @{$sal->{sourceWords}}=@{$al->{sourceWords}};
1503 0 @{$sal->{targetWords}}=@{$al->{targetWords}};
1504  
1505 0 my %side=("source"=>"target","target"=>"source");
1506 0 ▶ while (my ($source,$target)= each(%side)){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %side) 0 0 0
1507 0 ▶ for (my $j=0;$j<@{$al->{$source."Al"}};$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$al{$source . 'Al'};}) 0 0 0
1508 0 push @{$sal->{$source."Al"}},[];
1509 0 ▶ if (defined($al->{$source."Al"}[$j])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$al{$source . 'Al'}[$j]) 0 0 0
1510 0 foreach my $i (@{$al->{$source."Al"}[$j]}){
1511 0 ▶ if ($al->{$source."Links"}->{$j." ".$i}[0] ne "p" && $al->{$source."Links"}->{$j." ".$i}[0] ne "P"){
Branch: 1 branch
BranchTrueFalseTotal
if ($$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "p" and $$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "P") 0 0 0
Condition0%0/3
$$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "p" and $$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "P"
!ll&&!rl&&r
000
MC/DC0%0/2
$$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "p" and $$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "P"
$$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "p" $$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "P"
Truth table0%0/3
A and B
A= $$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "p"B= $$al{$source . 'Links'}{$j . ' ' . $i}[0] ne "P"
ABresult
0X0
100
111
1512 0 push @{$sal->{$source."Al"}[$j]},$i;
1513 }
1514 }
1515 }
1516 }
1517 }
1518 0 return $sal;
1519 }
1520  
1521  
1522 1 1;
« AlignmentSet.pm ↑ Summary AlSetLib.pm »