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