| 1 | #AnyDBM handling from perlindex: | ||
| 2 | # NDBM_File as LAST resort | ||
| 3 | |||
| 4 | package | ||
| 5 | AnyDBM_File; # hide from indexer | ||
| 6 | use vars '@ISA'; | ||
| 7 | my @try; @try = qw(DB_File GDBM_File SDBM_File ODBM_File NDBM_File) unless @ISA; | ||
| 8 | my $mod; | ||
| 9 | for $mod (@try) { | ||
| 10 | if (eval "require $mod") { | ||
| 11 | @ISA = $mod; | ||
| 12 | last; | ||
| 13 | } | ||
| 14 | }; | ||
| 15 | |||
| 16 | package Tk::Pod::Search_db; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use vars qw($VERSION); | ||
| 20 | |||
| 21 | $VERSION = '5.09'; | ||
| 22 | |||
| 23 | use Carp; | ||
| 24 | use Fcntl; | ||
| 25 | use File::Basename qw(dirname); | ||
| 26 | use File::Spec; | ||
| 27 | use Text::English; | ||
| 28 | use Config; | ||
| 29 | |||
| 30 | my $PREFIX = $Config::Config{prefix}; | ||
| 31 | # Bug in perlindex: because of assuming Unix directory separators the | ||
| 32 | # index files are stored in man/man1, not in man on Windows: | ||
| 33 | my $IDXDIR = $^O eq 'MSWin32' ? $Config::Config{man1dir} : dirname $Config::Config{man1dir}; | ||
| 34 | $IDXDIR ||= $PREFIX; # use perl directory if no manual directory exists | ||
| 35 | # Debian uses a non-standard directory: | ||
| 36 | if (-e "/etc/debian_version" && -d "/var/cache/perlindex") { | ||
| 37 | $IDXDIR = "/var/cache/perlindex"; | ||
| 38 | # XXX What to do if perlindex is installed by the user and uses | ||
| 39 | # the man directory for storing the index files? | ||
| 40 | } | ||
| 41 | # Deliberately ignore the INDEXDIR environment variable which is used | ||
| 42 | # by perlindex | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $class = shift; | ||
| 46 | my $idir = shift; | ||
| 47 | $idir ||= $IDXDIR; | ||
| 48 | my (%self, %IF, %IDF, %FN); | ||
| 49 | my $if_file = File::Spec->catfile($idir, "index_if"); | ||
| 50 | tie (%IF, 'AnyDBM_File', $if_file, O_RDONLY, 0644) | ||
| 51 | or confess "Could not tie $if_file: $!\n". | ||
| 52 | "Did you install Text::English and run 'perlindex -index'?\n"; | ||
| 53 | my $idf_file = File::Spec->catfile($idir, "index_idf"); | ||
| 54 | tie (%IDF, 'AnyDBM_File', $idf_file, O_RDONLY, 0644) | ||
| 55 | or confess "Could not tie $idf_file: $!\n"; | ||
| 56 | my $fn_file = File::Spec->catfile($idir, "index_fn"); | ||
| 57 | tie (%FN, 'AnyDBM_File', $fn_file, O_RDONLY, 0644) | ||
| 58 | or confess "Could not tie $fn_file: $!\n"; | ||
| 59 | |||
| 60 | $self{IF} = \%IF; | ||
| 61 | $self{IDF} = \%IDF; | ||
| 62 | $self{FN} = \%FN; | ||
| 63 | #xxx: -idir depended but where can I get this info? | ||
| 64 | # o A fourth index file? | ||
| 65 | # o todo: check perlindex index routine | ||
| 66 | $self{PREFIX} = $PREFIX; | ||
| 67 | |||
| 68 | bless \%self, $class; | ||
| 69 | } | ||
| 70 | |||
| 71 | # changes to perlindex's normalize | ||
| 72 | # o removed useless(?) stemmer check | ||
| 73 | # o lexicalized $word | ||
| 74 | |||
| 75 | sub normalize { | ||
| 76 | my $line = join ' ', @_; | ||
| 77 | my @result; | ||
| 78 | |||
| 79 | $line =~ tr/A-Z/a-z/; | ||
| 80 | $line =~ tr/a-z0-9_/ /cs; | ||
| 81 | |||
| 82 | my $word; | ||
| 83 | for $word (split / /, $line ) { | ||
| 84 | $word =~ s/^\d+//; | ||
| 85 | next unless length($word) > 2; | ||
| 86 | push @result, &Text::English::stem($word); | ||
| 87 | } | ||
| 88 | @result; | ||
| 89 | } | ||
| 90 | |||
| 91 | # changes for perlindex's search slightly modified | ||
| 92 | sub searchWords { | ||
| 93 | my($self, $term, %args) = @_; | ||
| 94 | |||
| 95 | my @words = split / /, $term; | ||
| 96 | |||
| 97 | my $restrict_pod = $args{-restrictpod}; | ||
| 98 | if (defined $restrict_pod) { | ||
| 99 | my(@modparts) = split /::/, $restrict_pod; | ||
| 100 | $restrict_pod = join('[/\\\\]', map { quotemeta } @modparts); | ||
| 101 | } | ||
| 102 | |||
| 103 | #print "try words|", join('|',@_),"\n"; | ||
| 104 | my $p = 'w'; | ||
| 105 | my %score; | ||
| 106 | my %termhits; | ||
| 107 | my $maxhits = 50; | ||
| 108 | my (@unknown, @stop); | ||
| 109 | |||
| 110 | my $IF = $self->{IF}; | ||
| 111 | my $IDF = $self->{IDF}; | ||
| 112 | my $FN = $self->{FN}; | ||
| 113 | |||
| 114 | #&initstop if $opt_verbose; | ||
| 115 | for my $word (normalize(@words)) { | ||
| 116 | unless ($IF->{$word}) { | ||
| 117 | # if ($stop{$word}) { | ||
| 118 | # push @stop, $word; | ||
| 119 | # } else { | ||
| 120 | # push @unknown, $word; | ||
| 121 | # } | ||
| 122 | next; | ||
| 123 | } | ||
| 124 | my %post = unpack($p.'*',$IF->{$word}); | ||
| 125 | my $idf = log($FN->{'last'}/$IDF->{$word}); | ||
| 126 | for my $did (keys %post) { | ||
| 127 | my ($maxtf) = unpack($p, $FN->{$did}); | ||
| 128 | $score{$did} = 0 unless defined $score{$did}; # perl -w | ||
| 129 | $score{$did} += $post{$did} / $maxtf * $idf; | ||
| 130 | $termhits{$did}++; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | my @results; | ||
| 135 | for my $did (sort { $termhits{$b} <=> $termhits{$a} || $score{$b} <=> $score{$a} } keys %score) { | ||
| 136 | my ($mtf, $path) = unpack($p.'a*', $FN->{$did}); | ||
| 137 | # XXX Should not use Tk::Pod::Search::split_path, or split_path should be moved to another package | ||
| 138 | if ($restrict_pod) { | ||
| 139 | my($check_path) = Tk::Pod::Search::split_path($path); | ||
| 140 | next if $check_path !~ /^$restrict_pod/; | ||
| 141 | } | ||
| 142 | #next if ($restrict_pod && $path !~ /$restrict_pod/); | ||
| 143 | $path = File::Spec->catfile($self->prefix, $path) unless $^O eq 'MSWin32'; # This seems to be a perlindex bug in MSWin32 | ||
| 144 | push @results, { termhits => $termhits{$did}, score => $score{$did}, path => $path }; | ||
| 145 | last unless --$maxhits; | ||
| 146 | } | ||
| 147 | |||
| 148 | #print "results|", join('|',@results),"\n"; | ||
| 149 | @results; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub prefix { | ||
| 153 | shift->{PREFIX}; | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||
| 157 | __END__ |