source: gsdl/trunk/perllib/mgppbuildproc.pm@ 14912

Last change on this file since 14912 was 14912, checked in by qq6, 16 years ago

updated by Veronica

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1###########################################################################
2#
3# mgppbuildproc.pm --
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
26# This document processor outputs a document
27# for mgpp to process
28
29
30package mgppbuildproc;
31
32use basebuildproc;
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36
37BEGIN {
38 @mgppbuildproc::ISA = ('basebuildproc');
39}
40
41#this must be the same as in mgppbuilder
42our %level_map = ('document'=>'Doc',
43 'section'=>'Sec',
44 'paragraph'=>'Para');
45
46sub new {
47 my $class = shift @_;
48 my $self = new basebuildproc (@_);
49
50 # use a different index specification to the default
51 $self->{'index'} = "text";
52
53 $self->{'dontindex'} = {};
54 $self->{'indexfieldmap'} = {};
55 $self->{'indexfields'} = {}; # only put in the ones that are not specified directly in the index
56 $self->{'strip_html'}=1;
57
58 return bless $self, $class;
59}
60
61
62sub set_indexfieldmap {
63 my $self = shift (@_);
64 my ($indexmap) = @_;
65
66 $self->{'indexfieldmap'} = $indexmap;
67}
68
69sub get_indexfieldmap {
70 my $self = shift (@_);
71
72 return $self->{'indexfieldmap'};
73}
74
75sub set_levels {
76 my $self = shift (@_);
77 my ($levels) = @_;
78
79 $self->{'levels'} = $levels;
80}
81
82sub set_strip_html {
83 my $self = shift (@_);
84 my ($strip) = @_;
85 $self->{'strip_html'}=$strip;
86}
87
88#sub find_paragraphs {
89# $_[1] =~ s/(<p\b)/<Paragraph>$1/gi;
90#}
91
92sub remove_gtlt {
93 my $self =shift(@_);
94 my ($text, $para) = @_;
95 $text =~s/[<>]//g;
96 return "$para$text$para";
97}
98
99sub process_tags {
100 my $self = shift(@_);
101 my ($text, $para) = @_;
102 if ($text =~ /^p\b/i) {
103 return $para;
104 }
105 return "";
106}
107
108sub preprocess_text {
109 my $self = shift (@_);
110 my ($text, $strip_html, $para) = @_;
111 # at this stage, we do not do paragraph tags unless have strip_html -
112 # it will result in a huge mess of non-xml
113 return unless $strip_html;
114
115 my $new_text = $text;
116
117 # if we have <pre> tags, we can have < > inside them, need to delete
118 # the <> before stripping tags
119 $new_text =~ s/<pre>(.*?)<\/pre>/$self->remove_gtlt($1,$para)/gse;
120
121 if ($para eq "") {
122 # just remove all tags
123 $new_text =~ s/<[^>]*>//gs;
124 } else {
125 # strip all tags except <p> tags which get turned into $para
126 $new_text =~ s/<([^>]*)>/$self->process_tags($1, $para)/gse;
127
128 }
129 return $new_text;
130}
131#this function strips the html tags from the doc if ($strip_html) and
132# if ($para) replaces <p> with <Paragraph> tags.
133# if both are false, the original text is returned
134#assumes that <pre> and </pre> have no spaces, and removes all < and > inside
135#these tags
136sub preprocess_text_old_and_slow {
137 my $self = shift (@_);
138 my ($text, $strip_html, $para) = @_;
139 my ($outtext) = "";
140 if ($strip_html) {
141 while ($text =~ /<([^>]*)>/ && $text ne "") {
142
143 my $tag = $1;
144 $outtext .= $`." "; #add everything before the matched tag
145 $text = $'; #'everything after the matched tag
146 if ($para && $tag =~ /^\s*p\s/i) {
147 $outtext .= $para;
148 }
149 elsif ($tag =~ /^pre$/) { # a pre tag
150 $text =~ /<\/pre>/; # find the closing pre tag
151 my $tmp_text = $`; #everything before the closing pre tag
152 $text = $'; #'everything after the </pre>
153 $tmp_text =~ s/[<>]//g; # remove all < and >
154 $outtext.= $tmp_text . " ";
155 }
156 }
157
158 $outtext .= $text; # add any remaining text
159 return $outtext;
160 } #if strip_html
161
162 #if ($para) {
163 #$text =~ s/(<p\b)/$para$1/gi;
164 #return $text;
165 # }
166 return $text;
167}
168
169
170
171sub filter_text {
172 # $self->filter_text ($field, $new_text);
173 # don't want to do anything for this version, however,
174 # in a particular collection you might want to override
175 # this method to post-process certain fields depending on
176 # the field, or whether we are outputting it for indexing
177}
178
179sub text {
180 my $self = shift (@_);
181 my ($doc_obj) = @_;
182 my $handle = $self->{'output_handle'};
183 my $outhandle = $self->{'outhandle'};
184
185 # only output this document if it is one to be indexed
186 return if ($doc_obj->get_doc_type() ne "indexed_doc");
187
188 my $indexed_doc = $self->is_subcollection_doc($doc_obj);
189
190 # this is another document
191 $self->{'num_docs'} += 1;
192
193 # get the parameters for the output
194 # split on : just in case there is subcoll and lang stuff
195 my ($fields) = split (/:/, $self->{'index'});
196
197 # we always do text and index on Doc and Sec levels
198 my ($documenttag) = "\n<". $level_map{'document'} . ">\n";
199 my ($documentendtag) = "\n</". $level_map{'document'} . ">\n";
200 my ($sectiontag) = "\n<". $level_map{'section'} . ">\n";
201 my ($sectionendtag) = "\n</". $level_map{'section'} . ">\n";
202
203 my ($paratag) = "";
204
205 # paragraph tags will only be used for indexing (can't retrieve
206 # paragraphs), and can ony be used if we are stripping HTML tags
207 if ($self->{'indexing_text'} && $self->{'levels'}->{'paragraph'}) {
208 if ($self->{'strip_html'}) {
209 $paratag = "<". $level_map{'paragraph'} . ">";
210 } else {
211 print $outhandle "Paragraph level can not be used with no_strip_html!. Not indexing Paragraphs.\n";
212 }
213 }
214
215 my $doc_section = 0; # just for this document
216
217 my $text = $documenttag;
218
219 # get the text for this document
220 my $section = $doc_obj->get_top_section();
221
222 while (defined $section) {
223 # update a few statistics
224 $doc_section++;
225 $self->{'num_sections'} += 1;
226 $text .= "$sectiontag";
227
228 my $indexed_section = $doc_obj->get_metadata_element($section, "gsdldoctype") || "indexed_section";
229 if (($indexed_doc == 0) || ($indexed_section ne "indexed_section" && $indexed_section ne "indexed_doc")) {
230 # we are not actually indexing anything for this document,
231 # but we want to keep the section numbers the same, so we just
232 # output section tags for each section (which is done above)
233 $text .= "$sectionendtag";
234 $section = $doc_obj->get_next_section($section);
235 next;
236 }
237
238 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
239 foreach my $field (split (/;/, $fields)) {
240 # only deal with this field if it doesn't start with top or
241 # this is the first section
242 my $real_field = $field;
243 next if (($real_field =~ s/^top//) && ($doc_section != 1));
244
245 my $new_text = "";
246
247 # we get allfields by default - do nothing
248 if ($real_field eq "allfields") {
249
250 }
251
252 # metadata - output all metadata we know about except gsdl stuff
253 elsif ($real_field eq "metadata") {
254 my $shortname = "";
255 my $metadata = $doc_obj->get_all_metadata ($section);
256 foreach my $pair (@$metadata) {
257 my ($mfield, $mvalue) = (@$pair);
258 # check fields here, maybe others dont want - change to use dontindex!!
259 if ($mfield ne "Identifier"
260 && $mfield !~ /^gsdl/
261 && $mfield ne "classifytype"
262 && $mfield ne "assocfilepath"
263 && defined $mvalue && $mvalue ne "") {
264
265 if (defined $self->{'indexfieldmap'}->{$mfield}) {
266 $shortname = $self->{'indexfieldmap'}->{$mfield};
267 }
268 else {
269 $shortname = $self->create_shortname($mfield);
270 $self->{'indexfieldmap'}->{$mfield} = $shortname;
271 $self->{'indexfieldmap'}->{$shortname} = 1;
272 }
273 $new_text .= "$paratag<$shortname>$mvalue</$shortname>\n";
274 if (!defined $self->{'indexfields'}->{$mfield}) {
275 $self->{'indexfields'}->{$mfield} = 1;
276 }
277 }
278 }
279 }
280 else {
281 #individual metadata and or text specified - could be
282 # a comma separated list
283 my $shortname="";
284 if (defined $self->{'indexfieldmap'}->{$real_field}) {
285 $shortname = $self->{'indexfieldmap'}->{$real_field};
286 }
287 else {
288 $shortname = $self->create_shortname($real_field);
289 $self->{'indexfieldmap'}->{$real_field} = $shortname;
290 $self->{'indexfieldmap'}->{$shortname} = 1;
291 }
292 my @metadata_list = ();
293 foreach my $submeta (split /,/, $real_field) {
294 if ($submeta eq "text") {
295 my $section_text = $doc_obj->get_text($section);
296 if ($self->{'indexing_text'}) {
297 # tag the text with <Text>...</Text>, add the <Paragraph> tags and strip out html if needed
298 $new_text .= "$paratag<$shortname>\n";
299 if ($paratag ne "") {
300 $section_text = $self->preprocess_text($section_text, $self->{'strip_html'}, "</$shortname>$paratag<$shortname>");
301 }
302 else {
303 $section_text = $self->preprocess_text($section_text, $self->{'strip_html'}, "");
304 }
305 $new_text .= "$section_text</$shortname>\n";
306 }
307 else {
308 # leave html stuff in, and don't add Paragraph tags - never retrieve paras at the moment
309 $new_text .= $section_text;
310 }
311 }
312 else {
313 my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
314 if ($section ne $doc_obj->get_top_section() && $self->{'indexing_text'} && defined ($self->{'sections_index_document_metadata'})) {
315 if ($self->{'sections_index_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_index_document_metadata'} eq "unless_section_metadata_exists")) {
316 push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)});
317 }
318 }
319 push (@metadata_list, @section_metadata);
320 }
321 }
322 foreach my $item (@metadata_list) {
323 $new_text .= "$paratag<$shortname>$item</$shortname>\n";
324 }
325 }
326
327 # filter the text
328 $self->filter_text ($field, $new_text);
329
330 $self->{'num_processed_bytes'} += length ($new_text);
331 $text .= "$new_text";
332 } # foreach field
333
334 $text .= "$sectionendtag";
335 $section = $doc_obj->get_next_section($section);
336 } # while defined section
337 print $handle "$text\n$documentendtag";
338
339}
340
341#chooses the first two letters or digits for the shortname
342#now ignores non-letdig characters
343sub create_shortname {
344 my $self = shift(@_);
345
346 my ($realname) = @_;
347 #take the first two chars
348 my $shortname;
349 if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
350 $shortname = "$1$2";
351 } else {
352 # there aren't two letdig's in the field - try arbitrary combinations
353 $realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
354 $shortname = "AB";
355 }
356 $shortname =~ tr/a-z/A-Z/;
357
358 #if already used, take the first and third letdigs and so on
359 my $count = 1;
360 while (defined $self->{'indexfieldmap'}->{$shortname}) {
361 if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
362 $shortname = "$1$3";
363 $count++;
364 $shortname =~ tr/a-z/A-Z/;
365
366 }
367 else {
368 #remove up to and incl the first letdig
369 $realname =~ s/^[^\w]*\w//;
370 $count = 0;
371 }
372 }
373
374 return $shortname;
375}
376
3771;
378
Note: See TracBrowser for help on using the repository browser.