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