In a recent post I discussed the creation of a stopwords database. The next step was to create a more reusable perl module to access that database. This isn't terribly complex: essentially we just want to know if a particular word is in the database or not. We are, however, going to use an object oriented interface.
package Search::StopWords;
use Carp;
use strict;
use DBI;
sub new {
my ($class, $database) = @_;
my $self = {};
unless (-e $database) { croak "$0: database file not found\n" }
$self->{database} = $database;
$self->{dbh} = DBI->connect(
'dbi:SQLite:dbname='.$self->{database}, '', '',
{RaiseError => 1}
) or croak "$0: database connection not made: $DBI::errstr\n";
bless $self, $class;
}
sub has {
my ($self, $word) = @_;
my $sth = $self->{dbh}->prepare('
SELECT count(*) FROM stopwords WHERE word = ?
');
$sth->execute(lc $word);
my @result = $sth->fetchrow_array;
$sth->finish();
return $result[0];
}
sub DESTROY {
my ($self) = @_;
$self->{dbh}->disconnect;
}
1;
Save this to a file called Search/StopWords.pm in your usual library location, then (assuming you have a stopwords database named /my/stopwords.db) you can use it like so...
my $stopwords = new Search::StopWords('/my/stopwords.db');
my $word = 'flibbit';
unless ($stopwords->has($word)) {
print "$word is searchable.";
}
