1 |
#!/usr/bin/perl -w |
2 |
# -*- Mode: Perl -*- |
3 |
# $Basename$ |
4 |
# $Revision: 1.10 $ |
5 |
# Author : Ulrich Pfeifer |
6 |
# Created On : Mon Dec 31 13:57:11 2001 |
7 |
# Last Modified By: Ulrich Pfeifer |
8 |
# Last Modified On: Fri Jan 4 15:59:20 2002 |
9 |
# Language : CPerl |
10 |
# |
11 |
# (C) Copyright 2001, Ulrich Pfeifer |
12 |
# |
13 |
|
14 |
use 5.007; |
15 |
|
16 |
use strict; |
17 |
use Devel::Peek qw(Dump); |
18 |
|
19 |
use File::Path; |
20 |
use DB_File; |
21 |
use Getopt::Long; |
22 |
use Cwd; |
23 |
|
24 |
BEGIN {require WAIT::Config;} |
25 |
use WAIT::Database; |
26 |
use WAIT::Parse::Ora; |
27 |
use WAIT::Document::Ora; |
28 |
use WAIT::InvertedIndex; |
29 |
|
30 |
|
31 |
$DB_BTREE->{'cachesize'} = 200_000 ; |
32 |
|
33 |
my %OPT = ( |
34 |
database => 'oreilly_de_catalog', |
35 |
dir => '/usr/local/apache/data', |
36 |
table => 'ora', |
37 |
); |
38 |
|
39 |
GetOptions(\%OPT, |
40 |
'database=s', |
41 |
'dir=s', |
42 |
'table=s', |
43 |
) || die "Usage: ...\n"; |
44 |
|
45 |
my @localtime = localtime; |
46 |
$localtime[5] += 1900; |
47 |
$localtime[4]++; |
48 |
my $jobid = sprintf "%04s-%02s-%02s_%02s:%02s_%d", @localtime[5,4,3,2,1], $$; |
49 |
my $db = WAIT::Database->create(name => "$OPT{database}-$jobid", |
50 |
directory => $OPT{dir}) |
51 |
or die "Could not create database $OPT{database}: $@\n"; |
52 |
|
53 |
my $layout = new WAIT::Parse::Ora; |
54 |
|
55 |
use lib "/usr/local/apache/lib"; |
56 |
use oreilly_de_catalog::wait_handler; |
57 |
|
58 |
my $stem = ['OR_tr_20020124', 'OR_lc_20020124', 'split2', 'stop', 'Stem']; |
59 |
my $text = [{ |
60 |
'prefix' => ['OR_tr_20020124', 'OR_lc_20020124'], |
61 |
'intervall' => ['OR_tr_20020124', 'OR_lc_20020124'], |
62 |
}, |
63 |
'OR_tr_20020124', 'OR_lc_20020124', 'split2', 'stop']; |
64 |
my $wplus = ['split2', 'OR_lc_20020124', 'OR_mixedonly_20020221']; |
65 |
my $sound = ['OR_tr_20020124', 'OR_lc_20020124', 'split2', 'Soundex']; |
66 |
my $trigr = ['OR_lc_20020124', 'OR_trigrams_20020125']; |
67 |
# split6 is better than split13 or split10: it allows them to enter |
68 |
# shorter sequences when searching. |
69 |
my $isbn = ['split6', 'OR_isbn_20020127']; |
70 |
|
71 |
my $cwd = cwd; |
72 |
|
73 |
my $droot = shift or die "Usage: $0 <options> Document-Rootdirectories"; |
74 |
|
75 |
my %D; |
76 |
my $access = tie %D, 'WAIT::Document::Ora', $droot, |
77 |
or die "Couldn't tie to dir $droot: $!\n"; |
78 |
|
79 |
my $tb = $db->create_table(name => $OPT{table}, |
80 |
attr => ['author', 'isbn', 'title', |
81 |
'headline', 'docid'], |
82 |
layout => $layout, |
83 |
access => $access, |
84 |
invindex => |
85 |
[ |
86 |
'title' => $text, |
87 |
'title' => $wplus, |
88 |
# 'title' => $stem, |
89 |
'aboutauthor' => $text, |
90 |
'aboutauthor' => $wplus, |
91 |
# 'aboutauthor' => $stem, |
92 |
'desc' => $text, |
93 |
'desc' => $wplus, |
94 |
'abstract' => $text, |
95 |
'abstract' => $wplus, |
96 |
'author' => $text, |
97 |
# 'author' => $sound, |
98 |
'colophon' => $text, |
99 |
'colophon' => $wplus, |
100 |
'isbn' => $isbn, |
101 |
] |
102 |
); |
103 |
die "Couldn't create table $OPT{table}: $@\n" unless $tb; |
104 |
|
105 |
my ($did, $value); |
106 |
binmode STDOUT, ":utf8"; |
107 |
while (($did, $value) = each %D) { |
108 |
my $record = $layout->split($value); |
109 |
my $headline = $record->{title}; |
110 |
$headline =~ s/\s+/ /sg; |
111 |
printf "%15s %s\n", $record->{isbn}, substr($headline,0,60); |
112 |
$tb->insert('docid' => $did, |
113 |
headline => $headline, |
114 |
%{$record}); |
115 |
} |
116 |
$tb->set(top=>1); |
117 |
|
118 |
my $tritb = $db->create_table( |
119 |
name => "$OPT{table}_fallback", |
120 |
attr => [qw(docid headline)], # name |
121 |
# "headline" |
122 |
# only for |
123 |
# sman |
124 |
invindex => [ headline => $trigr ], |
125 |
); |
126 |
my %dict; |
127 |
for my $f ($tb->fields) { |
128 |
my(@idx) = @{$tb->table->{inverted}{$f} || []}; |
129 |
for my $idx (@idx) { |
130 |
my $name = $idx->name; |
131 |
next if $name =~ /(_|\b)(mixedonly|Stem|Soundex)(\b|_)/; |
132 |
# irrelevant for alternatives |
133 |
my @keys = $idx->keys; |
134 |
@dict{@keys} = (); |
135 |
} |
136 |
} |
137 |
my @dictkeys = grep s/^p//, keys %dict; |
138 |
my $maxdebug = 5; |
139 |
for my $headline (@dictkeys) { |
140 |
if ($maxdebug && $headline =~ /[^\040-\177]/) { |
141 |
Dump $headline; |
142 |
$maxdebug--; |
143 |
} |
144 |
# printf "%s\n", substr($headline,0,60); |
145 |
$tritb->insert(docid => $headline, headline => $headline); |
146 |
} |
147 |
$tritb->set(top=>1); |
148 |
$tritb->close; |
149 |
$tb->close(); |
150 |
$db->close(); |
151 |
|
152 |
# Atomically relinking symlink: now we have a new database with a very |
153 |
# long name like oreilly_de_catalog-2002-01-28_16:12_16467 and we want |
154 |
# that database to be accessible with the oreilly_de_catalog name. |
155 |
|
156 |
use File::Spec; |
157 |
my $dir = "$OPT{database}-$jobid"; |
158 |
my $slwant = File::Spec->catdir($OPT{dir}, $OPT{database}); |
159 |
my $sltmp = File::Spec->catdir($OPT{dir}, "$OPT{database}-$$"); |
160 |
unlink $sltmp; # may fail |
161 |
symlink $dir, $sltmp or die "Couldn't symlink $dir, $sltmp: $!"; |
162 |
rename $sltmp, $slwant or die "Couldn't rename $sltmp, $slwant: $!"; |
163 |
warn "$slwant now points to $dir"; |
164 |
system("chmod 777 $slwant/*/read")==0 or die; |
165 |
|
166 |
$WAIT::Config = $WAIT::Config; # make perl -w happy |
167 |
|
168 |
|
169 |
__END__ |
170 |
## ################################################################### |
171 |
## pod |
172 |
## ################################################################### |
173 |
|
174 |
=head1 NAME |
175 |
|
176 |
index_ora - generate an WAIT index for O'Reilly catalog |
177 |
|
178 |
=head1 SYNOPSIS |
179 |
|
180 |
B<index_ora> |
181 |
[B<-database> I<dbname>] |
182 |
[B<-dir> I<directory>] |
183 |
[B<-table> I<table name>] |
184 |
I<directory> |
185 |
|
186 |
=head1 DESCRIPTION |
187 |
|
188 |
=head1 OPTIONS |
189 |
|
190 |
=over 5 |
191 |
|
192 |
=item B<-database> I<dbname> |
193 |
|
194 |
Specify database name. Default is F<DB>. |
195 |
|
196 |
=item B<-dir> I<directory> |
197 |
|
198 |
Alternate directory where databases are located. Default is the |
199 |
directory specified during configuration of WAIT. |
200 |
|
201 |
=item B<-table> I<table name> |
202 |
|
203 |
Specify an alternate table name. Default is C<ora>. |
204 |
|
205 |
=head1 AUTHOR |
206 |
|
207 |
Ulrich Pfeifer E<lt>F<pfeifer@wait.de>E<gt> |
208 |
|