1 |
#!/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 |
($title, $year) = ($2,$3); |
50 |
$all_titles->{$title}++; |
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 |
|
76 |
$doc->add_attr('@title', $title); |
77 |
$doc->add_hidden_text( |
78 |
($title x $title_rank) |
79 |
); |
80 |
|
81 |
$doc->add_attr('@size', length($trivia)); |
82 |
|
83 |
$doc->add_attr('year', $year) if ($year); |
84 |
foreach my $q (@qv) { |
85 |
$doc->add_attr('quote', $q); |
86 |
$doc->add_hidden_text($q); |
87 |
} |
88 |
|
89 |
# add the body text to the document object |
90 |
$doc->add_text($trivia); |
91 |
|
92 |
|
93 |
# register the document object to the database |
94 |
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
95 |
|
96 |
$trivia = ''; |
97 |
@qv = (); |
98 |
$in = 0; |
99 |
} else { |
100 |
print "#$_\n"; |
101 |
} |
102 |
|
103 |
# last if ($i > 1000); # XXX remove this! |
104 |
} |
105 |
|
106 |
sub dump_data($$) { |
107 |
my ($name,$hash) = @_; |
108 |
|
109 |
open(my $fh, "> $name") || die "can't open $name: $!"; |
110 |
|
111 |
foreach my $k (sort keys %{$hash}) { |
112 |
print $fh "$k\t",$hash->{$k},"\n"; |
113 |
} |
114 |
|
115 |
close($fh); |
116 |
} |
117 |
|
118 |
dump_data('titles.data', $all_titles); |
119 |
dump_data('quotes.data', $all_quotes); |
120 |
dump_data('years.data', $all_years); |
121 |
|
122 |
#print "\noptimizing...\n"; |
123 |
#$db->optimize(0); |