blib/lib/Text/Index.pm

filter: Statementstmt 92.7% 77 / 83 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 88.7% 110 / 124 CC 13 SCAR 11.2
CC
13
Cov
88%
new
6.8
_search
5.0
add_keywords
3.0
CRAP 3.1
↑ Summary
1 package Text::Index;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Params::Util qw/_INSTANCE _ARRAY/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.01';
10  
11 =head1 NAME
12  
13 Text::Index - Create indices of a set of pages using a set of keywords
14  
15 =head1 SYNOPSIS
16  
17 use Text::Index;
18 my $index = Text::Index->new;
19
20 $index->add_page($content);
21 $index->add_pages(@strings);
22 my @pages = $index->pages;
23
24 # Add keyword with equivalent derivates
25 $index->add_keyword('Hamilton function', 'Hamiltonian');
26 $index->add_keywords([$keyword, @derivates], ...);
27 my @keywords = $i->keywords;
28 # ->keywords returns an array reference for each keyword
29 # (see ->add_keywords syntax)
30
31 my $index = $i->generate_index;
32
33 # Or for a single keyword:
34 my @page_list = $i->find_keyword($keyword);
35 my @page_list2 = $i->find_keyword($keyword, @derivates);
36  
37 =head1 DESCRIPTION
38  
39 This (simple) module searches for keywords in a set of pages and creates
40 an index.
41  
42 =head2 EXPORT
43  
44 None.
45  
46 =head2 METHODS
47  
48 This is a list of public methods.
49  
50 =over 2
51  
52 =item new
53  
54 Returns a new Text::Index object. When called on an
55 existing object, C<new> clones that object (deeply).
56  
57 =cut
58  
59 1 ▶sub new {
Subroutine new: called CC=4 SCAR=19.2
Pod documented
60 1 my $proto = shift;
61 1 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
62
63 1 my $self = {
64 keywords => {},
65 pages => [],
66 };
67
68 1 ▶ if (_INSTANCE($proto, __PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($proto, __PACKAGE__)) 0 1 1
69 0 @{$self->{pages}} = $proto->pages;
70 0 foreach ($proto->keywords) {
71 my $clone = {
72 key => $_->[0],
73 0 deriv => [ @{ $_->[1] } ],
74 };
75 0 $self->{keywords}{$_->[0]} = $clone;
76 }
77 }
78  
79 1 return bless $self => $class;
80 }
81  
82 =item add_page
83  
84 Adds a page to the index object. The page is expected to be
85 a string of text passed in as first argument.
86  
87 Returns the Text::Index object for convenience of
88 method chaining.
89  
90 =cut
91  
92 1 ▶sub add_page {
Subroutine add_page: called CC=1 SCAR=0.0
Pod documented
93 1 my $self = shift;
94 1 my $page = shift;
95 1 push @{$self->{pages}}, $page;
96 1 return $self;
97 }
98  
99 =item add_pages
100  
101 Adds a number of pages to the index object.
102  
103 All arguments are treated as pages. See C<add_page>.
104  
105 =cut
106  
107 1 ▶sub add_pages {
Subroutine add_pages: called CC=1 SCAR=0.0
Pod documented
108 1 my $self = shift;
109 1 push @{$self->{pages}}, @_;
110 1 return $self;
111 }
112  
113 =item pages
114  
115 Returns all registered pages as a list.
116  
117 =cut
118  
119 1 ▶sub pages {
Subroutine pages: called CC=1 SCAR=0.0
Pod documented
120 1 my $self = shift;
121 1 return @{$self->{pages}};
122 }
123  
124 =item add_keyword
125  
126 Adds a new keyword to the index. First argument must be the
127 keyword to add. Following the keyword may be any number of
128 alternative names / string which should be treated to be equal
129 to the keyword.
130  
131 Returns the Text::Index object for convenience.
132  
133 =cut
134  
135 4 ▶sub add_keyword {
Subroutine add_keyword: called CC=2 SCAR=7.0
Pod documented
136 4 my $self = shift;
137 4 my $keyword = shift;
138 4 my @deriv = @_;
139  
140 4 ▶ croak("add_keyword requires a keyword as first argument.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyword 0 4 4
141 if not defined $keyword;
142
143 4 $self->{keywords}{$keyword} = {
144 key => $keyword, deriv => \@deriv,
145 };
146
147 4 return $self;
148 }
149  
150 =item add_keywords
151  
152 Works like C<add_keyword> except that its arguments must be
153 a number of array references each referencing an array containing
154 a keyword and its associated derivates.
155  
156 Returns the Text::Index object for convenience.
157  
158 =cut
159  
160 1 ▶sub add_keywords {
Subroutine add_keywords: called CC=3 SCAR=11.1
Pod documented
161 1 my $self = shift;
162 croak("add_keywords takes only array references as arguments")
163 1 ▶ if grep {!_ARRAY($_)} @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not &_ARRAY($_);} @_ 0 1 1
164 1 $self->add_keyword(@$_) for @_;
165 1 return $self;
166 }
167  
168  
169 =item keywords
170  
171 Returns all registered keywords as a list of array references.
172 Each of those references an array containing the keyword followed
173 by any possible derivates.
174  
175 =cut
176  
177 1 ▶sub keywords {
Subroutine keywords: called CC=1 SCAR=0.0
Pod documented
178 1 my $self = shift;
179 1 return( map {[$_->{key}, @{$_->{deriv}}]} values(%{$self->{keywords}}) );
180 }
181  
182  
183 6 ▶sub _search {
Subroutine _search: called CC=5 SCAR=16.1
184 6 my $self = shift;
185 6 my $key = shift;
186 6 my $pages = $self->{pages};
187  
188 my @regexes = map {
189 6 my @w = map {quotemeta($_)} split /\s+/, $_;
190 9 my $str = join '\s+', @w;
191 9 qr/$str/i
192 } @$key;
193
194 6 my @onpage;
195
196 6 foreach my $page_no (1..@$pages) {
197 18 my $page = $pages->[$page_no-1];
198 18 ▶ study($page) if @regexes > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @regexes > 1 9 9 18
199 18 foreach my $regex (@regexes) {
200 22 ▶ push(@onpage, $page_no), last if $page =~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
if $page =~ /$regex/ 14 8 22
201 }
202 }
203  
204 6 return @onpage;
205 }
206  
207 =item generate_index
208  
209 Generates an index from the registered keywords and pages.
210 It returns an index of the form:
211  
212 {
213 'keyword' => [ @pages_containing_keyword ],
214 ...
215 }
216  
217 The search for the keywords is performed case and whitespace insensitively.
218  
219 =cut
220  
221 1 ▶sub generate_index {
Subroutine generate_index: called CC=2 SCAR=6.9
Pod documented
222 1 my $self = shift;
223  
224 1 my $index = {};
225 1 foreach my $key (values %{$self->{keywords}}) {
226 $index->{$key->{key}} = [
227 4 $self->_search( [ $key->{key}, @{$key->{deriv}} ] )
228 ];
229 }
230 1 return $index;
231 }
232  
233  
234  
235 =item find_keyword
236  
237 This method works like C<generate_index> only that it searches for
238 just one keyword which is provided as argument in the style of
239 C<add_keyword>. It ignores any registered keywords and searches just
240 for the one given as argument.
241  
242 Returns a list of page number on which the keyword was found. The
243 list will be the empty list if the keyword wasn't found at all.
244  
245 =cut
246  
247 2 ▶sub find_keyword {
Subroutine find_keyword: called CC=2 SCAR=7.0
Pod documented
248 2 my $self = shift;
249 2 my $key = shift;
250 2 my @deriv = @_;
251
252 2 ▶ croak("keyword requires a keyword as first argument.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 2 2
253 if not defined $key;
254
255 2 push @deriv, $key;
256  
257 2 return $self->_search(\@deriv);
258 }
259  
260 3 1;
261  
262 __END__
↑ Summary