source: trunk/gsdl/perllib/mgbuildproc.pm@ 544

Last change on this file since 544 was 544, checked in by sjboddie, 25 years ago

minor change so 'filename' can be any case

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1###########################################################################
2#
3# mgbuildproc.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 mg to process
28
29
30package mgbuildproc;
31
32use classify;
33use doc;
34use docproc;
35use util;
36
37
38BEGIN {
39 @ISA = ('docproc');
40}
41
42
43sub new {
44 my ($class, $collection, $source_dir, $build_dir, $verbosity) = @_;
45 my $self = new docproc ();
46
47 $self->{'collection'} = $collection;
48 $self->{'source_dir'} = $source_dir;
49 $self->{'build_dir'} = $build_dir;
50 $self->{'verbosity'} = $verbosity;
51 $self->{'classifiers'} = [];
52 $self->{'mode'} = "text";
53 $self->{'index'} = "section:text";
54 $self->{'indexexparr'} = [];
55 $self->{'output_handle'} = "STDOUT";
56 $self->{'num_docs'} = 0;
57 $self->{'num_sections'} = 0;
58 $self->{'num_bytes'} = 0;
59
60 $self->{'indexing_text'} = 0;
61
62 return bless $self, $class;
63}
64
65sub reset {
66 my $self = shift (@_);
67
68 $self->{'num_docs'} = 0;
69 $self->{'num_sections'} = 0;
70 $self->{'num_bytes'} = 0;
71}
72
73sub get_num_docs {
74 my $self = shift (@_);
75
76 return $self->{'num_docs'};
77}
78
79sub get_num_sections {
80 my $self = shift (@_);
81
82 return $self->{'num_sections'};
83}
84
85sub get_num_bytes {
86 my $self = shift (@_);
87
88 return $self->{'num_bytes'};
89}
90
91sub set_output_handle {
92 my $self = shift (@_);
93 my ($handle) = @_;
94
95 $self->{'output_handle'} = $handle;
96}
97
98sub set_mode {
99 my $self = shift (@_);
100 my ($mode) = @_;
101
102 $self->{'mode'} = $mode;
103}
104
105sub set_index {
106 my $self = shift (@_);
107 my ($index, $indexexparr) = @_;
108
109 $self->{'index'} = $index;
110 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
111}
112
113sub set_classifiers {
114 my $self = shift (@_);
115 my ($classifiers) = @_;
116
117 $self->{'classifiers'} = $classifiers;
118}
119
120sub set_indexing_text {
121 my $self = shift (@_);
122 my ($indexing_text) = @_;
123
124 $self->{'indexing_text'} = $indexing_text;
125}
126
127sub process {
128 my $self = shift (@_);
129 my $method = $self->{'mode'};
130
131 $self->$method(@_);
132}
133
134sub infodb {
135 my $self = shift (@_);
136 my ($doc_obj, $filename) = @_;
137 my $handle = $self->{'output_handle'};
138# $handle = "main::STDOUT";
139
140 my $doctype = $doc_obj->get_doc_type();
141
142 # only output this document if it is one to be indexed
143 return if ($doctype ne "indexed_doc");
144
145 # this is another document
146 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
147
148 my $section = $doc_obj->get_top_section ();
149 my $doc_OID = $doc_obj->get_OID();
150 while (defined $section) {
151 # update a few statistics
152 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
153 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
154
155 # output the section name
156 if ($section eq "") { print $handle "[$doc_OID]\n"; }
157 else { print $handle "[$doc_OID.$section]\n"; }
158
159 # output the fact that this document is a document
160 print $handle "<doctype>doc\n";
161
162 # output whether this node contains text
163 if ($doc_obj->get_text_length($section) > 0) {
164 print $handle "<hastxt>1\n";
165 } else {
166 print $handle "<hastxt>0\n";
167 }
168
169 # output all the section metadata
170 my $metadata = $doc_obj->get_all_metadata ($section);
171 foreach $pair (@$metadata) {
172 my ($field, $value) = (@$pair);
173
174 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
175 defined $value && $value ne "") {
176 # escape problematic stuff
177 $value =~ s/\\/\\\\/g;
178 $value =~ s/\n/\\n/g;
179 $value =~ s/\r/\\r/g;
180
181 print $handle "<$field>$value\n";
182 }
183 }
184
185 # output archivedir if at top level
186 if ($section eq $doc_obj->get_top_section()) {
187 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
188 $archivedir = "" unless defined $archivedir;
189 $archivedir =~ s/^(\/|\\)*//;
190 $archivedir =~ s/(\/|\\)*$//;
191 print $handle "<archivedir>$archivedir\n";
192 }
193
194 # output a list of children
195 my $children = $doc_obj->get_children ($section);
196 if (scalar(@$children) > 0) {
197 print $handle "<contains>";
198 my $firstchild = 1;
199 foreach $child (@$children) {
200 print $handle ";" unless $firstchild;
201 $firstchild = 0;
202 if ($child =~ /^.*?\.(\d+)$/) {
203 print $handle "\".$1";
204 } else {
205 print $handle "\".$child";
206 }
207# if ($child eq "") { print $handle "$doc_OID"; }
208# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
209# else { print $handle "$doc_OID.$section.$child"; }
210 }
211 print $handle "\n";
212 }
213
214 # output the matching document number
215 print $handle "<docnum>$self->{'num_sections'}\n";
216
217 print $handle '-' x 70, "\n";
218
219
220 # output a database entry for the document number
221 print $handle "[$self->{'num_sections'}]\n";
222 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
223 else { print $handle "<section>$doc_OID.$section\n"; }
224 print $handle '-' x 70, "\n";
225
226
227 $section = $doc_obj->get_next_section($section);
228 }
229
230 # classify this document
231 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
232
233}
234
235sub find_paragraphs {
236 $_[1] =~ s/(<p\b)/\cC$1/gi;
237}
238
239sub filter_text {
240 # $self->filter_text ($field, $new_text);
241 # don't want to do anything for this version, however,
242 # in a particular collection you might want to override
243 # this method to post-process certain fields depending on
244 # the field, or whether we are outputting it for indexing
245}
246
247sub text {
248 my $self = shift (@_);
249 my ($doc_obj) = @_;
250 my $handle = $self->{'output_handle'};
251 my $indexed_doc = 1;
252
253 # only output this document if it is one to be indexed
254 return if ($doc_obj->get_doc_type() ne "indexed_doc");
255
256 # see if this document belongs to this subcollection
257 foreach $indexexp (@{$self->{'indexexparr'}}) {
258 $indexed_doc = 0;
259 my ($field, $exp, $options) = split /\//, $indexexp;
260 if (defined ($field) && defined ($exp)) {
261 my ($bool) = $field =~ /^(.)/;
262 $field =~ s/^.// if $bool eq '!';
263 if ($field =~ /^filename$/i) {
264 $field = $doc_obj->get_source_filename();
265 } else {
266 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
267 }
268 next unless defined $field;
269 if ($bool eq '!') {
270 if ($options =~ /^i$/i) {
271 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
272 } else {
273 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
274 }
275 } else {
276 if ($options =~ /^i$/i) {
277 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
278 } else {
279 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
280 }
281 }
282 }
283 }
284
285 # this is another document
286 $self->{'num_docs'} += 1;
287
288 # get the parameters for the output
289 my ($level, $fields) = split (/:/, $self->{'index'});
290 $fields =~ s/\ball\b/Title,Creator,text/;
291 $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
292
293 my $doc_section = 0; # just for this document
294 my $text = "";
295 my $text_extra = "";
296
297 # get the text for this document
298 my $section = $doc_obj->get_top_section();
299 while (defined $section) {
300 # update a few statistics
301 $doc_section++;
302 $self->{'num_sections'} += 1;
303
304 if ($indexed_doc) {
305 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
306 foreach $field (split (/,/, $fields)) {
307 # only deal with this field if it doesn't start with top or
308 # this is the first section
309 my $real_field = $field;
310 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
311 my $new_text = "";
312 if ($real_field eq "text") {
313 $new_text = $doc_obj->get_text ($section);
314 $new_text =~ s/[\cB\cC]//g;
315 $self->find_paragraphs($new_text);
316
317 } else {
318 $new_text = join ("\cC", @{$doc_obj->get_metadata ($section, $real_field)});
319 }
320
321 # filter the text
322 $self->filter_text ($field, $new_text);
323
324 if ($self->{'indexing_text'} &&
325 $new_text =~ /[\(\)\{\}]/) {
326 }
327
328 $text .= "$new_text\cC";
329 }
330 }
331 }
332
333 if ($level eq "document") { $text_extra .= "\cB"; }
334 else { $text .= "\cB"; }
335
336 $section = $doc_obj->get_next_section($section);
337 }
338
339 print $handle "$text$text_extra";
340}
341
3421;
343
Note: See TracBrowser for help on using the repository browser.