source: trunk/gsdl/perllib/lucenebuilder.pm@ 9197

Last change on this file since 9197 was 9197, checked in by kjdon, 19 years ago

a few changes to make it work on windows

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1###########################################################################
2#
3# lucenebuilder.pm -- perl wrapper for building index with Lucene
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package lucenebuilder;
27
28# Use same basic XML structure setup by mgppbuilder/mgppbuildproc
29
30use mgppbuilder;
31
32sub BEGIN {
33 @lucenebuilder::ISA = ('mgppbuilder');
34}
35
36
37sub new {
38 my $class = shift(@_);
39 my ($collection, $source_dir, $build_dir, $verbosity,
40 $maxdocs, $debug, $keepold, $allclassifications,
41 $outhandle, $no_text, $gli) = @_;
42
43 my $self = new mgppbuilder (@_);
44 $self = bless $self, $class;
45
46 # load up the document processor for building
47 # if a buildproc class has been created for this collection, use it
48 # otherwise, use the lucene buildproc
49 my ($buildprocdir, $buildproctype);
50 if (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}buildproc.pm") {
51 $buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
52 $buildproctype = "${collection}buildproc";
53 } else {
54 $buildprocdir = "$ENV{'GSDLHOME'}/perllib";
55 $buildproctype = "lucenebuildproc";
56 }
57 require "$buildprocdir/$buildproctype.pm";
58
59 eval("\$self->{'buildproc'} = new $buildproctype(\$collection, " .
60 "\$source_dir, \$build_dir, \$verbosity, \$outhandle)");
61 die "$@" if $@;
62
63 $self->{'buildtype'} = "lucene";
64
65 return $self;
66}
67
68# this writes a nice version of the text docs
69sub compress_text {
70
71 my $self = shift (@_);
72 my ($textindex) = @_;
73 my $outhandle = $self->{'outhandle'};
74 print STDERR "Saving the document text\n";
75 # the text directory
76 my $text_dir = &util::filename_cat($self->{'build_dir'}, "text");
77 my $build_dir = &util::filename_cat($self->{'build_dir'},"");
78 &util::mk_all_dir ($text_dir);
79
80 my $osextra = "";
81 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
82 $text_dir =~ s@/@\\@g;
83 } else {
84 if ($outhandle ne "STDERR") {
85 # so lucene_passes doesn't print to stderr if we redirect output
86 $osextra .= " 2>/dev/null";
87 }
88 }
89
90
91 # get any os specific stuff
92 my $scriptdir = "$ENV{'GSDLHOME'}/bin/script";
93
94 my $lucene_passes_exe = &util::filename_cat($scriptdir, "lucene_passes.pl");
95 my $full_lucene_passes_exe = $lucene_passes_exe;
96 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
97 $full_lucene_passes_exe = "perl.exe -S $lucene_passes_exe";
98 }
99 my $lucene_passes_sections = "Doc";
100
101 my ($handle);
102
103 if ($self->{'debug'}) {
104 $handle = STDOUT;
105 } else {
106 if (!-e "$lucene_passes_exe" ||
107 !open (PIPEOUT, "| $full_lucene_passes_exe text $lucene_passes_sections \"$build_dir\" \"dummy\" $osextra")) {
108 print STDERR "<FatalError name='NoRunLucenePasses'/>\n</Stage>\n" if $self->{'gli'};
109 die "lucenebuilder::build_index - couldn't run $lucene_passes_exe\n";
110 }
111 $handle = lucenebuilder::PIPEOUT;
112 }
113 my $levels = $self->{'levels'};
114 undef $levels->{'paragraph'}; # get rid of para if we had it.
115 # set up the document processr
116 $self->{'buildproc'}->set_output_handle ($handle);
117 $self->{'buildproc'}->set_mode ('text');
118 $self->{'buildproc'}->set_index ($textindex);
119 $self->{'buildproc'}->set_indexing_text (0);
120 $self->{'buildproc'}->set_store_text(1);
121 $self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
122 $self->{'buildproc'}->set_levels ($levels);
123 $self->{'buildproc'}->reset();
124 &plugin::begin($self->{'pluginfo'}, $self->{'source_dir'},
125 $self->{'buildproc'}, $self->{'maxdocs'});
126 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
127 "", {}, $self->{'buildproc'}, $self->{'maxdocs'});
128 &plugin::end($self->{'pluginfo'});
129 close ($handle) unless $self->{'debug'};
130 close PIPEOUT;
131 $self->print_stats();
132
133 print STDERR "</Stage>\n" if $self->{'gli'};
134
135}
136
137sub build_indexes {
138 my $self = shift (@_);
139 my ($indexname) = @_;
140 my $outhandle = $self->{'outhandle'};
141
142 my $indexes = [];
143 if (defined $indexname && $indexname =~ /\w/) {
144 push @$indexes, $indexname;
145 } else {
146 $indexes = $self->{'collect_cfg'}->{'indexes'};
147 }
148
149 # create the mapping between the index descriptions
150 # and their directory names (includes subcolls and langs)
151 $self->{'index_mapping'} = $self->create_index_mapping ($indexes);
152
153 # build each of the indexes
154 foreach $index (@$indexes) {
155 if ($self->want_built($index)) {
156
157 my $idx = $self->{'index_mapping'}->{$index};
158 foreach my $level (keys %{$self->{'levels'}}) {
159 next if $level =~ /paragraph/; # we don't do para indexing
160 my ($pindex) = $level =~ /^(.)/;
161 # should probably check that new name with level
162 # is unique ... but currently (with doc sec and para)
163 # each has unique first letter.
164 $self->{'index_mapping'}->{$index} = $pindex.$idx;
165
166 my $llevel = $mgppbuilder::level_map{$level};
167 print $outhandle "\n*** building index $index at level $llevel in subdirectory " .
168 "$self->{'index_mapping'}->{$index}\n" if ($self->{'verbosity'} >= 1);
169 print STDERR "<Stage name='Index' source='$index' level=$llevel>\n" if $self->{'gli'};
170
171 $self->build_index($index,$llevel);
172 }
173 $self->{'index_mapping'}->{$index} = $idx;
174
175 } else {
176 print $outhandle "\n*** ignoring index $index\n" if ($self->{'verbosity'} >= 1);
177 }
178 }
179
180 #define the final field lists
181 $self->make_final_field_list();
182}
183
184
185
186
187
188sub build_index {
189 my $self = shift (@_);
190 my ($index,$llevel) = @_;
191 my $outhandle = $self->{'outhandle'};
192 my $build_dir = $self->{'build_dir'};
193
194 # get the full index directory path and make sure it exists
195 my $indexdir = $self->{'index_mapping'}->{$index};
196 &util::mk_all_dir (&util::filename_cat($build_dir, $indexdir));
197
198 # get any os specific stuff
199 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
200 my $scriptdir = "$ENV{'GSDLHOME'}/bin/script";
201
202 my $exe = &util::get_os_exe ();
203 my $lucene_passes_exe = &util::filename_cat($scriptdir, "lucene_passes.pl");
204 my $full_lucene_passes_exe = $lucene_passes_exe;
205 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
206 $full_lucene_passes_exe = "perl.exe -S $lucene_passes_exe";
207 }
208
209 # define the section names for lucenepasses
210 # define the section names and possibly the doc name for lucenepasses
211 my $lucene_passes_sections = $llevel;
212
213 my $osextra = "";
214 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
215 $build_dir =~ s@/@\\@g;
216 } else {
217 $osextra = " -d /";
218 if ($outhandle ne "STDERR") {
219 # so lucene_passes doesn't print to stderr if we redirect output
220 $osextra .= " 2>/dev/null";
221 }
222 }
223
224 # get the index expression if this index belongs
225 # to a subcollection
226 my $indexexparr = [];
227
228 # there may be subcollection info, and language info.
229 my ($fields, $subcollection, $language) = split (":", $index);
230 my @subcollections = ();
231 @subcollections = split /,/, $subcollection if (defined $subcollection);
232
233 foreach $subcollection (@subcollections) {
234 if (defined ($self->{'collect_cfg'}->{'subcollection'}->{$subcollection})) {
235 push (@$indexexparr, $self->{'collect_cfg'}->{'subcollection'}->{$subcollection});
236 }
237 }
238
239 # add expressions for languages if this index belongs to
240 # a language subcollection - only put languages expressions for the
241 # ones we want in the index
242
243 # this puts a separate Language/en entry in for each language in the list
244 # is this what we want?
245 # should we just have one entry with Language/en,es/ ??
246 my @languages = ();
247 @languages = split /,/, $language if (defined $language);
248 foreach $language (@languages) {
249 my $not=0;
250 if ($language =~ s/^\!//) {
251 $not = 1;
252 }
253 if ($not) {
254 push (@$indexexparr, "!Language/$language/");
255 } else {
256 push (@$indexexparr, "Language/$language/");
257 }
258 }
259
260 # Build index dictionary. Uses verbatim stem method
261 print $outhandle "\n creating index dictionary (lucene_passes -I1)\n" if ($self->{'verbosity'} >= 1);
262 print STDERR "<Phase name='CreatingIndexDic'/>\n" if $self->{'gli'};
263 my ($handle);
264
265 if ($self->{'debug'}) {
266 $handle = STDOUT;
267 } else {
268 if (!-e "$lucene_passes_exe" ||
269 !open (PIPEOUT, "| $full_lucene_passes_exe index $lucene_passes_sections \"$build_dir\" \"$indexdir\" $osextra")) {
270 print STDERR "<FatalError name='NoRunLucenePasses'/>\n</Stage>\n" if $self->{'gli'};
271 die "lucenebuilder::build_index - couldn't run $lucene_passes_exe\n";
272 }
273 $handle = lucenebuilder::PIPEOUT;
274 }
275
276 my $store_levels = $self->{'levels'};
277
278 my $dom_level = "";
279 foreach my $key (keys %$store_levels) {
280 if ($mgppbuilder::level_map{$key} eq $llevel) {
281 $dom_level = $key;
282 }
283 }
284 if ($dom_level eq "") {
285 print STDERR "Warning: unrecognized tag level $llevel\n";
286 $dom_level = "document";
287 }
288
289 my $local_levels = { $dom_level => 1 }; # work on one level at a time
290
291 # set up the document processr
292 $self->{'buildproc'}->set_output_handle ($handle);
293 $self->{'buildproc'}->set_mode ('text');
294 $self->{'buildproc'}->set_index ($index, $indexexparr);
295 $self->{'buildproc'}->set_indexing_text (1);
296 $self->{'buildproc'}->set_store_text(1);
297 $self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
298 $self->{'buildproc'}->set_levels ($local_levels);
299 $self->{'buildproc'}->reset();
300 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
301 "", {}, $self->{'buildproc'}, $self->{'maxdocs'});
302 close ($handle) unless $self->{'debug'};
303
304 $self->print_stats();
305
306 $self->{'buildproc'}->set_levels ($store_levels);
307 print STDERR "</Stage>\n" if $self->{'gli'};
308}
309
3101;
311
312
Note: See TracBrowser for help on using the repository browser.