blib/lib/Text/Sentence/Alignment.pm

filter: Statementstmt 80.9% 106 / 131 Branchbran 82.8% 58 / 70 Conditioncond 83.3% 15 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 80.0% 8 / 10 Podpod 100% 7 / 7 totaltotal 81.7% 202 / 247 CC 44 SCAR 28.2
CC
44
Cov
82%
similarity_print
30.0
calculate_matrix
22.0
get_align_result
15.2
CRAP 16.8
↑ Summary
1 package Text::Sentence::Alignment;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Text::Sentence::Alignment - Two Sentence Alignment
10  
11 =head1 VERSION
12  
13 Version 0.12
14  
15 =cut
16  
17 5 our $VERSION = '0.12';
18  
19 =head1 SYNOPSIS
20  
21 This Module process two sentences (i.e. terms separated by space) alignment.
22 Now it provide two kind of alignment method, Global and Local Alignment.
23  
24 use Text::Sentence::Alignment;
25  
26 my $TSA = Text::Sentence::Alignment->new();
27  
28 # local alignment
29 $TSA->is_local(1);
30 my ($result1,$result2) = $TSA->do_alignment($s1,$s2);
31  
32 # global alignment
33 $TSA->is_local(0);
34 my ($result1,$result2) = $TSA->do_alignment($s1,$s2);
35  
36 =head1 FUNCTIONS
37  
38 =cut
39  
40 =head2 new
41
42 =cut
43  
44 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
45 3 my $class = shift;
46 3 my $self = {};
47 3 $self->{IS_LOCAL} = 0; # 0 for global alignment
48 3 $self->{DELIMETER} = '/'; # / for split tags
49 3 $self->{TABLE} = (); # Dynamic Programming Table
50 3 $self->{BEST} = (); # Best path, for local
51 3 $self->{max_len} = 0; # for global
52 3 $self->{SENARR1} = [];
53 3 $self->{SENARR2} = [];
54 3 $self->{has_tag} = 0; # if we need to do hierarchy match,
55 # e.g. POS, semantic tags
56 3 bless($self, $class);
57 3 return($self);
58 }
59  
60 =head2 is_local
61
62 Set/get if current algorithm is local alignment
63  
64 =cut
65  
66 2 ▶sub is_local {
Subroutine is_local: called CC=2 SCAR=7.0
Pod documented
67 2 my $self = shift;
68 2 ▶ if (@_) { $self->{IS_LOCAL} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
69 return $self->{IS_LOCAL}
70 2 }
71  
72 =head2 delimeter
73  
74 The delimeter() is used to set delimeter of word/tags.
75  
76 =cut
77  
78 0 ▶sub delimeter {
Subroutine delimeter: not called CC=2 SCAR=17.9
Pod documented
79 0 my $self = shift;
80 0 ▶ if (@_) { $self->{DELIMETER} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
81 return $self->{DELIMETER}
82 0 }
83  
84 =head2 do_alignment
85
86 =cut
87  
88 4 ▶sub do_alignment {
Subroutine do_alignment: called CC=3 SCAR=11.2
Pod documented
89 4 my $self = shift;
90 4 my $sen1 = shift;
91 4 my $sen2 = shift;
92 4 @{ $self->{SENARR1} } = split / /,$sen1;
93 4 @{ $self->{SENARR2} } = split / /,$sen2;
94 4 ▶ if ($sen1 =~ m/$self->{DELIMETER}/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($sen1 =~ /$self->{"DELIMETER"}/ or $sen2 =~ /$self->{"DELIMETER"}/) 0 4 4
Condition33%1/3
$sen1 =~ /$self->{"DELIMETER"}/ or $sen2 =~ /$self->{"DELIMETER"}/
l!l&&r!l&&!r
004
MC/DC0%0/2
$sen1 =~ /$self->{"DELIMETER"}/ or $sen2 =~ /$self->{"DELIMETER"}/
$sen1 =~ /$self->{"DELIMETER"}/ $sen2 =~ /$self->{"DELIMETER"}/
Truth table33%1/3
A or B
A= $sen1 =~ /$self->{"DELIMETER"}/B= $sen2 =~ /$self->{"DELIMETER"}/
ABresult
1X1
011
000
95 $sen2 =~ m/$self->{DELIMETER}/) {
96 0 $self->{has_tag} = 1;
97 }
98 4 $self->{TABLE} = ();
99 4 $self->{BEST} = ();
100 4 $self->{BEST}{MAX} = 0;
101 4 $self->{TABLE}{0}{0} = 0;
102 4 calculate_matrix($self);
103 # similarity_print();
104 4 return get_align_result($self);
105 }
106  
107 =head2 calculate_matrix
108
109 =cut
110  
111 4 ▶sub calculate_matrix {
Subroutine calculate_matrix: called CC=22 SCAR=30.9
Pod documented
112 4 my $self = shift;
113 4 my @sa1 = @{ $self->{SENARR1}};
114 4 my @sa2 = @{ $self->{SENARR2}};
115 4 ▶ if ($self->{IS_LOCAL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'IS_LOCAL'}) { } 2 2 4
116 2 $self->{max_len}= 0;
117 } else {
118 2 ▶ $self->{max_len} = scalar(@sa1) > scalar(@sa2) ? scalar(@sa1): scalar(@sa2); # for global
Branch: 1 branch
BranchTrueFalseTotal
scalar @sa1 > scalar @sa2 ? : 0 2 2
119 }
120 4 my ($len_s1, $len_s2) = (0,0); # length of s1/s2
121  
122 # print STDERR "max_len is ".$max_len."\n";
123 4 ▶ while ($len_s1 <= (scalar @sa1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s1 <= scalar @sa1) 36 4 40
124 36 ▶ while ($len_s2 <= scalar @sa2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s2 <= scalar @sa2) 408 36 444
125 408 my ($candidate1, $candidate2, $candidate3) = ($self->{max_len},$self->{max_len},$self->{max_len});
126 408 ▶ if ($len_s1 > 0 and $len_s2 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s1 > 0 and $len_s2 > 0) 339 69 408
Condition100%3/3
$len_s1 > 0 and $len_s2 > 0
!ll&&!rl&&r
3732339
MC/DC100%2/2
$len_s1 > 0 and $len_s2 > 0
$len_s1 > 0 $len_s2 > 0
Truth table100%3/3
A and B
A= $len_s1 > 0B= $len_s2 > 0
ABresult
0X0
100
111
127 # if match, we add 1 for local, 0 for global
128 # else (not matched), we add -1 for local, 1 for global
129 $candidate1 = int($self->{TABLE}{$len_s1-1}{$len_s2-1}) +
130 ( $self->{IS_LOCAL} ? 1: -1) *
131 ( $self->{has_tag} ?
132 how_similar($sa1[$len_s1-1], $sa2[$len_s2-1]) :
133 ( ( $sa1[$len_s1-1] eq $sa2[$len_s2-1] ) ?
134 339 ▶ 1+(-1+$self->{IS_LOCAL}) : -1 )
Branch: 3 branches
BranchTrueFalseTotal
$self->{'IS_LOCAL'} ? : 288 51 339
$self->{'has_tag'} ? : 0 339 339
$sa1[$len_s1 - 1] eq $sa2[$len_s2 - 1] ? : 38 301 339
135 )
136 ;
137 }
138 408 ▶ if ($len_s1 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s1 > 0) 371 37 408
139 $candidate2 = int($self->{TABLE}{$len_s1-1}{$len_s2}) +
140 371 ▶ ( $self->{IS_LOCAL} ? (-1) : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'IS_LOCAL'} ? : 312 59 371
141 }
142 408 ▶ if ($len_s2 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s2 > 0) 372 36 408
143 $candidate3 = int($self->{TABLE}{$len_s1}{$len_s2 - 1}) +
144 372 ▶ ( $self->{IS_LOCAL} ? (-1) : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'IS_LOCAL'} ? : 312 60 372
145 }
146 # print STDERR "setting ($len_s1,$len_s2)...";
147 # print STDERR "(".$candidate1."\t".$candidate2."\t".$candidate3.")\n";
148 408 ▶ if ($self->{IS_LOCAL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'IS_LOCAL'}) { } 338 70 408
149 338 ▶ $self->{TABLE}{$len_s1}{$len_s2} = max (
Branch: 1 branch
BranchTrueFalseTotal
if $len_s1 > 0 or $len_s2 > 0 336 2 338
Condition100%3/3
$len_s1 > 0 or $len_s2 > 0
l!l&&r!l&&!r
312242
MC/DC100%2/2
$len_s1 > 0 or $len_s2 > 0
$len_s1 > 0 $len_s2 > 0
Truth table100%3/3
A or B
A= $len_s1 > 0B= $len_s2 > 0
ABresult
1X1
011
000
150 $candidate1, $candidate2, $candidate3, 0
151 ) if ($len_s1 > 0 or $len_s2 > 0);
152 338 ▶ $self->{BEST}{X} = $len_s1 if $self->{BEST}{MAX} <= $self->{TABLE}{$len_s1}{$len_s2};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BEST"}{"MAX"} <= $self->{"TABLE"}{$len_s1}{$len_s2} 95 243 338
153 338 ▶ $self->{BEST}{Y} = $len_s2 if $self->{BEST}{MAX} <= $self->{TABLE}{$len_s1}{$len_s2};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BEST"}{"MAX"} <= $self->{"TABLE"}{$len_s1}{$len_s2} 95 243 338
154 338 ▶ $self->{BEST}{MAX} = $self->{TABLE}{$len_s1}{$len_s2} if $self->{BEST}{MAX} <= $self->{TABLE}{$len_s1}{$len_s2};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BEST"}{"MAX"} <= $self->{"TABLE"}{$len_s1}{$len_s2} 95 243 338
155 } else { # global
156 70 ▶ $self->{TABLE}{$len_s1}{$len_s2} = min (
Branch: 1 branch
BranchTrueFalseTotal
if $len_s1 > 0 or $len_s2 > 0 68 2 70
Condition100%3/3
$len_s1 > 0 or $len_s2 > 0
l!l&&r!l&&!r
5992
MC/DC100%2/2
$len_s1 > 0 or $len_s2 > 0
$len_s1 > 0 $len_s2 > 0
Truth table100%3/3
A or B
A= $len_s1 > 0B= $len_s2 > 0
ABresult
1X1
011
000
157 $candidate1, $candidate2, $candidate3
158 ) if ($len_s1 > 0 or $len_s2 > 0);
159 }
160 408 $len_s2 +=1;
161 }
162 36 $len_s2 = 0;
163 36 $len_s1 +=1;
164 }
165 }
166  
167 =head2 similarity_print
168  
169 =cut
170  
171 0 ▶sub similarity_print {
Subroutine similarity_print: not called CC=5 SCAR=34.0
Pod documented
172 0 my $self = shift;
173 0 my @sa1 = @{ $self->{SENARR1}};
174 0 my @sa2 = @{ $self->{SENARR2}};
175 0 print STDERR "\n \t \t".join("\t",@sa2)."\n";
176 0 for my $key (sort {int($a) <=> int($b)}(keys %{$self->{TABLE}})) {
177 0 ▶ print STDERR $sa1[$key-1]."\t" if $key > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $key > 0 0 0 0
178 0 ▶ print STDERR " \t" unless $key > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $key > 0 0 0 0
179 0 for my $subkey (sort {int($a) <=> int($b)} (keys %{$self->{TABLE}{$key}})) {
180 0 print STDERR $self->{TABLE}{$key}{$subkey}."\t";
181 }
182 0 print STDERR "\n";
183 }
184 };
185  
186 =head2 get_align_result
187  
188 =cut
189  
190 4 ▶sub get_align_result {
Subroutine get_align_result: called CC=15 SCAR=27.2
Pod documented
191 4 my $self = shift;
192 4 my @sa1 = @{ $self->{SENARR1}};
193 4 my @sa2 = @{ $self->{SENARR2}};
194 4 my ($i, $j) = (0, 0);
195 4 my (@as1, @as2);
196 4 my $baseline = 0;
197 4 my $first_round = 0;
198 4 ▶ if ($self->{IS_LOCAL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'IS_LOCAL'}) { } 2 2 4
199 2 $i = $self->{BEST}{X};
200 2 $j = $self->{BEST}{Y};
201 } else {
202 2 $i = scalar @sa1;
203 2 $j = scalar @sa2;
204 }
205 4 my $pi = $i + 2;
206 4 my $pj = $j + 2;
207 do {
208 30 ▶ if ($first_round) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_round) { } 0 30 30
209 0 push @as1, $sa1[$i];
210 0 push @as2, $sa2[$j];
211 0 $first_round = 0;
212 } else {
213 30 ▶ if ($i == ($pi-1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == $pi - 1) { } 23 7 30
elsif ($i == $pi) { } 3 4 7
214 23 push @as1, $sa1[$i];
215 }
216 elsif ($i == $pi) {
217 3 push @as1, "-";
218 }
219 30 ▶ if ($j == ($pj-1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($j == $pj - 1) { } 25 5 30
elsif ($j == $pj) { } 1 4 5
220 25 push @as2, $sa2[$j];
221 }
222 elsif ($j == $pj) {
223 1 push @as2, "-";
224 }
225 }
226 30 $pi = $i;
227 30 $pj = $j;
228 30 ▶ if ($self->{IS_LOCAL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'IS_LOCAL'}) { } 20 10 30
229 20 $baseline = max($self->{TABLE}{$i-1}{$j-1},$self->{TABLE}{$i-1}{$j},$self->{TABLE}{$i}{$j-1});
230 } else {
231 10 $baseline = min($self->{TABLE}{$i-1}{$j-1},$self->{TABLE}{$i-1}{$j},$self->{TABLE}{$i}{$j-1});
232 }
233 30 ▶ if ($self->{TABLE}{$i-1}{$j-1} == $baseline) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'TABLE'}{$i - 1}{$j - 1} == $baseline) { } 25 5 30
elsif ($self->{'TABLE'}{$i}{$j - 1} == $baseline) { } 4 1 5
elsif ($self->{'TABLE'}{$i - 1}{$j} == $baseline) { } 1 0 1
234 25 $i--;
235 25 $j--;
236 } elsif ($self->{TABLE}{$i}{$j-1} == $baseline) {
237 4 $j--;
238 } elsif ($self->{TABLE}{$i-1}{$j} == $baseline) {
239 1 $i--;
240 } else {
241 0 die $!;
242 }
243 4 ▶ } while ( $self->{TABLE}{$pi}{$pj} > 0 and $i >0 and $j > 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TABLE"}{$pi}{$pj} > 0 and $i > 0 and $j > 0 and do { do { if ($first_round) { push @as1, $sa1[$i]; push @as2, $sa2[$j]; $first_round = 0; } else { if ($i == $pi - 1) { push @as1, $sa1[$i]; } elsif ($i == $pi) { push @as1, "-"; } ; if ($j == $pj - 1) { push @as2, $sa2[$j]; } elsif ($j == $pj) { push @as2, "-"; } ; } ; $pi = $i; $pj = $j; if ($self->{"IS_LOCAL"}) { $baseline = &max($self->{"TABLE"}{$i - 1}{$j - 1}, $self->{"TABLE"}{$i - 1}{$j}, $self->{"TABLE"}{$i}{$j - 1}); } else { $baseline = &min($self->{"TABLE"}{$i - 1}{$j - 1}, $self->{"TABLE"}{$i - 1}{$j}, $self->{"TABLE"}{$i}{$j - 1}); } ; if ($self->{"TABLE"}{$i - 1}{$j - 1} == $baseline) { --$i; --$j; } elsif ($self->{"TABLE"}{$i}{$j - 1} == $baseline) { --$j; } elsif ($self->{"TABLE"}{$i - 1}{$j} == $baseline) { --$i; } else { die $!; } } } 26 4 30
Condition83%5/6
$self->{"TABLE"}{$pi}{$pj} > 0 and $i > 0 and $j > 0
!ll&&!rl&&r
4026
$self->{"TABLE"}{$pi}{$pj} > 0 and $i > 0
!ll&&!rl&&r
3126
MC/DC66%2/3
$self->{"TABLE"}{$pi}{$pj} > 0 and $i > 0 and $j > 0
$self->{"TABLE"}{$pi}{$pj} > 0 $i > 0 $j > 0
Truth table75%3/4
A and B and C
A= $self->{"TABLE"}{$pi}{$pj} > 0B= $i > 0C= $j > 0
ABCresult
0XX0
10X0
1100
1111
244 4 ▶ if (!$self->{IS_LOCAL}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"IS_LOCAL"}) 2 2 4
245 2 push @as1, $sa1[$i];
246 2 push @as2, $sa2[$j];
247 }
248 4 return ( join (" ",reverse @as1)."\t".join (" ",reverse @as2) );
249 }
250  
251 =head1 AUTHOR
252  
253 Cheng-Lung Sung, C<< <clsung@cpan.org> >>
254  
255 =head1 BUGS
256  
257 Please report any bugs or feature requests to
258 C<bug-text-sentence-alignment@rt.cpan.org>, or through the web interface at
259 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Sentence-Alignment>.
260 I will be notified, and then you'll automatically be notified of progress on
261 your bug as I make changes.
262  
263 =head1 ACKNOWLEDGEMENTS
264  
265 =head1 COPYRIGHT & LICENSE
266  
267 Copyright 2005 - 2007 Cheng-Lung Sung, All Rights Reserved.
268  
269 This program is free software; you can redistribute it and/or modify it
270 under the same terms as Perl itself.
271  
272 =cut
273  
274 5 1; # End of Text::Sentence::Alignment
↑ Summary