/[pgestraier]/trunk/data/indexer.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/data/indexer.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 29 - (hide annotations)
Wed Jul 6 11:47:56 2005 UTC (18 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 2334 byte(s)
added indexer.pl written using perl Hyper Estraier bindings

1 dpavlin 29 #!/usr/bin/perl -w
2    
3     use strict;
4     use HyperEstraier;
5    
6     # score for words in title
7     my $title_rank = 3;
8    
9     open(my $t, "gzip -cd trivia.list.gz |") || die "can't open trivia.list.gz: $!";
10    
11     # open the database
12     my $db = HyperEstraier::Database->new();
13     $db->open('casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
14    
15    
16     my $header = 1;
17     my ($title, $year) = ('',undef);
18     my $in = 0;
19     my $trivia = '';
20     my @qv;
21    
22     my ($all_years,$all_titles,$all_quotes);
23    
24     sub qv {
25     my $t = shift || return;
26    
27     sub qv_print {
28     my $v = shift || return '';
29     # $v =~ s/(.*)\s*,\s*(.+)/$2 $1/g;
30     push @qv, $v;
31     $all_quotes->{$v}++;
32     return '';
33     }
34     $t =~ s#([_'])([^_']+?)\1 \(qv\)#qv_print($2)#ge;
35     }
36    
37     my $i = 0;
38    
39     while(<$t>) {
40     if ($header && /^=====+/) {
41     $header = 0;
42     next;
43     }
44     next if $header;
45    
46     if (/^#\s+(.*)\s*$/) {
47     $title = $1;
48     if ($title =~ m#^("*)(.*)\1\s*\((\d+)\)(:?\s*\(\w+\))*$#) {
49     $year = $3;
50     $all_titles->{$2}++;
51     $all_years->{$3}++;
52     } else {
53     $year = undef;
54     }
55    
56     print "# $title ", ( $year ? "[$year]" : "" ), "\n";
57     next;
58    
59     } elsif (/^-\s(.*)\s*$/) {
60     $in = 1;
61     $trivia = "$1\n";
62     qv($1);
63     } elsif (/^\s\s(.*)\s*$/) {
64     $trivia .= "$1\n";
65     qv($1);
66     } elsif (/^$/ && $in) {
67     $i++;
68     print "$i ";
69    
70     # create a document object
71     my $doc = HyperEstraier::Document->new;
72    
73     # add attributes to the document object
74     $doc->add_attr('@uri', "file://localhost/trivia/$i");
75     $doc->add_attr('@title', $title);
76     $doc->add_attr('@size', length($trivia));
77    
78     $doc->add_attr('year', $year) if ($year);
79     foreach my $q (@qv) {
80     $doc->add_attr('quote', $q);
81     $doc->add_hidden_text($q);
82     }
83    
84     # add the body text to the document object
85     $doc->add_text($trivia);
86     $doc->add_hidden_text(
87     ($title x $title_rank)
88     );
89    
90    
91     # register the document object to the database
92     $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
93    
94     $trivia = '';
95     @qv = ();
96     $in = 0;
97     } else {
98     print "#$_\n";
99     }
100    
101     # last if ($i > 1000); # XXX remove this!
102     }
103    
104     sub dump_data($$) {
105     my ($name,$hash) = @_;
106    
107     open(my $fh, "> $name") || die "can't open $name: $!";
108    
109     foreach my $k (sort keys %{$hash}) {
110     print $fh "$k\t",$hash->{$k},"\n";
111     }
112    
113     close($fh);
114     }
115    
116     dump_data('titles.data', $all_titles);
117     dump_data('quotes.data', $all_quotes);
118     dump_data('years.data', $all_years);
119    
120     #print "\noptimizing...\n";
121     #$db->optimize(0);

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26