source: trunk/gsdl/perllib/lucenebuildproc.pm@ 9178

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

tried to tidy up the paragraph tagging stuff, but its still not working - needs gs2:id att in the para tag to work properly with the indexing as is. needs to be thought about more

  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1###########################################################################
2#
3# lucenebuildproc.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 lucenebuildproc;
27
28# This document processor outputs a document
29# for lucene to process
30
31# Use same basic XML structure setup by mgppbuilder/mgppbuildproc
32
33use mgppbuildproc;
34
35
36sub BEGIN {
37 @lucenebuildproc::ISA = ('mgppbuildproc');
38}
39
40
41sub new {
42 my $class = shift @_;
43 my $self = new mgppbuildproc (@_);
44
45 return bless $self, $class;
46}
47
48sub text {
49 my $self = shift (@_);
50 my ($doc_obj,$file) = @_;
51 my $handle = $self->{'output_handle'};
52 my $outhandle = $self->{'outhandle'};
53 my $indexed_doc = 1;
54
55 # only output this document if it is one to be indexed
56 return if ($doc_obj->get_doc_type() ne "indexed_doc");
57
58 # see if this document belongs to this subcollection
59 foreach my $indexexp (@{$self->{'indexexparr'}}) {
60 $indexed_doc = 0;
61 my ($field, $exp, $options) = split /\//, $indexexp;
62 if (defined ($field) && defined ($exp)) {
63 my ($bool) = $field =~ /^(.)/;
64 $field =~ s/^.// if $bool eq '!';
65 if ($field =~ /^filename$/i) {
66 $field = $doc_obj->get_source_filename();
67 } else {
68 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
69 }
70 next unless defined $field;
71 if ($bool eq '!') {
72 if ($options =~ /^i$/i) {
73 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
74 } else {
75 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
76 }
77 } else {
78 if ($options =~ /^i$/i) {
79 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
80 } else {
81 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
82 }
83 }
84 }
85 }
86
87 # this is another document
88 $self->{'num_docs'} += 1;
89
90 # get the parameters for the output
91 # split on : just in case there is subcoll and lang stuff
92 my ($fields) = split (/:/, $self->{'index'});
93
94 my $doc_level = $mgppbuildproc::level_map{'document'};
95 my $gs2ns = 'xmlns:gs2="http://www.greenstone.org/gs2"';
96
97 my $levels = $self->{'levels'};
98 my $ldoc_level = $levels->{'document'};
99 my $lsec_level = $levels->{'section'};
100 my $lpar_level = $levels->{'paragraph'};
101
102 my $docid="";
103 if ($ldoc_level) {
104 my $doc_sec_num = $self->{'num_sections'}+1;
105 $docid = "gs2:id=\"$doc_sec_num\"";
106 }
107
108 my $documenttag = "<$doc_level $gs2ns file=\"$file\" $docid >\n";
109 my $documentendtag = "\n</$doc_level>\n";
110
111 my ($sectiontag) = "";
112 if ($lsec_level) {
113 $sectiontag = $mgppbuildproc::level_map{'section'};
114 }
115 my ($parastarttag) = "";
116 my ($paraendtag) = "";
117 if ($self->{'levels'}->{'paragraph'}) {
118 if ($self->{'strip_html'}) {
119 $parastarttag = "<".$mgppbuildproc::level_map{'paragraph'}.">";
120 $paraendtag = "</".$mgppbuildproc::level_map{'paragraph'}.">";
121 } else {
122 print $outhandle "Paragraph level can not be used with no_strip_html!. Not indexing Paragraphs.\n";
123 }
124 }
125
126 my $doc_section = 0; # just for this document
127
128 my $text = "";
129 $text .= $documenttag;
130 # get the text for this document
131 my $section = $doc_obj->get_top_section();
132 while (defined $section) {
133 # update a few statistics
134 $doc_section++;
135 $self->{'num_sections'} += 1;
136
137 if ($sectiontag ne "") {
138 my $secid = "gs2:id=\"".$self->{'num_sections'}."\"";
139 $text .= "\n<$sectiontag $secid >\n";
140 }
141
142 # if we are doing subcollections, then some docs shouldn't be indexed.
143 # but we need to put the section tag placeholders in there so the
144 # sections match up with gdbm db
145 if ($indexed_doc) {
146 #if ($self->{'indexing_text'}) {
147 # $text .= "$parastarttag"; # only add para tags for indexing
148 # note that we assume that metadata will not be asked for for the compressed text, so we add para tags without checking for indexing_text
149 # }
150 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
151 foreach my $field (split (/,/, $fields)) {
152 # only deal with this field if it doesn't start with top or
153 # this is the first section
154 my $real_field = $field;
155 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
156 my $new_text = "";
157 my $tmp_text = "";
158 if ($real_field eq "text") {
159 if ($self->{'indexing_text'}) { #tag the text with <Text>...</Text>, add the <Paragraph> tags and strip out html if needed
160 $new_text .= "$parastarttag<TX index=\"1\">\n";
161 $tmp_text .= $doc_obj->get_text ($section);
162 if ($parastarttag =~ "") {
163 # we don't want to individually tag each paragraph if not doing para indexing
164 $tmp_text = $self->preprocess_text($tmp_text, $self->{'strip_html'}, "");
165 } else {
166 $tmp_text = $self->preprocess_text($tmp_text, $self->{'strip_html'}, "</TX>$paraendtag$parastarttag<TX index=\"1\">");
167 }
168
169 $new_text .= "$tmp_text</TX>$paraendtag\n";
170 #if (!defined $self->{'indexfields'}->{'TextOnly'}) {
171 #$self->{'indexfields'}->{'TextOnly'} = 1;
172 #}
173 }
174 else { # leave html stuff in, but escape the tags, and dont add Paragraph tags - never retrieve paras at the moment
175 if ($self->{'store_text'}) {
176 $tmp_text .= $doc_obj->get_text ($section);
177 $tmp_text =~ s/</&lt;/g;
178 $tmp_text =~ s/>/&gt;/g;
179 $new_text .= $tmp_text;
180 }
181 }
182 } else { # metadata field
183 if ($real_field eq "allfields") { #ignore
184 }
185 elsif ($real_field eq "metadata") { # insert all metadata
186 #except gsdl stuff
187 my $shortname = "";
188 my $metadata = $doc_obj->get_all_metadata ($section);
189 foreach $pair (@$metadata) {
190 my ($mfield, $mvalue) = (@$pair);
191 # check fields here, maybe others dont want - change to use dontindex!!
192 if ($mfield ne "Identifier"
193 && $mfield !~ /^gsdl/
194 && $mfield ne "classifytype"
195 && $mfield ne "assocfilepath"
196 && defined $mvalue && $mvalue ne "") {
197
198 if (defined $self->{'indexfieldmap'}->{$mfield}) {
199 $shortname = $self->{'indexfieldmap'}->{$mfield};
200 }
201 else {
202 $shortname = $self->create_shortname($mfield);
203 $self->{'indexfieldmap'}->{$mfield} = $shortname;
204 $self->{'indexfieldmap'}->{$shortname} = 1;
205 }
206 $new_text .= "$parastarttag<$shortname index=\"1\">$mvalue</$shortname>$paraendtag\n";
207 if (!defined $self->{'indexfields'}->{$mfield}) {
208 $self->{'indexfields'}->{$mfield} = 1;
209 }
210 }
211 }
212
213 }
214 else { #individual metadata specified
215 my $shortname="";
216 #if (!defined $self->{'indexfields'}->{$real_field}) {
217 #$self->{'indexfields'}->{$real_field} = 1;
218 #}
219 if (defined $self->{'indexfieldmap'}->{$real_field}) {
220 $shortname = $self->{'indexfieldmap'}->{$real_field};
221 }
222 else {
223 $shortname = $self->create_shortname($real_field);
224 $self->{'indexfieldmap'}->{$real_field} = $shortname;
225 $self->{'indexfieldmap'}->{$shortname} = 1;
226 }
227 foreach $item (@{$doc_obj->get_metadata ($section, $real_field)}) {
228 $new_text .= "$parastarttag<$shortname index=\"1\">$item</$shortname>$paraendtag\n";
229 }
230 }
231
232 }
233
234 # filter the text
235 $self->filter_text ($field, $new_text);
236
237 $self->{'num_processed_bytes'} += length ($new_text);
238 $text .= "$new_text";
239 }
240 }
241 } # if (indexed_doc)
242
243 $text .= "\n</$sectiontag>\n" if ($sectiontag ne "");
244
245 $section = $doc_obj->get_next_section($section);
246 } #while defined section
247 print $handle "$text\n$documentendtag";
248
249}
250
2511;
252
Note: See TracBrowser for help on using the repository browser.