home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / perlindex.bat < prev    next >
Encoding:
DOS Batch File  |  2004-03-20  |  15.5 KB  |  921 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl
  14. #line 15
  15.     eval 'exec perl -S $0 "$@"'
  16.     if 0;
  17. #                              -*- Mode: Perl -*- 
  18. # $Basename: perlindex.PL $
  19. # $Revision: 1.3 $
  20. # Author          : Ulrich Pfeifer
  21. # Created On      : Mon Jan 22 13:00:41 1996
  22. # Last Modified By: Ulrich Pfeifer
  23. # Last Modified On: Wed Jun 18 19:43:36 2003
  24. # Language        : Perl
  25. # Update Count    : 302
  26. # Status          : Unknown, Use with caution!
  27. # (C) Copyright 1997, Ulrich Pfeifer, all rights reserved.
  28. # %SEEN is used to store the absolute pathes to files which have been
  29. #       indexed. Probably this could be replaced by %FN.
  30. # %FN   $FN{'last'}    greatest documentid
  31. #       $FN{$did}      a pair of $mtf and $filename where $mtf is the
  32. #                      number of occurances of the most frequent word in
  33. #                      the document with number $did.
  34. # %IDF  $IDF{'*all*'}  number of documents (essentially the same as 
  35. #                      $FN{'last'})
  36. #       $IDF{$word}    number of documents containing $word
  37. # %IF   $IF{$word}     list of pairs ($docid,$tf) where $docid is
  38. #                      the number of a document containing $word $tf
  39. use Fcntl;
  40. use less 'time';
  41. use Getopt::Long;
  42. use File::Basename;
  43. use Text::English;
  44.  
  45. # NDBM_File as LAST resort
  46. package AnyDBM_File;
  47.  
  48. @ISA = qw(DB_File GDBM_File SDBM_File ODBM_File NDBM_File) unless @ISA;
  49.  
  50. for $mod (@ISA) {
  51.     last if eval "require $mod"
  52. }
  53.  
  54. package main;
  55.  
  56. $p          = 'w'; # compressed int patch available
  57. $nroff      = '';
  58. $man1direxp = 'D:\Perl\man\man1';
  59. $man3direxp = 'D:\Perl\man\man3';
  60. @perllib    = 'D:\Perl\lib';
  61. push @perllib,'D:\Perl\site\lib';
  62. $prefix     = 'D:\Perl';
  63. $pager      = 'more /e';
  64. use Term::ReadKey;
  65.  
  66. $stemmer = \&Text::English::stem;
  67. # directory for the index 
  68. $IDIR = $man1direxp;
  69. $IDIR =~ s:/[^/]*$::;
  70.  
  71. $opt_index   = '';                # make perl -w happy
  72. $opt_menu    = 1;
  73. $opt_maxhits = 15;
  74. $opt_cbreak  = 1;
  75. &GetOptions(
  76.             'index',
  77.             'cbreak!',
  78.             'maxhits=i',
  79.             'menu!',
  80.             'verbose',
  81.             'dict:i',
  82.             'idir=s',
  83.             ) || die "Usage: $0 [-index] [words ...]\n";
  84.  
  85. if (defined $opt_idir) {
  86.     $IDIR = $opt_idir;          # avoid to many changes below.
  87. }
  88.  
  89. if (defined $opt_dict) {
  90.     $opt_dict ||= 100;
  91. }
  92.  
  93. if ($opt_index) {
  94.     &initstop;
  95.  
  96.     tie (%IF,   AnyDBM_File, "$IDIR/index_if",   O_CREAT|O_RDWR, 0644)
  97.         or die "Could not tie $IDIR/index_if: $!\n";
  98.     tie (%IDF,  AnyDBM_File, "$IDIR/index_idf",  O_CREAT|O_RDWR, 0644)
  99.         or die "Could not tie $IDIR/index_idf: $!\n";
  100.     tie (%SEEN, AnyDBM_File, "$IDIR/index_seen", O_CREAT|O_RDWR, 0644)
  101.         or die "Could not tie $IDIR/index_seen: $!\n";
  102.     tie (%FN,   AnyDBM_File, "$IDIR/index_fn",   O_CREAT|O_RDWR, 0644)
  103.         or die "Could not tie $IDIR/index_fn: $!\n";
  104.  
  105.     require "find.pl";
  106.  
  107.     unless (@ARGV) {            # breaks compatibility :-(
  108.         for $dir (@perllib) {
  109.             print "Scanning $dir ... \n";
  110.             &find($dir);
  111.         }
  112.     }
  113.     for $name (@ARGV) {
  114.         my $fns = $name;
  115.         $fns =~ s:$prefix/::;
  116.         next if $SEEN{$fns};
  117.         next unless -f $name;
  118.         if ($name !~ /(~|,v)$/) {
  119.             $did = $FN{'last'}++;
  120.             $SEEN{$fns} = &index($name, $fns, $did); 
  121.         }
  122.     }
  123.     untie %IF;
  124.     untie %IDF;
  125.     untie %FN;
  126.     untie %SEEN;
  127. } elsif ($opt_dict) {
  128.     tie (%IDF,  AnyDBM_File, "$IDIR/index_idf",  O_RDONLY, 0644)
  129.         or die "Could not tie $IDIR/index_idf: $!\n".
  130.             "Did you run '$0 -index'?\n";
  131.     while (($key,$val) = each %IDF) {
  132.         printf "%-20s %d\n", $key, $val if $val >= $opt_dict;
  133.     }
  134.     untie %IDF;
  135. } else {
  136.     tie (%IF,   AnyDBM_File, "$IDIR/index_if",   O_RDONLY, 0644)
  137.         or die "Could not tie $IDIR/index_if: $!\n".
  138.             "Did you run '$0 -index'?\n";
  139.     tie (%IDF,  AnyDBM_File, "$IDIR/index_idf",   O_RDONLY, 0644)
  140.         or die "Could not tie $IDIR/index_idf: $!\n";
  141.     tie (%FN,   AnyDBM_File, "$IDIR/index_fn",   O_RDONLY, 0644)
  142.         or die "Could not tie $IDIR/index_fn: $!\n";
  143.     &search(@ARGV);
  144.     untie %IF;
  145.     untie %IDF;
  146.     untie %FN;
  147.     untie %SEEN;
  148. }
  149.  
  150. sub wanted {
  151.     my $fns = $name;
  152.  
  153.     if ($name eq $man3direxp) {
  154.         $prune = 1;
  155.     }
  156.     $fns =~ s:$prefix/::;
  157.     return if $SEEN{$fns};
  158.     return unless -f $_;
  159.     if ($name =~ /man|bin|\.(pod|pm)$/) {
  160.         if (!/(~|,v)$/) {
  161.             $did = $FN{'last'}++;
  162.             $SEEN{$fns} = &index($name, $fns, $did); 
  163.         }
  164.     }
  165. }
  166.  
  167. sub index {
  168.     my $fn  = shift;
  169.     my $fns = shift;
  170.     my $did = shift;
  171.     my %tf;
  172.     my $maxtf = 0;
  173.     my $pod = ($fns =~ /\.pod|man/);
  174.  
  175.     open(IN, "<$fn") || warn "Could not open $fn: $!\n", return (0);
  176.     while ($line = <IN>) {
  177.         if ($line =~ /^=head/) {
  178.             $pod = 1;
  179.         } elsif ($line =~ /^=cut/){
  180.             $pod = 0;
  181.         } else {
  182.             next unless $pod;
  183.         }
  184.         for $word (&normalize($line)) {
  185.             next if $stop{$word};
  186.             $tf{$word}++;
  187.         }
  188.     }
  189.     for $tf (values %tf) {
  190.         $maxtf = $tf if $tf > $maxtf;
  191.     }
  192.     for $word (keys %tf) {
  193.         $IDF{$word}++;
  194.         $IF{$word} = '' unless defined $IF{$word}; # perl -w
  195.         $IF{$word} .= pack($p.$p, $did, $tf{$word});
  196.     }
  197.     close(IN);
  198.     $FN{$did} = pack($p, $maxtf).$fns; 
  199.     print STDERR "$fns\n";
  200.     1;
  201. }
  202.  
  203. sub normalize {
  204.     my $line = join ' ', @_;
  205.     my @result;
  206.  
  207.     $line =~ tr/A-Z/a-z/;
  208.     $line =~ tr/a-z0-9/ /cs;
  209.     for $word (split ' ', $line ) {
  210.         $word =~ s/^\d+//;
  211.         next unless length($word) > 2;
  212.         if ($stemmer) {
  213.             push @result, &$stemmer($word);
  214.         } else {
  215.             push @result, $word;
  216.         }
  217.     }
  218.     @result;
  219. }
  220.  
  221. sub search {
  222.     my %score;
  223.     my $maxhits = $opt_maxhits;
  224.     my (@unknown, @stop);
  225.  
  226.     &initstop if $opt_verbose;
  227.     for $word (normalize(@_)) {
  228.         unless ($IF{$word}) {
  229.             if ($stop{$word}) {
  230.                 push @stop, $word;
  231.             } else {
  232.                 push @unknown, $word;
  233.             }
  234.             next;
  235.         }
  236.         my %post = unpack($p.'*',$IF{$word});
  237.         my $idf = log($FN{'last'}/$IDF{$word});
  238.         for $did (keys %post) {
  239.             my ($maxtf) = unpack($p, $FN{$did});
  240.             $score{$did} = 0 unless defined $score{$did}; # perl -w 
  241.             $score{$did} += $post{$did} / $maxtf * $idf;
  242.         }
  243.     }
  244.     if ($opt_verbose) {
  245.         print "Unkown:  @unknown\n" if @unknown;
  246.         print "Ingnore: @stop\n" if @stop;
  247.     }
  248.     if ($opt_menu) {
  249.         my @menu;
  250.         my $answer = '';
  251.         my $no = 0;
  252.         my @s = ('1' .. '9', 'a' .. 'z');
  253.         my %s;
  254.         
  255.         for $did (sort {$score{$b} <=> $score{$a}} keys %score) {
  256.             my ($mtf, $path) = unpack($p.'a*', $FN{$did});
  257.             my $s = $s[$no];
  258.             push @menu, sprintf "%s %6.3f %s\n", $s, $score{$did}, $path;
  259.             $s{$s} = ++$no;
  260.             last unless --$maxhits;
  261.         }
  262.         &cbreak('on') if $opt_cbreak;
  263.         while (1) {
  264.             print @menu;
  265.             print "\nEnter Number or 'q'> ";
  266.             if ($opt_cbreak) {
  267.                 read(TTYIN,$answer,1);
  268.                 print "\n";
  269.             } else {
  270.                 $answer = <STDIN>;
  271.             }
  272.             last if $answer =~ /^q/i;
  273.             $answer = ($s{substr($answer,0,1)})-1;
  274.             if ($answer >= 0 and $answer <= $#menu) {
  275.                 my $selection = $menu[$answer];
  276.                 if ($selection =~ m:/man:) {
  277.                     my ($page, $sect) = 
  278.                         ($selection =~ m:([^/]*)\.(.{1,3})$:);
  279.                     print STDERR "Running man $sect $page\n";
  280.                     system 'man', $sect, $page;
  281.                 } else {
  282.                     my ($path) = ($selection =~ m:(\S+)$:);
  283.                     $path = $prefix.'/'.$path;
  284.                     print STDERR "Running pod2man $path\n";
  285.                     system "pod2man --official $path | $nroff -man | $pager";
  286.                 }
  287.             } else {
  288.                 my $path = $prefix."/bin/perlindex";
  289.                 system "pod2man --official $path | $nroff -man | $pager";
  290.             }
  291.         }
  292.         &cbreak('off') if $opt_cbreak;
  293.     } else {
  294.         for $did (sort {$score{$b} <=> $score{$a}} keys %score) {
  295.             printf("%6.3f %s\n", $score{$did}, 
  296.                    (unpack($p.'a*', $FN{$did}))[1]);
  297.             last unless --$maxhits;
  298.         }
  299.     }
  300. }
  301.  
  302. sub cbreak {
  303.     my $mode = shift;
  304.     if ($mode eq 'on') {
  305.         open(TTYIN, "</dev/tty") || die "can't read /dev/tty: $!";
  306.         open(TTYOUT, ">/dev/tty") || die "can't write /dev/tty: $!";
  307.         select(TTYOUT);
  308.         $| = 1;
  309.         select(STDOUT);
  310.         $SIG{'QUIT'} = $SIG{'INT'} = 'cbreak';
  311.     ReadMode 3; # Set cbreak mode
  312.     } else {
  313.     ReadMode 0; # Restore non-cbreak mode
  314.     }
  315. }
  316.  
  317.  
  318. $stopinited = 0;                # perl -w
  319. sub initstop {
  320.     return if $stopinited++;
  321.     while (<DATA>) {
  322.         next if /^\#/;
  323.         for (normalize($_)) {
  324.           $stop{$_}++;
  325.         }
  326.     }
  327. }
  328.  
  329. =head1 NAME
  330.  
  331. perlindex - index and query perl manual pages
  332.  
  333. =head1 SYNOPSIS
  334.  
  335.     perlindex -index
  336.  
  337.     perlindex tell me where the flowers are
  338.  
  339. =head1 DESCRIPTION
  340.  
  341. "C<perlindex -index>" generates an AnyDBM_File index which can be
  342. searched with free text queries "C<perlindex> I<a verbose query>".
  343.  
  344. Each word of the query is searched in the index and a score is
  345. generated for each document containing it. Scores for all words are
  346. added and the documents with the highest score are printed.  All words
  347. are stemed with Porters algorithm (see L<Text::English>) before
  348. indexing and searching happens.
  349.  
  350. The score is computed as:
  351.  
  352.     $score{$document} += $tf{$word,$document}/$maxtf{$document}
  353.                          * log ($N/$n{$word});
  354.  
  355. where
  356.  
  357. =over 10
  358.  
  359. =item C<$N>
  360.  
  361. is the number of documents in the index,
  362.  
  363. =item C<$n{$word}>
  364.  
  365. is the number of documents containing the I<word>,
  366.  
  367. =item C<$tf{$word,$document}>
  368.  
  369. is the number of occurances of I<word> in the I<document>, and
  370.  
  371. =item C<$maxtf{$document}>
  372.  
  373. is the maximum freqency of any word in I<document>.
  374.  
  375. =back
  376.  
  377. =head1 OPTIONS
  378.  
  379. All options may be abreviated.
  380.  
  381. =over 10
  382.  
  383. =item B<-maxhits> maxhits
  384.  
  385. Maximum numer of hits to display. Default is 15.
  386.  
  387. =item B<-menu>
  388.  
  389. =item B<-nomenu>
  390.  
  391. Use the matches as menu for calling C<man>. Default is B<-menu>.q
  392.  
  393. =item B<-cbreak>
  394.  
  395. =item B<-nocbreak>
  396.  
  397. Switch to cbreak in menu mode or dont. B<-cbreak> is the default.
  398.  
  399. =item B<-verbose>
  400.  
  401. Generates additional information which query words have been not found
  402. in the database and which words of the query are stopwords.
  403.  
  404. =back
  405.  
  406. =head1 EXAMPLE
  407.  
  408.     perlindex foo bar
  409.  
  410.     1  3.735 lib/pod/perlbot.pod
  411.     2  2.640 lib/pod/perlsec.pod
  412.     3  2.153 lib/pod/perldata.pod
  413.     4  1.920 lib/Symbol.pm
  414.     5  1.802 lib/pod/perlsub.pod
  415.     6  1.586 lib/Getopt/Long.pm
  416.     7  1.190 lib/File/Path.pm
  417.     8  1.042 lib/pod/perlop.pod
  418.     9  0.857 lib/pod/perlre.pod
  419.     a  0.830 lib/Shell.pm
  420.     b  0.691 lib/strict.pm
  421.     c  0.691 lib/Carp.pm
  422.     d  0.680 lib/pod/perlpod.pod
  423.     e  0.680 lib/File/Find.pm
  424.     f  0.626 lib/pod/perlsyn.pod
  425.     Enter Number or 'q'>
  426.  
  427. Hitting the keys C<1> to C<f> will display the corresponding manual
  428. page. Hitting C<q> quits. All other keys display this manual page.
  429.  
  430. =head1 FILES
  431.  
  432. The index will be generated in your man directory. Strictly speaking in 
  433. C<$Config{man1direxp}/..>
  434.  
  435.     The following files will be generated:
  436.  
  437.     index_fn           # docid -> (max frequency, filename)
  438.     index_idf          # term  -> number of documents containing term
  439.     index_if           # term  -> (docid, frequency)*
  440.     index_seen         # fn    -> indexed?
  441.     
  442.  
  443. =head1 AUTHOR
  444.  
  445. Ulrich Pfeifer E<lt>F<pfeifer@ls6.informatik.uni-dortmund.de>E<gt>
  446.  
  447. =cut
  448.  
  449. __END__
  450. # freeWAIS-sf stopwords
  451. a
  452. about
  453. above
  454. according
  455. across
  456. actually
  457. adj
  458. after
  459. afterwards
  460. again
  461. against
  462. all
  463. almost
  464. alone
  465. along
  466. already
  467. also
  468. although
  469. always
  470. among
  471. amongst
  472. an
  473. and
  474. another
  475. any
  476. anyhow
  477. anyone
  478. anything
  479. anywhere
  480. are
  481. aren't
  482. around
  483. as
  484. at
  485. b
  486. be
  487. became
  488. because
  489. become
  490. becomes
  491. becoming
  492. been
  493. before
  494. beforehand
  495. begin
  496. beginning
  497. behind
  498. being
  499. below
  500. beside
  501. besides
  502. between
  503. beyond
  504. billion
  505. both
  506. but
  507. by
  508. c
  509. can
  510. can't
  511. cannot
  512. caption
  513. co
  514. co.
  515. could
  516. couldn't
  517. d
  518. did
  519. didn't
  520. do
  521. does
  522. doesn't
  523. don't
  524. down
  525. during
  526. e
  527. each
  528. eg
  529. eight
  530. eighty
  531. either
  532. else
  533. elsewhere
  534. end
  535. ending
  536. enough
  537. etc
  538. even
  539. ever
  540. every
  541. everyone
  542. everything
  543. everywhere
  544. except
  545. f
  546. few
  547. fifty
  548. first
  549. five
  550. vfor
  551. former
  552. formerly
  553. forty
  554. found "
  555. four
  556. from
  557. further
  558. g
  559. h
  560. had
  561. has
  562. hasn't
  563. have
  564. haven't
  565. he
  566. he'd
  567. he'll
  568. he's
  569. hence
  570. her
  571. here
  572. here's
  573. hereafter
  574. hereby
  575. herein
  576. hereupon
  577. hers
  578. herself
  579. him
  580. himself
  581. his
  582. how
  583. however
  584. hundred
  585. i
  586. i'd
  587. i'll
  588. i'm
  589. i've
  590. ie
  591. if
  592. in
  593. inc.
  594. indeed
  595. instead
  596. into
  597. is
  598. isn't
  599. it
  600. it's
  601. its
  602. itself
  603. j
  604. k
  605. l
  606. last
  607. later
  608. latter
  609. latterly
  610. least
  611. less
  612. let
  613. let's
  614. like
  615. likely
  616. ltd
  617. m
  618. made
  619. make
  620. makes
  621. many
  622. maybe
  623. me
  624. meantime
  625. meanwhile
  626. might
  627. million
  628. miss
  629. more
  630. moreover
  631. most
  632. mostly
  633. mr
  634. mrs
  635. much
  636. must
  637. my
  638. myself
  639. n
  640. namely
  641. neither
  642. never
  643. nevertheless
  644. next
  645. nine
  646. ninety
  647. no
  648. nobody
  649. none
  650. nonetheless
  651. noone
  652. nor
  653. not
  654. nothing
  655. now
  656. nowhere
  657. o
  658. of
  659. off
  660. often
  661. on
  662. once
  663. one
  664. one's
  665. only
  666. onto
  667. or
  668. other
  669. others
  670. otherwise
  671. our
  672. ours
  673. ourselves
  674. out
  675. over
  676. overall
  677. own
  678. p
  679. per
  680. perhaps
  681. q
  682. r
  683. rather
  684. recent
  685. recently
  686. s
  687. same
  688. seem
  689. seemed
  690. seeming
  691. seems
  692. seven
  693. seventy
  694. several
  695. she
  696. she'd
  697. she'll
  698. she's
  699. should
  700. shouldn't
  701. since
  702. six
  703. sixty
  704. so
  705. some
  706. somehow
  707. someone
  708. something
  709. sometime
  710. sometimes
  711. somewhere
  712. still
  713. stop
  714. such
  715. t
  716. taking
  717. ten
  718. than
  719. that
  720. that'll
  721. that's
  722. that've
  723. the
  724. their
  725. them
  726. themselves
  727. then
  728. thence
  729. there
  730. there'd
  731. there'll
  732. there're
  733. there's
  734. there've
  735. thereafter
  736. thereby
  737. therefore
  738. therein
  739. thereupon
  740. these
  741. they
  742. they'd
  743. they'll
  744. they're
  745. they've
  746. thirty
  747. this
  748. those
  749. though
  750. thousand
  751. three
  752. through
  753. throughout
  754. thru
  755. thus
  756. to
  757. together
  758. too
  759. toward
  760. towards
  761. trillion
  762. twenty
  763. two
  764. u
  765. under
  766. unless
  767. unlike
  768. unlikely
  769. until
  770. up
  771. upon
  772. us
  773. used
  774. using
  775. v
  776. very
  777. via
  778. w
  779. was
  780. wasn't
  781. we
  782. we'd
  783. we'll
  784. we're
  785. we've
  786. well
  787. were
  788. weren't
  789. what
  790. what'll
  791. what's
  792. what've
  793. whatever
  794. when
  795. whence
  796. whenever
  797. where
  798. where's
  799. whereafter
  800. whereas
  801. whereby
  802. wherein
  803. whereupon
  804. wherever
  805. whether
  806. which
  807. while
  808. whither
  809. who
  810. who'd
  811. who'll
  812. who's
  813. whoever
  814. whole
  815. whom
  816. whomever
  817. whose
  818. why
  819. will
  820. with
  821. within
  822. without
  823. won't
  824. would
  825. wouldn't
  826. x
  827. y
  828. yes
  829. yet
  830. you
  831. you'd
  832. you'll
  833. you're
  834. you've
  835. your
  836. yours
  837. yourself
  838. yourselves
  839. z
  840. # occuring in more than 100 files
  841. acc
  842. accent
  843. accents
  844. and
  845. are
  846. bell
  847. can
  848. character
  849. corrections
  850. crt
  851. daisy
  852. dash
  853. date
  854. defined
  855. definitions
  856. description
  857. devices
  858. diablo
  859. dummy
  860. factors
  861. following
  862. font
  863. for
  864. from
  865. fudge
  866. give
  867. have
  868. header
  869. holds
  870. log
  871. logo
  872. low
  873. lpr
  874. mark
  875. name
  876. nroff
  877. out
  878. output
  879. perl
  880. pitch
  881. put
  882. rcsfile
  883. reference
  884. resolution
  885. revision
  886. see
  887. set
  888. simple
  889. smi
  890. some
  891. string
  892. synopsis
  893. system
  894. that
  895. the
  896. this
  897. translation
  898. troff
  899. typewriter
  900. ucb
  901. unbreakable
  902. use
  903. used
  904. user
  905. vroff
  906. wheel
  907. will
  908. with
  909. you
  910.  
  911.  
  912. __END__
  913. :endofperl
  914.