blib/lib/Search/Tools/Query.pm

filter: Statementstmt 80.0% 16 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod n/a totaltotal 78.5% 22 / 28 SCAR 0.1
CC
1
Cov
80%
__ANON__
2.0
__ANON__
2.0
BEGIN
1.3
CRAP 1.0
« blib/lib/Search/Tools/Transliterate.pm ↑ Summary blib/lib/Search/Tools/HiLiter.pm »
1 package Search::Tools::Query;
2 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 extends 'Search::Tools::Object';
4 use overload
5 0 ▶ '""' => sub { $_[0]->str; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
6 0 ▶ 'bool' => sub {1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
7 11 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Search::Tools::RegEx;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Search::Tools::Tokenizer;
13 use Search::Tools::XML;
14  
15 use namespace::autoclean;
16  
17 our $VERSION = '1.004';
18  
19 has 'terms' => ( is => 'ro' );
20 has 'fields' => ( is => 'ro' );
21 has 'dialect' => ( is => 'ro' );
22 has 'str' => ( is => 'ro' );
23 has 'regex' => ( is => 'ro' );
24 has 'qp' => ( is => 'ro' );
25  
26 =head1 NAME
27  
28 Search::Tools::Query - objectified string for highlighting, snipping, etc.
29  
30 =head1 SYNOPSIS
31  
32 use Search::Tools::QueryParser;
33 my $qparser = Search::Tools::QueryParser->new;
34 my $query = $qparser->parse(q(the quick color:brown "fox jumped"));
35 my $fields = $query->fields; # ['color']
36 my $terms = $query->terms; # ['quick', 'brown', '"fox jumped"']
37 my $regex = $query->regex_for($terms->[0]); # S::T::RegEx
38 my $tree = $query->tree; # the Search::Query::Dialect tree()
39 print "$query\n"; # the quick color:brown "fox jumped"
40 print $query->str . "\n"; # same thing
41  
42  
43 =head1 DESCRIPTION
44  
45  
46 =head1 METHODS
47  
48 =head2 fields
49  
50 Array ref of fields from the original query string.
51 See Search::Tools::QueryParser for controls over ignore_fields().
52  
53 =head2 terms
54  
55 Array ref of key words from the original query string.
56 See Search::Tools::QueryParser for controls over ignore_fields()
57 and tokenizing regex.
58  
59 B<NOTE:>
60 Only positive words are extracted by QueryParser.
61 In other words, if you search for:
62  
63 foo not bar
64
65 then only C<foo> is returned. Likewise:
66  
67 +foo -bar
68
69 would return only C<foo>.
70  
71 =head2 str
72  
73 The original string.
74  
75 =head2 regex
76  
77 The hash ref of terms to Search::Tools::RegEx objects.
78  
79 =head2 dialect
80  
81 The internal Search::Query::Dialect object. See tree()
82 and str_clean() which delegate to the dialect object.
83  
84 =head2 qp
85  
86 The Search::Tools::QueryParser object used to generate the Query.
87  
88 =head2 num_terms
89  
90 Returns the number of terms().
91  
92 =cut
93  
94 sub num_terms {
95 return scalar @{ shift->{terms} };
96 }
97  
98 =head2 unique_terms
99  
100 Returns array ref of unique terms from query.
101 If stemming was on in the QueryParser,
102 all terms have already been stemmed as part
103 of the parsing process.
104  
105 =cut
106  
107 sub unique_terms {
108 my $self = shift;
109 my @t = @{ $self->{terms} };
110 my %uniq;
111 for my $t (@t) {
112 my $re = $self->regex_for($t);
113 if ( $re->is_phrase ) {
114 for my $pt ( @{ $re->phrase_terms } ) {
115 $uniq{ $pt->term }++;
116 }
117 }
118 else {
119 $uniq{ $re->term }++;
120 }
121 }
122 return [ keys %uniq ];
123 }
124  
125 =head2 num_unique_terms
126  
127 Returns number of unique_terms().
128  
129 =cut
130  
131 sub num_unique_terms {
132 return scalar( @{ $_[0]->unique_terms } );
133 }
134  
135 =head2 phrases
136  
137 Return array ref of RegEx objects for all terms where is_phrase
138 is true.
139  
140 =cut
141  
142 sub phrases {
143 my $self = shift;
144 my @p;
145 for my $t ( keys %{ $self->{regex} } ) {
146 if ( $self->{regex}->{$t}->is_phrase ) {
147 push @p, $self->{regex}->{$t};
148 }
149 }
150 return \@p;
151 }
152  
153 =head2 non_phrases
154  
155 Return array ref of RegEx objects for all terms where is_phrase
156 is false.
157  
158 =cut
159  
160 sub non_phrases {
161 my $self = shift;
162 my @p;
163 for my $t ( keys %{ $self->{regex} } ) {
164 if ( !$self->{regex}->{$t}->is_phrase ) {
165 push @p, $self->{regex}->{$t};
166 }
167 }
168 return \@p;
169 }
170  
171 =head2 tree
172  
173 Returns the internal Search::Query::Dialect tree().
174  
175 =cut
176  
177 sub tree {
178 my $self = shift;
179 return $self->dialect->tree();
180 }
181  
182 =head2 str_clean
183  
184 Returns the internal Search::Query::Dialect stringify().
185  
186 =cut
187  
188 sub str_clean {
189 my $self = shift;
190 return $self->dialect->stringify();
191 }
192  
193 =head2 regex_for(I<term>)
194  
195 Returns a Search::Tools::RegEx object for I<term>.
196  
197 =cut
198  
199 sub regex_for {
200 my $self = shift;
201 my $term = shift;
202 unless ( defined $term ) {
203 croak "term required";
204 }
205 my $regex = $self->{regex} or croak "regex not defined for query";
206 if ( !exists $regex->{$term} ) {
207 croak "no regex for $term";
208 }
209 return $regex->{$term};
210 }
211  
212 =head2 regexp_for
213  
214 Alias for regex_for(). The author has come to prefer "regex"
215 instead of "regexp" because it's one less keystroke.
216  
217 =cut
218  
219 *regexp_for = \&regex_for;
220  
221 =head2 matches_text( I<text> )
222  
223 Returns the number of matches for the query against I<text>.
224  
225 =head2 matches_html( I<html> )
226  
227 Returns the number of matches for the query against I<html>.
228  
229 =cut
230  
231 sub _matches_stemmed {
232 my $self = shift;
233 my $text = to_utf8( $_[0] );
234 my $count = 0;
235 my $qp = $self->qp;
236 my $stemmer = $qp->stemmer;
237 my $wildcard = $qp->wildcard;
238 my $tokenizer = Search::Tools::Tokenizer->new(
239 re => $qp->term_re,
240 debug => $self->debug,
241 );
242  
243 # stem the whole text, creating a new buffer to
244 # match against. This covers both the cases where
245 # a term is a phrase and where it is not.
246 my @buf;
247 my $buf_maker = sub {
248 push @buf, $stemmer->( $qp, $_[0]->str );
249 };
250 $tokenizer->tokenize( $text, $buf_maker );
251 my $new_text = join( " ", @buf );
252  
253 for my $term ( @{ $self->{terms} } ) {
254 my $re = $self->{regex}->{$term}->{plain};
255 $count += $new_text =~ m/$re/;
256 }
257 return $count;
258 }
259  
260 sub _matches {
261 my $self = shift;
262 my $style = shift;
263 my $text = to_utf8( $_[0] );
264 my $count = 0;
265 for my $term ( @{ $self->{terms} } ) {
266 my $regex = $self->{regex}->{$term}->{$style};
267 $count += $text =~ m/$regex/;
268 }
269 return $count;
270 }
271  
272 sub matches_text {
273 my $self = shift;
274 my $text = shift;
275 if ( !defined $text ) {
276 croak "text required";
277 }
278 return $self->_matches_stemmed($text) if $self->qp->stemmer;
279 return $self->_matches( 'plain', $text );
280 }
281  
282 sub matches_html {
283 my $self = shift;
284 my $html = shift;
285 if ( !defined $html ) {
286 croak "html required";
287 }
288 if ( $self->qp->stemmer ) {
289 return $self->_matches_stemmed( Search::Tools::XML->no_html($html) );
290 }
291 return $self->_matches( 'html', $html );
292 }
293  
294 =head2 terms_as_regex([I<treat_phrases_as_singles>])
295  
296 Returns all terms() as a single qr// regex, pipe-joined in a "OR"
297 logic.
298  
299 =cut
300  
301 sub terms_as_regex {
302 my $self = shift;
303 my $treat_phrases_as_singles = shift;
304 $treat_phrases_as_singles = 1 unless defined $treat_phrases_as_singles;
305 my $wildcard = $self->qp->wildcard;
306 my $wild_esc = quotemeta($wildcard);
307 my $wc = $self->qp->word_characters;
308 my @re;
309 for my $term ( @{ $self->{terms} } ) {
310  
311 my $q = quotemeta($term); # quotemeta speeds up the match, too
312 # even though we have to unquote below
313  
314 $q =~ s/\\$wild_esc/[$wc]*/g; # wildcard match is very approximate
315  
316 # treat phrases like OR'd words
317 # since that will just create more matches.
318 # if hiliting later, the phrase will be treated as such.
319 if ($treat_phrases_as_singles) {
320 $q =~ s/(\\ )+/\|/g;
321 }
322  
323 # if keeping phrases together use a less-naive regex instead of a space.
324 else {
325  
326 #$q = $self->regex_for($term)->plain();
327 #$q =~ s/(\\ )+/[^$wc]+/g;
328 }
329  
330 push( @re, $q );
331 }
332  
333 my $j = sprintf( '(%s)', join( '|', @re ) );
334 return qr/$j/i;
335 }
336  
337 1;
338  
339 __END__
« blib/lib/Search/Tools/Transliterate.pm ↑ Summary blib/lib/Search/Tools/HiLiter.pm »