| 1 | package Search::Tools::Query; | ||||||||||
| 2 | 26 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 26 | extends 'Search::Tools::Object'; | |||||||||
| 4 | use overload | ||||||||||
| 5 | 9 | ▶ | '""' => sub { $_[0]->str; }, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 36 | ▶ | 'bool' => sub {1}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 26 | fallback => 1; | |||||||||
| 8 | 26 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 26 | ▶ | use Data::Dump qw( dump ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 26 | ▶ | use Search::Tools::RegEx; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 26 | ▶ | use Search::Tools::UTF8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 26 | ▶ | use Search::Tools::Tokenizer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 26 | ▶ | use Search::Tools::XML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 26 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 26 | our $VERSION = '1.007'; | |||||||||
| 18 | |||||||||||
| 19 | 26 | has 'terms' => ( is => 'ro' ); | |||||||||
| 20 | 26 | has 'fields' => ( is => 'ro' ); | |||||||||
| 21 | 26 | has 'dialect' => ( is => 'ro' ); | |||||||||
| 22 | 26 | has 'str' => ( is => 'ro' ); | |||||||||
| 23 | 26 | has 'regex' => ( is => 'ro' ); | |||||||||
| 24 | 26 | 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 | 38 | ▶ | sub num_terms { | ||||||||
|
Subroutine
num_terms: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | 38 | 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 | 2 | ▶ | sub unique_terms { | ||||||||
|
Subroutine
unique_terms: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 108 | 2 | my $self = shift; | |||||||||
| 109 | 2 | my @t = @{ $self->{terms} }; | |||||||||
| 110 | 2 | my %uniq; | |||||||||
| 111 | 2 | for my $t (@t) { | |||||||||
| 112 | 4 | my $re = $self->regex_for($t); | |||||||||
| 113 | 4 | ▶ | if ( $re->is_phrase ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 4 | for my $pt ( @{ $re->phrase_terms } ) { | |||||||||
| 115 | 8 | $uniq{ $pt->term }++; | |||||||||
| 116 | } | ||||||||||
| 117 | } | ||||||||||
| 118 | else { | ||||||||||
| 119 | 0 | $uniq{ $re->term }++; | |||||||||
| 120 | } | ||||||||||
| 121 | } | ||||||||||
| 122 | 2 | return [ keys %uniq ]; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | =head2 num_unique_terms | ||||||||||
| 126 | |||||||||||
| 127 | Returns number of unique_terms(). | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||
| 131 | 2 | ▶ | sub num_unique_terms { | ||||||||
|
Subroutine
num_unique_terms: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 132 | 2 | 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 | 30 | ▶ | sub phrases { | ||||||||
|
Subroutine
phrases: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 143 | 30 | my $self = shift; | |||||||||
| 144 | 30 | my @p; | |||||||||
| 145 | 30 | for my $t ( keys %{ $self->{regex} } ) { | |||||||||
| 146 | 57 | ▶ | if ( $self->{regex}->{$t}->is_phrase ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 21 | push @p, $self->{regex}->{$t}; | |||||||||
| 148 | } | ||||||||||
| 149 | } | ||||||||||
| 150 | 30 | 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 | 0 | ▶ | sub non_phrases { | ||||||||
|
Subroutine
non_phrases: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 161 | 0 | my $self = shift; | |||||||||
| 162 | 0 | my @p; | |||||||||
| 163 | 0 | for my $t ( keys %{ $self->{regex} } ) { | |||||||||
| 164 | 0 | ▶ | if ( !$self->{regex}->{$t}->is_phrase ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 0 | push @p, $self->{regex}->{$t}; | |||||||||
| 166 | } | ||||||||||
| 167 | } | ||||||||||
| 168 | 0 | return \@p; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | =head2 tree | ||||||||||
| 172 | |||||||||||
| 173 | Returns the internal Search::Query::Dialect tree(). | ||||||||||
| 174 | |||||||||||
| 175 | =cut | ||||||||||
| 176 | |||||||||||
| 177 | 1 | ▶ | sub tree { | ||||||||
|
Subroutine
tree: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 178 | 1 | my $self = shift; | |||||||||
| 179 | 1 | return $self->dialect->tree(); | |||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | =head2 str_clean | ||||||||||
| 183 | |||||||||||
| 184 | Returns the internal Search::Query::Dialect stringify(). | ||||||||||
| 185 | |||||||||||
| 186 | =cut | ||||||||||
| 187 | |||||||||||
| 188 | 1 | ▶ | sub str_clean { | ||||||||
|
Subroutine
str_clean: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 189 | 1 | my $self = shift; | |||||||||
| 190 | 1 | 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 | 116 | ▶ | sub regex_for { | ||||||||
|
Subroutine
regex_for: called CC=4 SCAR=15.5
Pod
documented
| |||||||||||
| 200 | 116 | my $self = shift; | |||||||||
| 201 | 116 | my $term = shift; | |||||||||
| 202 | 116 | ▶ | unless ( defined $term ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 0 | croak "term required"; | |||||||||
| 204 | } | ||||||||||
| 205 | 116 | ▶ | my $regex = $self->{regex} or croak "regex not defined for query"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 206 | 116 | ▶ | if ( !exists $regex->{$term} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 207 | 0 | croak "no regex for $term"; | |||||||||
| 208 | } | ||||||||||
| 209 | 116 | 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 | 26 | *regexp_for = \®ex_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 | 4 | ▶ | sub _matches_stemmed { | ||||||||
|
Subroutine
_matches_stemmed: called CC=2 SCAR=6.9
| |||||||||||
| 232 | 4 | my $self = shift; | |||||||||
| 233 | 4 | my $text = to_utf8( $_[0] ); | |||||||||
| 234 | 4 | my $count = 0; | |||||||||
| 235 | 4 | my $qp = $self->qp; | |||||||||
| 236 | 4 | my $stemmer = $qp->stemmer; | |||||||||
| 237 | 4 | my $wildcard = $qp->wildcard; | |||||||||
| 238 | 4 | 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 | 4 | my @buf; | |||||||||
| 247 | 30 | ▶ | my $buf_maker = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 248 | 30 | push @buf, $stemmer->( $qp, $_[0]->str ); | |||||||||
| 249 | 4 | }; | |||||||||
| 250 | 4 | $tokenizer->tokenize( $text, $buf_maker ); | |||||||||
| 251 | 4 | my $new_text = join( " ", @buf ); | |||||||||
| 252 | |||||||||||
| 253 | 4 | for my $term ( @{ $self->{terms} } ) { | |||||||||
| 254 | 4 | my $re = $self->{regex}->{$term}->{plain}; | |||||||||
| 255 | 4 | $count += $new_text =~ m/$re/; | |||||||||
| 256 | } | ||||||||||
| 257 | 4 | return $count; | |||||||||
| 258 | } | ||||||||||
| 259 | |||||||||||
| 260 | 2 | ▶ | sub _matches { | ||||||||
|
Subroutine
_matches: called CC=2 SCAR=6.9
| |||||||||||
| 261 | 2 | my $self = shift; | |||||||||
| 262 | 2 | my $style = shift; | |||||||||
| 263 | 2 | my $text = to_utf8( $_[0] ); | |||||||||
| 264 | 2 | my $count = 0; | |||||||||
| 265 | 2 | for my $term ( @{ $self->{terms} } ) { | |||||||||
| 266 | 2 | my $regex = $self->{regex}->{$term}->{$style}; | |||||||||
| 267 | 2 | $count += $text =~ m/$regex/; | |||||||||
| 268 | } | ||||||||||
| 269 | 2 | return $count; | |||||||||
| 270 | } | ||||||||||
| 271 | |||||||||||
| 272 | 3 | ▶ | sub matches_text { | ||||||||
|
Subroutine
matches_text: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 273 | 3 | my $self = shift; | |||||||||
| 274 | 3 | my $text = shift; | |||||||||
| 275 | 3 | ▶ | if ( !defined $text ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 276 | 0 | croak "text required"; | |||||||||
| 277 | } | ||||||||||
| 278 | 3 | ▶ | return $self->_matches_stemmed($text) if $self->qp->stemmer; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 279 | 1 | return $self->_matches( 'plain', $text ); | |||||||||
| 280 | } | ||||||||||
| 281 | |||||||||||
| 282 | 3 | ▶ | sub matches_html { | ||||||||
|
Subroutine
matches_html: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 283 | 3 | my $self = shift; | |||||||||
| 284 | 3 | my $html = shift; | |||||||||
| 285 | 3 | ▶ | if ( !defined $html ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 286 | 0 | croak "html required"; | |||||||||
| 287 | } | ||||||||||
| 288 | 3 | ▶ | if ( $self->qp->stemmer ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 289 | 2 | return $self->_matches_stemmed( Search::Tools::XML->no_html($html) ); | |||||||||
| 290 | } | ||||||||||
| 291 | 1 | 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 | 31 | ▶ | sub terms_as_regex { | ||||||||
|
Subroutine
terms_as_regex: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 302 | 31 | my $self = shift; | |||||||||
| 303 | 31 | my $treat_phrases_as_singles = shift; | |||||||||
| 304 | 31 | ▶ | $treat_phrases_as_singles = 1 unless defined $treat_phrases_as_singles; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 305 | 31 | my $wildcard = $self->qp->wildcard; | |||||||||
| 306 | 31 | my $wild_esc = quotemeta($wildcard); | |||||||||
| 307 | 31 | my $wc = $self->qp->word_characters; | |||||||||
| 308 | 31 | my @re; | |||||||||
| 309 | 31 | for my $term ( @{ $self->{terms} } ) { | |||||||||
| 310 | |||||||||||
| 311 | 65 | my $q = quotemeta($term); # quotemeta speeds up the match, too | |||||||||
| 312 | # even though we have to unquote below | ||||||||||
| 313 | |||||||||||
| 314 | 65 | $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 | 65 | ▶ | if ($treat_phrases_as_singles) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 320 | 46 | $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 | 65 | push( @re, $q ); | |||||||||
| 331 | } | ||||||||||
| 332 | |||||||||||
| 333 | 31 | my $j = sprintf( '(%s)', join( '|', @re ) ); | |||||||||
| 334 | 31 | return qr/$j/i; | |||||||||
| 335 | } | ||||||||||
| 336 | |||||||||||
| 337 | 26 | 1; | |||||||||
| 338 | |||||||||||
| 339 | __END__ | ||||||||||