blib/lib/String/Compare.pm

filter: Statementstmt 100% 81 / 81 Branchbran 76.4% 26 / 34 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 6 / 6 totaltotal 93.9% 124 / 132 CC 21 SCAR 19.7
CC
21
Cov
93%
word_by_word
11.3
compare
6.0
char_by_char
6.0
CRAP 7.2
↑ Summary
1 package String::Compare;
2  
3 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 @EXPORT = qw(compare);
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 String::Compare - Compare two strings and return how much they are alike
10  
11 =head1 SYNOPSIS
12  
13 use String::Compare;
14 my $str1 = "J R Company";
15 my $str2 = "J. R. Company";
16 my $str3 = "J R Associates";
17 my $points12 = compare($str1,$str2);
18 my $points13 = compare($str1,$str3);
19 if ($points12 > $points13) {
20 print $str1." refers to ".$str2;
21 } else {
22 print $str1." refers to ".$str3;
23 }
24  
25 =head1 DESCRIPTION
26  
27 This module was created when I needed to merge the information
28 between two databases, and I had to find who were who in each database,
29 but the names weren't always equals, sometimes there were differences.
30  
31 The problem was that I need to choose the right person, so I must see how
32 much the different names are alike. I've tried testing char by char, but situations
33 like the described in the synopsis showed me that wasn't enough. So I created a
34 set of tests to give a more accurate pontuation of how much the names are alike.
35  
36 The result is in percentage. If the strings are exactly equal, it would return 1,
37 if they have nothing in common, it would return 0.
38  
39 =head1 METHODS
40  
41 =over
42  
43 =item compare($str1,$str2,%tests)
44  
45 This method receives the two strings and optionally the names and weights
46 of each test. The default behavior is to use all the tests with the weigth 1.
47 This method lowercases both strings, since case doesn't change the meaning
48 of the content. But each test is case sensitive, so if you like you must lc the strings.
49  
50 The current tests are (you can use the tests individually if you like:
51  
52 P.S.: You can use custom tests, because the tests are executed using eval,
53 so if you want a custom test, just use the full name of a method.
54  
55 P.S.2: If you created a test, please share it, sending me by email and I will be
56 glad to include it into the default set.
57  
58 =back
59  
60 =cut
61  
62 1 my %default_options =
63 (
64 char_by_char => 1,
65 consonants => 1,
66 vowels => 1,
67 word_by_word => 1,
68 chars_only => 1
69 );
70  
71 2 ▶sub compare {
Subroutine compare: called CC=6 SCAR=18.0
Pod documented
72 2 my $str1 = shift;
73 2 my $str2 = shift;
74  
75 2 $str1 = lc($str1);
76 2 $str2 = lc($str2);
77 # skip any tests if they are the same
78 2 ▶ return 1 if $str1 eq $str2;
Branch: 1 branch
BranchTrueFalseTotal
if $str1 eq $str2 0 2 2
79  
80 2 my %user_opt = @_;
81 2 my %opt = (%default_options, %user_opt);
82 2 my %results;
83 2 my $totalPoints = 0;
84 2 my $score = 0;
85 2 foreach my $test (keys %opt) {
86 10 $totalPoints += $opt{$test};
87 }
88 2 foreach my $test (keys %opt) {
89 10 ▶ next if $opt{$test} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{$test} == 0 0 10 10
90 10 ▶ my $result = __PACKAGE__->can($test)->($str1,$str2) || 0;
Condition100%2/2
__PACKAGE__->can($test)->($str1, $str2) || 0
l!l
91
MC/DC100%1/1
__PACKAGE__->can($test)->($str1, $str2) || 0
__PACKAGE__->can($test)->($str1, $str2)
Truth table100%2/2
A
A= __PACKAGE__->can($test)->($str1, $str2)
Aresult
11
00
91 10 $score += $result * $opt{$test}/$totalPoints;
92 }
93 2 return $score;
94 }
95  
96 =over
97  
98 =item char_by_char($str1,$str2)
99  
100 Tests character by character
101  
102 =back
103  
104 =cut
105  
106 26 ▶sub char_by_char {
Subroutine char_by_char: called CC=6 SCAR=17.9
Pod documented
107 26 my $str1 = shift;
108 26 my $str2 = shift;
109 26 my $size1 = length $str1;
110 26 my $size2 = length $str2;
111 26 my $score = 0;
112 26 ▶ my $size = $size1>$size2?$size1:$size2;
Branch: 1 branch
BranchTrueFalseTotal
$size1 > $size2 ? : 4 22 26
113 26 ▶ for (my $i = 0;$i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 121 13 134
114 121 ▶ if (length $str1 < $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str1 < $i) 9 112 121
115 9 last;
116 }
117 112 ▶ if (length $str2 < $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str2 < $i) 4 108 112
118 4 last;
119 }
120 108 my $c1 = substr $str1, $i, 1;
121 108 my $c2 = substr $str2, $i, 1;
122 108 ▶ if ($c1 eq $c2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c1 eq $c2) 38 70 108
123 38 $score += 1/$size;
124 }
125 }
126 26 return $score;
127 }
128  
129 =over
130  
131 =item consonants($str1,$str2)
132  
133 Test char_by_char only in the consonants.
134  
135 =back
136  
137 =cut
138  
139 1 *consoants = *consonants;
140 2 ▶sub consonants {
Subroutine consonants: called CC=1 SCAR=0.0
Pod documented
141 2 my $str1 = shift;
142 2 my $str2 = shift;
143 2 $str1 =~ s/[^bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ]//g;
144 2 $str2 =~ s/[^bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ]//g;
145 2 return char_by_char($str1,$str2);
146 }
147  
148 =over
149  
150 =item vowels($str1,$str2)
151  
152 Test char_by_char only in the vowels.
153  
154 =back
155  
156 =cut
157  
158 2 ▶sub vowels {
Subroutine vowels: called CC=1 SCAR=0.0
Pod documented
159 2 my $str1 = shift;
160 2 my $str2 = shift;
161 2 $str1 =~ s/[^aeiouyAEIOUY]//g;
162 2 $str2 =~ s/[^aeiouyAEIOUY]//g;
163 2 return char_by_char($str1,$str2);
164 }
165  
166 =over
167  
168 =item word_by_word($str1, $str2)
169  
170 Test char_by_char each word, giving points according to the
171 size of the word.
172  
173 =back
174  
175 =cut
176  
177 2 ▶sub word_by_word {
Subroutine word_by_word: called CC=11 SCAR=24.3
Pod documented
178 2 my $str1 = shift;
179 2 my $str2 = shift;
180 2 my @words1 = split(/\s+/,$str1);
181 2 my @words2 = split(/\s+/,$str2);
182 2 my $size1 = scalar @words1;
183 2 my $size2 = scalar @words2;
184 2 ▶ my $size = $size1>$size2?$size1:$size2;
Branch: 1 branch
BranchTrueFalseTotal
$size1 > $size2 ? : 0 2 2
185 2 my $score;
186 my $totalChars;
187 2 my @totalCharsPerWord;
188 2 ▶ for (my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 6 2 8
189 6 ▶ my $subsize1 = $i < $size1 ? length($words1[$i]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < $size1 ? : 6 0 6
190 6 ▶ my $subsize2 = $i < $size2 ? length($words2[$i]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < $size2 ? : 6 0 6
191 6 ▶ my $subsize = $subsize1 > $subsize2?$subsize1:$subsize2;
Branch: 1 branch
BranchTrueFalseTotal
$subsize1 > $subsize2 ? : 0 6 6
192 6 $totalChars += $subsize;
193 6 push @totalCharsPerWord, $subsize;
194 }
195 2 ▶ for (my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 6 2 8
196 6 ▶ last if $i >= $size1;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $size1 0 6 6
197 6 my $bestScore = 0;
198 6 ▶ for (my $j = 0; $j < $size; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $size) 18 6 24
199 18 ▶ last if $j >= $size2;
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $size2 0 18 18
200 18 my $result = char_by_char($words1[$i],$words2[$j]);
201 18 ▶ $bestScore = $result if $result > $bestScore;
Branch: 1 branch
BranchTrueFalseTotal
if $result > $bestScore 5 13 18
202 }
203 6 $score += $bestScore * $totalCharsPerWord[$i]/$totalChars;
204 }
205 2 return $score;
206 }
207  
208 =over
209  
210 =item chars_only($str1,$str2)
211  
212 Test char_by_char only with the characters matched by \w.
213  
214 =back
215  
216 =cut
217  
218 2 ▶sub chars_only {
Subroutine chars_only: called CC=1 SCAR=0.0
Pod documented
219 2 my $str1 = shift;
220 2 my $str2 = shift;
221 2 $str1 =~ s/\W//g;
222 2 $str2 =~ s/\W//g;
223 2 return char_by_char($str1,$str2);
224 }
225  
226  
227 =head1 COPYRIGHT
228  
229 This module was created by "Daniel Ruoso" <daniel@ruoso.com>. It is licensed under both
230 the GNU GPL and the Artistic License.
231  
232 =cut
233  
↑ Summary