pre-exception MARC validation abstracted
[migration-tools.git] / fingerprinter
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use open ':utf8';
5
6 use Getopt::Long;
7 use MARC::Batch;
8 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
9 use MARC::Field;
10 use Unicode::Normalize;
11
12 my $conf  = {}; # configuration hashref
13 my $count = 0;
14 $| = 1;
15
16 initialyze($conf);
17
18 open OF, '>', $conf->{output};
19 binmode(OF, ':utf8');
20 open XF, '>', $conf->{exception};
21 binmode(XF, ':utf8');
22
23 for my $file (@ARGV) {
24     print XF "Processing $file\n";
25     open my $records, '<:utf8', $file;
26
27     my $batch = MARC::Batch->new('XML', $records);
28     $batch->strict_off();
29     $batch->warnings_off();
30
31     while ( my $record = $batch->next ) {
32         $count++; progress_ticker();
33
34         my $id = $record->field($conf->{tag});
35         unless ($id) {
36             print XF "ERROR: Record $count in $file is missing a",
37               $conf->{tag}, "field.\n", $record->as_formatted(), "\n=====\n";
38             next;
39         }
40
41         my $marc = populate_marc($record, $id);
42         $marc    = normalize_marc($marc);
43         unless (marc_isvalid($marc))
44           { dump_exception($marc); next; }
45         dump_fingerprints($marc);
46     }
47 }
48 print "\nProcessed $count records\n" unless $conf->{quiet};
49
50
51
52 =head2 populate_marc
53
54 Constructs a hash containing the relevant MARC data for a record and
55 returns a reference to it.
56
57 =cut
58
59 sub populate_marc {
60     my ($record, $id) = @_;
61     my %marc = (); $marc{isbns} = [];
62
63     # id, stringified
64     $marc{id} = $id->as_string($conf->{subfield});
65
66     # record_type, bib_lvl
67     $marc{record_type} = substr($record->leader, 6, 1);
68     $marc{bib_lvl}     = substr($record->leader, 7, 1);
69
70     # date1, date2
71     my $my_008 = $record->field('008');
72     $my_008 = $my_008->as_string() if ($my_008);
73     $marc{date1} = substr($my_008,7,4) if ($my_008);
74     $marc{date2} = substr($my_008,11,4) if ($my_008); # UNUSED
75
76     # item_form
77     if ( $marc{record_type} =~ /[gkroef]/ ) { # MAP, VIS
78         $marc{item_form} = substr($my_008,29,1) if ($my_008);
79     } else {
80         $marc{item_form} = substr($my_008,23,1) if ($my_008);
81     }
82
83     # isbns
84     my @isbns = $record->field('020') if $record->field('020');
85     push @isbns, $record->field('024') if $record->field('024');
86     for my $f ( @isbns ) {
87         push @{ $marc{isbns} }, $1 if ( defined $f->subfield('a') and
88                                         $f->subfield('a')=~/(\S+)/ );
89     }
90
91     # author
92     for my $rec_field (100, 110, 111) {
93         if ($record->field($rec_field)) {
94             $marc{author} = $record->field($rec_field)->subfield('a');
95             last;
96         }
97     }
98
99     # issn, lccn, title, desc, pages, pub, pubyear, edition
100     $marc{lccn} = $record->field('010')->subfield('a') if $record->field('010');
101     $marc{issn} = $record->field('022')->subfield('a') if $record->field('022');
102     $marc{desc} = $record->field('300')->subfield('a') if $record->field('300');
103     $marc{pages} = $1 if (defined $marc{desc} and $marc{desc} =~ /(\d+)/);
104     $marc{title} = $record->field('245')->subfield('a')
105       if defined $record->field('245');
106     $marc{edition} = $record->field('250')->subfield('a')
107       if $record->field('250');
108     if ($record->field('260')) {
109         $marc{publisher} = $record->field('260')->subfield('b');
110         $marc{pubyear} = $record->field('260')->subfield('c');
111         $marc{pubyear} =
112           (defined $marc{pubyear} and $marc{pubyear} =~ /(\d{4})/) ? $1 : '';
113     }
114     return \%marc;
115 }
116
117
118
119 =head2 normalize_marc
120
121 Gently massages your data.
122
123 =cut
124
125 sub normalize_marc {
126     my ($marc) = @_;
127
128     $marc->{record_type }= 'a' if ($marc->{record_type} eq ' ');
129     if ($marc->{title}) {
130         $marc->{title} = NFD($marc->{title});
131         $marc->{title} =~ s/[\x{80}-\x{ffff}]//go;
132         $marc->{title} = lc($marc->{title});
133         $marc->{title} =~ s/\W+$//go;
134     }
135     if ($marc->{author}) {
136         $marc->{author} = NFD($marc->{author});
137         $marc->{author} =~ s/[\x{80}-\x{ffff}]//go;
138         $marc->{author} = lc($marc->{author});
139         $marc->{author} =~ s/\W+$//go;
140         if ($marc->{author} =~ /^(\w+)/) {
141             $marc->{author} = $1;
142         }
143     }
144     if ($marc->{publisher}) {
145         $marc->{publisher} = NFD($marc->{publisher});
146         $marc->{publisher} =~ s/[\x{80}-\x{ffff}]//go;
147         $marc->{publisher} = lc($marc->{publisher});
148         $marc->{publisher} =~ s/\W+$//go;
149         if ($marc->{publisher} =~ /^(\w+)/) {
150             $marc->{publisher} = $1;
151         }
152     }
153     return $marc;
154 }
155
156
157
158 =head2 marc_isvalid
159
160 Checks MARC record to see if neccessary fingerprinting data is
161 available
162
163 =cut
164
165 sub marc_isvalid {
166     my ($marc) = @_;
167     return 1 if ($marc->{item_form} and ($marc->{date1} =~ /\d{4}/) and
168                  $marc->{record_type} and $marc->{bib_lvl} and $marc->{title});
169     return 0;
170 }
171
172
173 =head2 dump_fingerprints
174
175 =cut
176
177 sub dump_fingerprints {
178     my ($marc) = @_;
179
180     if ($conf->{runtype} eq "primary") {
181         print OF join("\t",$marc->{id}, $marc->{item_form},
182                           $marc->{date1}, $marc->{record_type},
183                           $marc->{bib_lvl}, $marc->{title}), "\n";
184     } else {
185         if ((scalar @{ $marc->{isbns} } > 0) && $marc->{pages}) {
186             # case a : isbn and pages
187             foreach my $isbn ( @{ $marc->{isbns}} ) {
188                 print OF join("\t", $marc->{id}, "case a",
189                                   $marc->{item_form}, $marc->{date1},
190                                   $marc->{record_type},
191                                   $marc->{bib_lvl}, $marc->{title},
192                                   $isbn, $marc->{pages}), "\n";
193             }
194         }
195
196         if ($marc->{edition}) { # case b : edition
197             print OF join("\t", $marc->{id}, "case b",
198                               $marc->{item_form}, $marc->{date1},
199                               $marc->{record_type}, $marc->{bib_lvl},
200                               $marc->{title}, $marc->{edition}), "\n";
201         }
202
203         if ($marc->{issn}) { # case c : issn
204             print OF join("\t", $marc->{id}, "case c",
205                               $marc->{item_form}, $marc->{date1},
206                               $marc->{record_type}, $marc->{bib_lvl},
207                               $marc->{title}, $marc->{issn}), "\n";
208         }
209
210         if ($marc->{lccn}) { # case d : lccn
211             print OF join("\t", $marc->{id}, "case d",
212                               $marc->{item_form}, $marc->{date1},
213                               $marc->{record_type}, $marc->{bib_lvl},
214                               $marc->{title}, $marc->{lccn}) ,"\n";
215         }
216
217         # case e : author, publisher, pubyear, pages
218         if ($marc->{author} and $marc->{publisher} and $marc->{pubyear}
219             and $marc->{pages}) {
220             print OF join("\t", $marc->{id}, "case e",
221                               $marc->{item_form}, $marc->{date1},
222                               $marc->{record_type}, $marc->{bib_lvl},
223                               $marc->{title}, $marc->{author},
224                               $marc->{publisher}, $marc->{pubyear},
225                               $marc->{pages}), "\n";
226         }
227     }
228 }
229
230
231
232 =head2 dump_exception
233
234 Write line of exception report
235
236 =cut
237
238 sub dump_exception {
239     my ($marc) = @_;
240     print XF "Record ", $marc->{id}, " did not make the cut: ";
241     print XF "Missing item_form. " unless ($marc->{item_form});
242     print XF "Missing valid date1. "
243       unless (defined $marc->{date1} and $marc->{date1} =~ /\d{4}/);
244     print XF "Missing record_type. " unless ($marc->{record_type});
245     print XF "Missing bib_lvl. " unless ($marc->{bib_lvl});
246     print XF "Missing title. " unless ($marc->{title});
247     print XF "\n";
248 }
249
250
251 =head2 initialyze
252
253 Performs boring script initialization. Handles argument parsing,
254 mostly.
255
256 =cut
257
258 sub initialyze {
259     my ($c) = @_;
260     my @missing = ();
261
262     # set mode on existing filehandles
263     binmode(STDIN, ':utf8');
264
265     my $rc = GetOptions( $c,
266                          'exception|x=s',
267                          'output|o=s',
268                          'runtype|r=s',
269                          'subfield|s=s',
270                          'tag|t=s',
271                          'quiet|q',
272                          'help|h',
273                        );
274     show_help() unless $rc;
275
276     my @keys = keys %{$c};
277     show_help() unless (@ARGV and @keys);
278     for my $key ('runtype', 'tag', 'subfield', 'output', 'exception') {
279         push @missing, $key unless $c->{$key}
280     }
281     if (@missing) {
282         print "Required option: ", join(', ', @missing), " missing!\n";
283         show_help();
284     }
285
286     show_help() if ($c->{help});
287 }
288
289
290 =head2 progress_ticker
291
292 =cut
293
294 sub progress_ticker {
295     return if $conf->{quiet};
296
297     if ($count % 100 == 0) {
298         print '|';
299         print " $count \n" unless ($count % 1400);
300     } elsif ($count % 20 == 0) {
301         print '.';
302     }
303 }
304
305 =head2 show_help
306
307 Display usage message when things go wrong
308
309 =cut
310
311 sub show_help {
312 print <<HELP;
313 Usage is: fingerprinter [REQUIRED ARGS] [OPTIONS] <filelist>
314 Req'd Arguments
315   --runtype=(primary|full) -r  Do 'primary' or 'full' fingerprinting
316   --tag=N                  -t  Which tag to use
317   --subfield=X             -s  Which subfield to use
318   --output=<file>          -o  Output filename
319   --exceptions=<file>      -x  Exception report filename
320 Options
321   --quiet  -q  Don't write status messages to STDOUT
322 HELP
323 exit 1;
324 }