r9002@llin: dpavlin | 2005-11-21 15:43:39 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 21 Nov 2005 14:42:22 +0000 (14:42 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 21 Nov 2005 14:42:22 +0000 (14:42 +0000)
 Text::Iconv working, fixed tests, minor tweaks

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@81 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Search/Estraier.pm
t/7-est.t

index 52fbfc1..4e24997 100644 (file)
@@ -35,7 +35,7 @@ Connect to Hyper Estraier index using HTTP
        user => 'admin',
        passwd => 'admin',
        encoding => 'iso-8859-2',
-       log => $Log::Log4Perl->log_object,
+       log => $Log::Log4perl->log_object,
  );
 
 Options are:
@@ -60,6 +60,11 @@ character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
 (and it probably is). This encoding will be converted to C<UTF-8> for
 Hyper Estraier.
 
+=item log
+
+L<Log::Log4perl> object or equivalent (C<< $c->log >> can be used in
+L<Catalyst> and there is support for it).
+
 =back
 
 =cut
@@ -83,8 +88,8 @@ sub new {
        my $encoding = $self->{'encoding'} || 'ISO-8859-2';
        $log->info("using encoding $encoding");
 
-       $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or
-               $log->die("can't create conversion from $encoding to UTF-8");
+       $self->{'iconv'} = new Text::Iconv('UTF-8', $encoding) or
+               $log->die("can't create conversion from UTF-8 to $encoding");
 
        $self ? return $self : return undef;
 }
@@ -94,11 +99,13 @@ sub new {
 
 Locate items in index
 
-  $est->search(
+  my @results = $est->search(
        query => 'name of book or novel',
-       attr => [ qw/PersonalName Title/ ],
+       attr => qw/PersonalName TitleProper/,
   );
 
+Results are returned as hash array with keys named by attributes
+
 =cut
 
 sub search {
@@ -110,12 +117,8 @@ sub search {
 
        $log->logconfess('need db in object') unless ($self->{'db'});
        $log->logconfess('need attr') unless ($args->{'attr'});
-       $log->logconfess("need attr as array not " . ref($args->{'attr'}) ) unless (ref($args->{'attr'}) eq 'ARRAY');
 
-#      my $database = $self->{'database'} || $log->logconfess('no database in $self');
-#      foreach my $p (qw/id ds type/) {
-#              $log->logconfess("need $p") unless ($args->{$p});
-#      }
+       $log->logconfess("need attr as array not " . ref($args->{'attr'}) ) unless (ref($args->{'attr'}) eq 'ARRAY');
 
        my $cond = HyperEstraier::Condition->new();
 #      $cond->add_attr("filepath ISTRINC $q");
@@ -131,11 +134,9 @@ sub search {
        my $hits = $result->doc_num;
        $log->debug("found $hits hits");
 
-       my @attrs = $args->{'attr'} || $self->confess("need attr");
-
        my @results;
 
-       for my $i ( 0 .. $hits ) {
+       for my $i ( 0 .. ( $hits - 1 ) ) {
 
                $log->debug("get_doc($i)");
                my $doc = $result->get_doc( $i );
@@ -146,10 +147,10 @@ sub search {
 
                my $hash;
 
-               foreach my $attr (@attrs) {
+               foreach my $attr (@{ $args->{'attr'} }) {
                        my $val = $doc->attr( $attr );
                        $log->debug("attr $attr = ", $val || 'undef');
-                       $hash->{$attr} = $val if (defined($val));
+                       $hash->{$attr} = $self->{'iconv'}->convert( $val ) if (defined($val));
                }
 
                if ($hash) {
index 875b474..7b52ce4 100755 (executable)
--- a/t/7-est.t
+++ b/t/7-est.t
@@ -2,7 +2,7 @@
 
 use strict;
 
-use Test::More tests => 16;
+use Test::More tests => 6;
 use Test::Exception;
 use Cwd qw/abs_path/;
 use File::Temp qw/tempdir/;
@@ -26,15 +26,15 @@ ok(my $est = new WebPAC::Search::Estraier(
 ), "new");
 
 my $query = 'ivan';
-my $max = 3;
+my $max = 10;
 
 ok(my @res = $est->search(
        query => $query,
        max => $max,
-       attr => [ qw/PersonalName/ ],
+       attr => [ qw/PersonalName TitleProper/ ],
 ), "search $query, max: $max");
 
-cmp_ok($#res, '==', $max, "$max hits");
+cmp_ok(($#res + 1), '==', $max, "$max hits");
 
 diag Dumper(\@res);