blib/lib/Text/CommonParts.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 12 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 97.5% 79 / 81 CC 15 SCAR 19.3
CC
15
Cov
99%
_common_parts
11.0
_get_candidates
5.0
BEGIN
1.0
CRAP 6.9
↑ Summary
1 package Text::CommonParts;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use vars qw($VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 $VERSION = 0.5;
8 4 @EXPORT_OK = qw(common_parts shortest_common_parts);
9  
10 =head1 NAME
11  
12 Text::CommonParts - return the common starting parts of phrases
13  
14 =head1 SYNOPSIS
15  
16 use Text::CommonParts qw(common_parts);
17  
18 # returns "sheep"
19 common_parts("sheep shearing", "sheep dipping", "sheep rustling");
20
21 # returns "sheep", "sheep shearing"
22 common_parts("sheep shearing", "sheep dipping", "sheep rustling", "sheep shearing shears");
23  
24 # returns "sheep"
25 shortest_common_parts("sheep shearing", "sheep dipping", "sheep rustling");
26
27 # returns "sheep"
28 shortest_common_parts("sheep shearing", "sheep dipping", "sheep rustling", "sheep shearing shears");
29
30  
31  
32  
33 =head1 METHODS
34  
35 =head2 common_parts <phrases>
36  
37 Takes a list of phrases and returns the longest common parts.
38  
39 If a phrase shares no common parts with any other phrases then it will be returned whole.
40  
41 Given a set of phrases which have a common prt and a subset of phrases that have a
42 longer common part then both parts will be returned. e.g given
43  
44 "something good", "something bad", "something in the woodshed", "something in my eye"
45  
46 will return
47  
48 "something", "something in"
49  
50 =cut
51  
52 6 ▶sub common_parts {
Subroutine common_parts: called CC=1 SCAR=0.0
Pod documented
53 6 return _common_parts(1,@_);
54  
55 }
56  
57 =head2 shortest_common_parts <phrases>
58  
59 Same as common_parts but will not return subsets. e.g given
60  
61 "something good", "something bad", "something in the woodshed", "something in my eye"
62  
63 will just return
64  
65 "something"
66  
67  
68  
69 =cut
70  
71  
72  
73 6 ▶sub shortest_common_parts {
Subroutine shortest_common_parts: called CC=1 SCAR=0.0
Pod documented
74 6 return _common_parts(0,@_);
75 }
76  
77 12 ▶sub _common_parts {
Subroutine _common_parts: called CC=11 SCAR=24.0
78 12 my $longest = shift;
79 12 my @keys = sort _slength @_;
80 12 ▶ @keys = reverse @keys if !$longest;
Branch: 1 branch
BranchTrueFalseTotal
unless $longest 6 6 12
81  
82  
83 # this fetches a list of all candidate parts
84 12 my %candidates = _get_candidates($longest, @keys);
85  
86 12 my %seen;
87 my %results;
88  
89 12 my @cand_keys = sort _slength keys %candidates;
90 12 ▶ @cand_keys = reverse @cand_keys if !$longest;
Branch: 1 branch
BranchTrueFalseTotal
unless $longest 6 6 12
91  
92  
93 # note which phrases we've seen already
94 12 foreach my $cand (@cand_keys) {
95 24 my @phrases = @{$candidates{$cand}};
96 24 foreach my $match (@phrases) {
97 54 ▶ next if $seen{$match}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$match}++ 16 38 54
98 38 push @{$results{$cand}}, $match;
99 }
100 }
101  
102  
103 # clean up the results hash
104 12 my %tmp;
105 12 foreach my $result (keys %results) {
106 20 my @phrases = @{$results{$result}};
107 # we're golden if it's got more than two phrase attached
108 # Butif there's only one phrase since it will be
109 # the n-1th ngram of the phrases when we want the whole phrase
110 # (we'll deal with that later)
111 20 ▶ if (@phrases>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@phrases > 1) 12 8 20
112 12 $tmp{$result}++;
113 12 next;
114 }
115 # claim we've never seen it
116 8 delete $seen{$_} for @phrases;
117  
118 }
119 12 %results = %tmp;
120 12 %tmp = ();
121  
122 # now get anything that hasn't been matches already
123 # i.e get singletons
124 12 foreach my $key (@keys) {
125 38 ▶ next if $seen{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key} 30 8 38
126 8 $results{$key}++;
127 }
128  
129 12 return keys %results;
130 }
131  
132 12 ▶sub _get_candidates {
Subroutine _get_candidates: called CC=5 SCAR=16.1
133 12 my @keys = @_;
134  
135 12 my %cands;
136 12 for my $key (@keys) {
137 50 my @so_far;
138 # split each phrase up into parts
139 50 foreach my $part (split ' ', $key) {
140 104 push @so_far, $part;
141 # make the sub part
142 104 my $match = join(" ",@so_far);
143 # we don't wnat whole matches yet
144 # since they'll always be the longest match
145 # we'll add whole phrases that share no common parts
146 # in later
147 104 ▶ next if $match eq $key && !exists $cands{$match};
Branch: 1 branch
BranchTrueFalseTotal
if $match eq $key and not exists $cands{$match} 50 54 104
Condition66%2/3
$match eq $key and not exists $cands{$match}
!ll&&!rl&&r
54050
MC/DC50%1/2
$match eq $key and not exists $cands{$match}
$match eq $key not exists $cands{$match}
Truth table66%2/3
A and not(B)
A= $match eq $keyB= not exists $cands{$match}
ABresult
0X0
100
111
148 # keep it for later
149 54 push @{$cands{$match}}, $key;
150 }
151 }
152 12 return %cands;
153 }
154  
155 # sort by length, longest first
156 53 ▶sub _slength ($$) {
Subroutine _slength: called CC=1 SCAR=0.0
157 53 return length($_[1]) <=> length($_[0]);
158 }
159  
160  
161  
162  
163  
164 =head1 AUTHOR
165  
166 Simon Wistow <simon@thegestalt.org
167  
168 =head1 COPYRIGHT
169  
170 Copyright 2006, Simon Wistow
171  
172 Distributed under the same terms as Perl itself
173  
174 =cut
175  
176 4 1;
↑ Summary