From f75126490d45727b0b0594560c66138b0b345403 Mon Sep 17 00:00:00 2001 From: tipaul Date: Thu, 3 Oct 2002 11:28:18 +0000 Subject: [PATCH] Extending Context.pm to add stopword management and using it in MARC-API. First benchmarks show a medium speed improvement, which is nice as this part is heavily called. --- C4/Biblio.pm | 19 ++++++++++++------- C4/Context.pm | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 2df9f9645b..772ccbc9fb 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1,6 +1,10 @@ package C4::Biblio; # $Id$ # $Log$ +# Revision 1.14 2002/10/03 11:28:18 tipaul +# Extending Context.pm to add stopword management and using it in MARC-API. +# First benchmarks show a medium speed improvement, which is nice as this part is heavily called. +# # Revision 1.13 2002/10/02 16:26:44 tipaul # road to 1.3.1 # @@ -798,13 +802,14 @@ sub MARCaddword { $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g; my @words = split / /,$sentence; # build stopword list - my $sth2 =$dbh->prepare("select word from stopwords"); - $sth2->execute; - my $stopwords; - my $stopword; - while(($stopword) = $sth2->fetchrow_array) { - $stopwords->{$stopword} = $stopword; - } +# my $sth2 =$dbh->prepare("select word from stopwords"); +# $sth2->execute; +# my $stopwords; +# my $stopword; +# while(($stopword) = $sth2->fetchrow_array) { +# $stopwords->{$stopword} = $stopword; +# } + my $stopwords= C4::Context->stopwords; my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values (?,?,?,?,?,?,soundex(?))"); foreach my $word (@words) { diff --git a/C4/Context.pm b/C4/Context.pm index 4177f3d654..93005a86e4 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -182,6 +182,7 @@ sub new return undef if !defined($self->{"config"}); $self->{"dbh"} = undef; # Database handle + $self->{"stopwords"} = undef; # stopwords list bless $self, $class; return $self; @@ -422,6 +423,54 @@ sub restore_dbh # return something, then this function should, too. } +=item stopwords + + $dbh = C4::Context->stopwords; + +Returns a hash with stopwords. + +This hash is cached for future use: if you call +Cstopwords> twice, you will get the same hash without real DB access + +=cut +#' +sub stopwords +{ + my $retval = {}; + + # If the hash already exists, return it. + return $context->{"stopwords"} if defined($context->{"stopwords"}); + + # No hash. Create one. + $context->{"stopwords"} = &_new_stopwords(); + + return $context->{"stopwords"}; +} + +# _new_stopwords +# Internal helper function (not a method!). This creates a new +# hash with stopwords +sub _new_stopwords +{ + my $dbh = &dbh; + my $stopwordlist; + my $sth = $dbh->prepare("select word from stopwords"); + $sth->execute; + while (my $stopword = $sth->fetchrow_array) { + my $retval = {}; + $stopwordlist->{$stopword} = uc($stopword); + } + return $stopwordlist; +# my $db_driver = $context->{"config"}{"db_scheme"} || "mysql"; +# my $db_name = $context->{"config"}{"database"}; +# my $db_host = $context->{"config"}{"hostname"}; +# my $db_user = $context->{"config"}{"user"}; +# my $db_passwd = $context->{"config"}{"pass"}; + +# return DBI->connect("DBI:$db_driver:$db_name:$db_host", +# $db_user, $db_passwd); +} + 1; __END__ =back -- 2.39.5