| 1 | package SWISH::Prog; | ||
| 2 | 6 | ▶ | use 5.008003; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 6 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 6 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 6 | ▶ | use base qw( SWISH::Prog::Class ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 6 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 6 | ▶ | use Data::Dump qw( dump ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 6 | ▶ | use Scalar::Util qw( blessed ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 6 | ▶ | use SWISH::Prog::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use SWISH::Prog::InvIndex; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use SWISH::Prog::ReplaceRules; | ||
| 12 | |||
| 13 | our $VERSION = '0.75'; | ||
| 14 | |||
| 15 | __PACKAGE__->mk_accessors( | ||
| 16 | qw( aggregator aggregator_opts indexer_opts test_mode )); | ||
| 17 | |||
| 18 | # each $swishProg hasa aggregator, which hasa indexer and hasa invindex | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | SWISH::Prog - information retrieval application framework | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use SWISH::Prog; | ||
| 29 | my $program = SWISH::Prog->new( | ||
| 30 | invindex => 'path/to/myindex', | ||
| 31 | aggregator => 'fs', | ||
| 32 | indexer => 'native', | ||
| 33 | config => 'some/swish/config/file', | ||
| 34 | filter => sub { print $_[0]->url . "\n" }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | $program->run('some/dir'); | ||
| 38 | |||
| 39 | print $program->count . " documents indexed\n"; | ||
| 40 | |||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | SWISH::Prog is a full-text search framework based on Swish-e | ||
| 45 | (L<http://swish-e.org/>). | ||
| 46 | |||
| 47 | SWISH::Prog tries to fill a niche similar to L<Data::SearchEngine> | ||
| 48 | or L<DBI>: providing a uniform and flexible interface to several | ||
| 49 | different search engine tools and libraries. | ||
| 50 | |||
| 51 | SWISH::Prog does B<not> try to replace the use of the underlying | ||
| 52 | search engine tools, but instead tries to fill in some usability | ||
| 53 | gaps and, like the DBI, make it relatively easy to switch | ||
| 54 | between backend tools without needing to re-write an entire | ||
| 55 | codebase. | ||
| 56 | |||
| 57 | SWISH::Prog implements all five basic components of a search | ||
| 58 | application: | ||
| 59 | |||
| 60 | =over | ||
| 61 | |||
| 62 | =item Aggregator | ||
| 63 | |||
| 64 | Gather a document collection. A collection might be a group | ||
| 65 | of HTML pages, or XML documents, or rows in a database. A collection | ||
| 66 | might originate from the web, a filesystem, a database, an email | ||
| 67 | inbox, or anywhere bytes are stored. An Aggregator gathers | ||
| 68 | those documents in a uniform way. | ||
| 69 | |||
| 70 | SWISH::Prog provides a variety of Aggregators, for filesystems, | ||
| 71 | email trees, spidering the web, pulling from databases, to name | ||
| 72 | a few. See L<SWISH::Prog::Aggregator> and its subclasses. | ||
| 73 | |||
| 74 | =item Normalizer | ||
| 75 | |||
| 76 | Documents come in a variety of formats (MIME types). A Normalizer | ||
| 77 | turns those disparate types into something text-based | ||
| 78 | and parseable. SWISH::Prog uses L<SWISH::Filter> to normalize | ||
| 79 | documents. | ||
| 80 | |||
| 81 | =item Parser/Analyzer | ||
| 82 | |||
| 83 | Documents are tokenized into "words" with attention to position, | ||
| 84 | context, length, encoding, and linguistic quality (stemming, | ||
| 85 | case, stopwords, etc.). | ||
| 86 | |||
| 87 | With the exception of the Native classes, | ||
| 88 | SWISH::Prog uses L<SWISH::3> to parse HTML and XML documents (the | ||
| 89 | most common normalized format for SWISH::Filter), and | ||
| 90 | then delegates further analysis (tokenization, etc) to | ||
| 91 | backend tools or libraries. | ||
| 92 | |||
| 93 | =item Indexer | ||
| 94 | |||
| 95 | Each L<SWISH::Prog::Indexer> subclass fronts an information retrieval (IR) | ||
| 96 | tool or library that implements its own proprietary, highly | ||
| 97 | optimized inverted index storage system that preserves the | ||
| 98 | intelligence of the Parser/Analyzer. | ||
| 99 | |||
| 100 | For example, the L<SWISH::Prog::Lucy::Indexer> is a wrapper around | ||
| 101 | L<Lucy::Index::Indexer>. L<SWISH::Prog::Native::Indexer> is a wrapper | ||
| 102 | around the C<swish-e> tool. | ||
| 103 | |||
| 104 | =item Searcher | ||
| 105 | |||
| 106 | Like the Indexer, each SWISH::Prog::Searcher subclass delegates | ||
| 107 | the searching of the inverted index to the backend IR tool | ||
| 108 | or library. | ||
| 109 | |||
| 110 | For example, the L<SWISH::Prog::Lucy::Searcher> is a wrapper | ||
| 111 | around L<Lucy::Search::PolySearcher>. | ||
| 112 | L<SWISH::Prog::Native::Searcher> is a wrapper | ||
| 113 | around the L<SWISH::API::More> module. | ||
| 114 | |||
| 115 | =back | ||
| 116 | |||
| 117 | =head1 BACKGROUND | ||
| 118 | |||
| 119 | The name "SWISH::Prog" comes from the Swish-e -S prog feature. | ||
| 120 | "prog" is short for "program". SWISH::Prog makes it easy to | ||
| 121 | write indexing and search programs. | ||
| 122 | |||
| 123 | SWISH::Prog started as a way of making the C<swish-e> binary | ||
| 124 | tool easier to integrate into Perl applications, and has since | ||
| 125 | been expanded as a full implementation of Swish3, with alternate | ||
| 126 | backend libraries (KinoSearch, Xapian, Apache Lucy, etc) | ||
| 127 | filling the Indexer and Searcher roles. | ||
| 128 | |||
| 129 | =head1 METHODS | ||
| 130 | |||
| 131 | All of the following methods may be overridden when subclassing | ||
| 132 | this module. | ||
| 133 | |||
| 134 | |||
| 135 | =head2 init | ||
| 136 | |||
| 137 | Overrides base SWISH::Prog::Class init() method. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | # allow for short names. we map to class->new | ||
| 142 | my %ashort = ( | ||
| 143 | fs => 'SWISH::Prog::Aggregator::FS', | ||
| 144 | mail => 'SWISH::Prog::Aggregator::Mail', | ||
| 145 | mailfs => 'SWISH::Prog::Aggregator::MailFS', | ||
| 146 | dbi => 'SWISH::Prog::Aggregator::DBI', | ||
| 147 | spider => 'SWISH::Prog::Aggregator::Spider', | ||
| 148 | object => 'SWISH::Prog::Aggregator::Object', | ||
| 149 | ); | ||
| 150 | my %ishort = ( | ||
| 151 | native => 'SWISH::Prog::Native::Indexer', | ||
| 152 | xapian => 'SWISH::Prog::Xapian::Indexer', | ||
| 153 | ks => 'SWISH::Prog::KSx::Indexer', | ||
| 154 | lucy => 'SWISH::Prog::Lucy::Indexer', | ||
| 155 | dbi => 'SWISH::Prog::DBI::Indexer', | ||
| 156 | ); | ||
| 157 | |||
| 158 | sub init { | ||
| 159 | my $self = shift; | ||
| 160 | my %arg = @_; | ||
| 161 | |||
| 162 | # no such method. just convenience. | ||
| 163 | my $filter = delete $arg{filter}; | ||
| 164 | |||
| 165 | $self->SUPER::init(%arg); | ||
| 166 | |||
| 167 | # search mode requires only invindex | ||
| 168 | if ( $self->{query} && !$self->{indexer} && !$self->{aggregator} ) { | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | # need to make sure we have an aggregator | ||
| 173 | # indexer and/or config might already be set in aggregator | ||
| 174 | # but if set here, we override. | ||
| 175 | |||
| 176 | my ( $aggregator, $indexer ); | ||
| 177 | |||
| 178 | # ok if undef | ||
| 179 | my $config = $self->{config}; | ||
| 180 | |||
| 181 | # get indexer | ||
| 182 | $indexer = $self->{indexer} || 'native'; | ||
| 183 | if ( $self->{aggregator} and blessed( $self->{aggregator} ) ) { | ||
| 184 | $indexer = $self->{aggregator}->indexer; | ||
| 185 | $config = $self->{aggregator}->config; | ||
| 186 | } | ||
| 187 | if ( !blessed($indexer) ) { | ||
| 188 | |||
| 189 | if ( exists $ishort{$indexer} ) { | ||
| 190 | $indexer = $ishort{$indexer}; | ||
| 191 | } | ||
| 192 | |||
| 193 | $self->debug and warn "creating indexer: $indexer"; | ||
| 194 | eval "require $indexer"; | ||
| 195 | if ($@) { | ||
| 196 | croak "invalid indexer $indexer: $@"; | ||
| 197 | } | ||
| 198 | my %indexer_opts = ( | ||
| 199 | debug => $self->debug, | ||
| 200 | invindex => $self->{invindex}, # may be undef | ||
| 201 | verbose => $self->verbose, | ||
| 202 | config => $config, # may be undef | ||
| 203 | test_mode => $self->test_mode, | ||
| 204 | %{ $self->indexer_opts || {} }, | ||
| 205 | ); | ||
| 206 | |||
| 207 | $self->debug and warn "indexer opts: " . dump( \%indexer_opts ); | ||
| 208 | |||
| 209 | $indexer = $indexer->new(%indexer_opts); | ||
| 210 | } | ||
| 211 | elsif ( !$indexer->isa('SWISH::Prog::Indexer') ) { | ||
| 212 | croak "$indexer is not a SWISH::Prog::Indexer-derived object"; | ||
| 213 | } | ||
| 214 | |||
| 215 | $aggregator = $self->{aggregator} || 'fs'; | ||
| 216 | my $aggregator_opts = $self->aggregator_opts || {}; | ||
| 217 | |||
| 218 | if ( !blessed($aggregator) ) { | ||
| 219 | |||
| 220 | if ( exists $ashort{$aggregator} ) { | ||
| 221 | $aggregator = $ashort{$aggregator}; | ||
| 222 | } | ||
| 223 | |||
| 224 | $self->debug and warn "creating aggregator: $aggregator"; | ||
| 225 | eval "require $aggregator"; | ||
| 226 | if ($@) { | ||
| 227 | croak "invalid aggregator $aggregator: $@"; | ||
| 228 | } | ||
| 229 | $aggregator = $aggregator->new( | ||
| 230 | indexer => $indexer, | ||
| 231 | debug => $self->debug, | ||
| 232 | verbose => $self->verbose, | ||
| 233 | test_mode => $self->test_mode, | ||
| 234 | %$aggregator_opts, | ||
| 235 | ); | ||
| 236 | } | ||
| 237 | elsif ( !$aggregator->isa('SWISH::Prog::Aggregator') ) { | ||
| 238 | croak "$aggregator is not a SWISH::Prog::Aggregator-derived object"; | ||
| 239 | } | ||
| 240 | |||
| 241 | # set these now so we can call $self->config | ||
| 242 | $self->{aggregator} = $aggregator; | ||
| 243 | $self->{indexer} = $indexer; | ||
| 244 | |||
| 245 | # allow filter to be a file containing a sub ref | ||
| 246 | if ( $filter and -f $filter ) { | ||
| 247 | $filter = do $filter; | ||
| 248 | } | ||
| 249 | |||
| 250 | if ( $self->config and $self->config->ReplaceRules ) { | ||
| 251 | |||
| 252 | # create a CODE ref that uses the ReplaceRules | ||
| 253 | my $rr = $self->config->ReplaceRules; | ||
| 254 | my $rules = SWISH::Prog::ReplaceRules->new(@$rr); | ||
| 255 | if ($filter) { | ||
| 256 | my $filter_copy = $filter; | ||
| 257 | $filter = sub { | ||
| 258 | $_[0]->url( $rules->apply( $_[0]->url ) ); | ||
| 259 | $filter_copy->( $_[0] ); | ||
| 260 | }; | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | $filter = sub { | ||
| 264 | $_[0]->url( $rules->apply( $_[0]->url ) ); | ||
| 265 | }; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | if ($filter) { | ||
| 270 | $aggregator->set_filter($filter); | ||
| 271 | } | ||
| 272 | |||
| 273 | $indexer->{test_mode} = $self->{test_mode} | ||
| 274 | unless exists $indexer->{test_mode}; | ||
| 275 | $aggregator->{test_mode} = $self->{test_mode} | ||
| 276 | unless exists $aggregator->{test_mode}; | ||
| 277 | |||
| 278 | $self->debug and carp dump $self; | ||
| 279 | |||
| 280 | return $self; | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 filter( I<CODE ref> ) | ||
| 284 | |||
| 285 | Set in new(). See L<SWISH::Prog::Doc>. | ||
| 286 | |||
| 287 | Example: | ||
| 288 | |||
| 289 | my $prog = SWISH::Prog->new( | ||
| 290 | filter => { | ||
| 291 | my $doc = shift; | ||
| 292 | |||
| 293 | # alter url | ||
| 294 | my $url = $doc->url; | ||
| 295 | $url =~ s/my.foo.com/my.bar.org/; | ||
| 296 | $doc->url( $url ); | ||
| 297 | |||
| 298 | # alter content | ||
| 299 | my $buf = $doc->content; | ||
| 300 | $buf =~ s/foo/bar/gi; | ||
| 301 | $doc->content( $buf ); | ||
| 302 | } | ||
| 303 | ); | ||
| 304 | |||
| 305 | The I<filter> value can also be the name of a file | ||
| 306 | that evals to a CODE ref. | ||
| 307 | |||
| 308 | =head2 aggregator( I<$swish_prog_aggregator> ) | ||
| 309 | |||
| 310 | Get the SWISH::Prog::Aggregator object. You should set this in new(). | ||
| 311 | |||
| 312 | =head2 aggregator_opts | ||
| 313 | |||
| 314 | Get the hashref of options passed internally to the B<aggregator> constructor. | ||
| 315 | |||
| 316 | =head2 indexer_opts | ||
| 317 | |||
| 318 | Get the hashref of options passed internally to the B<indexer> constructor. | ||
| 319 | |||
| 320 | =head2 run( I<collection> ) | ||
| 321 | |||
| 322 | Execute the program. This is an alias for index(). | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | *run = \&index; | ||
| 327 | |||
| 328 | =head2 index( I<collection> ) | ||
| 329 | |||
| 330 | Add items in I<collection> to the invindex(). | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub index { | ||
| 335 | my $self = shift; | ||
| 336 | my $aggregator = $self->aggregator or croak 'aggregator required'; | ||
| 337 | unless ( $aggregator->isa('SWISH::Prog::Aggregator') ) { | ||
| 338 | croak "aggregator is not a SWISH::Prog::Aggregator"; | ||
| 339 | } | ||
| 340 | |||
| 341 | $aggregator->indexer->start; | ||
| 342 | $aggregator->crawl(@_); | ||
| 343 | $aggregator->indexer->finish; | ||
| 344 | return $aggregator->indexer->count; | ||
| 345 | } | ||
| 346 | |||
| 347 | =head2 config | ||
| 348 | |||
| 349 | Returns the aggregator's config() object. | ||
| 350 | |||
| 351 | =cut | ||
| 352 | |||
| 353 | sub config { | ||
| 354 | my $self = shift; | ||
| 355 | if ( $self->aggregator ) { | ||
| 356 | return $self->aggregator->config; | ||
| 357 | } | ||
| 358 | if ( $self->indexer ) { | ||
| 359 | return $self->indexer->config; | ||
| 360 | } | ||
| 361 | return undef; | ||
| 362 | } | ||
| 363 | |||
| 364 | =head2 invindex | ||
| 365 | |||
| 366 | Returns the indexer's invindex. | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub invindex { | ||
| 371 | my $self = shift; | ||
| 372 | if ( $self->aggregator ) { | ||
| 373 | return $self->indexer->invindex; | ||
| 374 | } | ||
| 375 | return blessed( $self->{invindex} ) | ||
| 376 | ? $self->{invindex} | ||
| 377 | : SWISH::Prog::InvIndex->new( path => $self->{invindex} ); | ||
| 378 | } | ||
| 379 | |||
| 380 | =head2 indexer | ||
| 381 | |||
| 382 | Returns the indexer. | ||
| 383 | |||
| 384 | =cut | ||
| 385 | |||
| 386 | sub indexer { | ||
| 387 | shift->aggregator->indexer; | ||
| 388 | } | ||
| 389 | |||
| 390 | =head2 count | ||
| 391 | |||
| 392 | Returns the indexer's count. B<NOTE> This is the number of documents | ||
| 393 | actually indexed, not counting the number of documents considered and | ||
| 394 | discarded by the aggregator. If you want the number of documents | ||
| 395 | the aggregator looked at, regardless of whether they were indexed, | ||
| 396 | use the aggregator's count() method. | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | sub count { | ||
| 401 | shift->indexer->count; | ||
| 402 | } | ||
| 403 | |||
| 404 | =head2 test_mode | ||
| 405 | |||
| 406 | Dry run mode, just prints info on stderr but does not | ||
| 407 | build index. This flag is set in new() and passed to | ||
| 408 | the indexer and aggregator. | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | 1; | ||
| 413 | |||
| 414 | __END__ | ||