8 use Unicode::Normalize;
9 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
11 my $conf = {}; # configuration hashref
12 my $count = 0; my $scount = 0;
18 open OF, '>', $conf->{output};
19 open XF, '>', $conf->{exception};
21 for my $file (@ARGV) {
22 print XF "Processing $file\n";
23 my $batch = undef; my $record = undef;
25 $batch = MARC::Batch->new($conf->{marctype}, $file);
27 $batch->warnings_off();
29 while ( $record = $batch->next ) {
30 $count++; progress_ticker();
32 unless ( defined $record )
33 { dump_exception($marc); next; }
35 my $id = $record->field($conf->{tag});
37 print XF "ERROR: Record $count in $file is missing a ",
38 $conf->{tag}, " field.\n", $record->as_formatted(), "\n=====\n";
42 # populate and normalize marc
43 $marc = populate_marc($record, $id);
44 normalize_marc($marc);
45 unless (marc_isvalid($marc))
46 { dump_exception($marc); next; }
48 # if everything looks good, score it and dump fingerprints
49 score_marc($marc, $record);
50 dump_fingerprints($marc);
51 $scount++; progress_ticker();
55 print "\nSuccessfully processed:\t$count\n" unless $conf->{quiet};
59 Constructs a hash containing the relevant MARC data for a record and
60 returns a reference to it.
65 my ($record, $id) = @_;
66 my %marc = (); $marc{isbns} = [];
69 $marc{id} = $id->as_string($conf->{subfield});
71 # record_type, bib_lvl
72 $marc{record_type} = substr($record->leader, 6, 1);
73 $marc{bib_lvl} = substr($record->leader, 7, 1);
76 my $my_008 = $record->field('008');
77 $my_008 = $my_008->as_string() if ($my_008);
78 if (defined $my_008) {
79 unless (length $my_008 == 40) {
80 $my_008 = $my_008 . (' ' x (40 - length($my_008)));
81 print XF ">> Short 008 padded to ",length($my_008)," at rec $count\n";
83 $marc{date1} = substr($my_008,7,4) if ($my_008);
84 $marc{date2} = substr($my_008,11,4) if ($my_008); # UNUSED
86 unless ($marc{date1} and $marc{date1} =~ /\d{4}/) {
87 my $my_260 = $record->field('260');
88 my $date1 = $my_260->subfield('c') if $my_260;
89 if (defined $date1 and $date1 =~ /\d{4}/) {
90 $marc{date1} = $date1;
91 print XF ">> using 260c as date1 at rec $count\n";
96 if ( $marc{record_type} =~ /[gkroef]/ ) { # MAP, VIS
97 $marc{item_form} = substr($my_008,29,1) if ($my_008);
99 $marc{item_form} = substr($my_008,23,1) if ($my_008);
103 my @isbns = $record->field('020') if $record->field('020');
104 push @isbns, $record->field('024') if $record->field('024');
105 for my $f ( @isbns ) {
106 push @{ $marc{isbns} }, $1 if ( defined $f->subfield('a') and
107 $f->subfield('a')=~/(\S+)/ );
111 for my $rec_field (100, 110, 111) {
112 if ($record->field($rec_field)) {
113 $marc{author} = $record->field($rec_field)->subfield('a');
120 push @{ $marc{oclc} }, $record->field('001')->as_string()
121 if ($record->field('001') and $record->field('003') and
122 $record->field('003')->as_string() =~ /OCo{0,1}LC/);
123 for ($record->field('035')) {
124 my $oclc = $_->subfield('a');
125 push @{ $marc{oclc} }, $oclc
126 if (defined $oclc and $oclc =~ /\(OCoLC\)/ and $oclc =~/([0-9]+)/);
129 # "Accompanying material" (300e)
130 $marc{accomp} = $record->field('300')->subfield('e')
131 if $record->field('300');
133 # issn, lccn, title, desc, pages, pub, pubyear, edition
134 $marc{lccn} = $record->field('010')->subfield('a') if $record->field('010');
135 $marc{issn} = $record->field('022')->subfield('a') if $record->field('022');
136 $marc{desc} = $record->field('300')->subfield('a') if $record->field('300');
137 $marc{pages} = $1 if (defined $marc{desc} and $marc{desc} =~ /(\d+)/);
138 $marc{title} = $record->field('245')->subfield('a')
139 if $record->field('245');
140 $marc{edition} = $record->field('250')->subfield('a')
141 if $record->field('250');
142 if ($record->field('260')) {
143 $marc{publisher} = $record->field('260')->subfield('b');
144 $marc{pubyear} = $record->field('260')->subfield('c');
146 (defined $marc{pubyear} and $marc{pubyear} =~ /(\d{4})/) ? $1 : '';
153 =head2 normalize_marc
155 Gently massages your data.
162 $marc->{record_type }= 'a' if ($marc->{record_type} eq ' ');
163 if ($marc->{title}) {
164 $marc->{title} = NFD($marc->{title});
165 $marc->{title} =~ s/[\x{80}-\x{ffff}]//go;
166 $marc->{title} = lc($marc->{title});
167 $marc->{title} =~ s/\W+$//go;
169 if ($marc->{author}) {
170 $marc->{author} = NFD($marc->{author});
171 $marc->{author} =~ s/[\x{80}-\x{ffff}]//go;
172 $marc->{author} = lc($marc->{author});
173 $marc->{author} =~ s/\W+$//go;
174 if ($marc->{author} =~ /^(\w+)/) {
175 $marc->{author} = $1;
178 if ($marc->{publisher}) {
179 $marc->{publisher} = NFD($marc->{publisher});
180 $marc->{publisher} =~ s/[\x{80}-\x{ffff}]//go;
181 $marc->{publisher} = lc($marc->{publisher});
182 $marc->{publisher} =~ s/\W+$//go;
183 if ($marc->{publisher} =~ /^(\w+)/) {
184 $marc->{publisher} = $1;
194 Checks MARC record to see if neccessary fingerprinting data is
201 return 1 if ($marc->{item_form} and ($marc->{date1} =~ /\d{4}/) and
202 $marc->{record_type} and $marc->{bib_lvl} and $marc->{title});
209 Assign a score to the record based on various criteria.
211 Score is constructed by pushing elements onto a list. At the end of
212 the routine, the list is flattened into a string via join();
217 my ($marc, $record) = @_;
221 # Is this an OCLC record?
222 if ($conf->{scores}{oclc})
223 { push @score, ( defined $marc->{oclc}[0] ? 1 : 0 ) }
225 # does 040a contain "dlc"?
226 if ($conf->{scores}{dlc}) {
227 if ($record->field('040') and $record->field('040')->subfield('a')) {
228 $chunk = $record->field('040')->subfield('a');
229 push @score, ( $chunk =~ /dlc/i ? 1 : 0 );
235 # number of 650 datafields
236 # zero-padded to 4 digits with printf
237 if ($conf->{scores}{num_650}) {
238 if ($record->field('650')) {
239 my @tags = $record->field('650');
240 push @score, ( sprintf("%04d", scalar @tags) );
246 # number of tags in total
247 # zero-padded to 4 digits with printf
248 if ($conf->{scores}{num_tags}) {
249 my @tags = $record->fields;
250 push @score, ( sprintf("%04d", scalar @tags) );
254 if ($conf->{scores}{enc_lvl}) {
255 my $enc = substr($record->leader, 17, 1);
256 my %levels = ( ' ' => 9, 1 => 8, 2 => 7, 3 => 6, 4 => 5, 5 => 4,
257 6 => 3, 7 => 2, 8 => 1, 'u' => 0, 'z' => 0 );
258 if (defined $enc and $levels{$enc})
259 { push @score, $levels{$enc} }
264 # put score in marc hash
265 my $json = join('', '{oclc:', $score[0], ',dlc:', $score[1],
266 ',num_650:', $score[2], ',num_tags:', $score[3],
267 ',enc_lvl:', $score[4], '}');
268 my $compact = join('', @score);
269 $marc->{score} = "$compact\t$json";
273 =head2 dyn_score_marc
275 Assign a score to the record based on various criteria.
277 Score is constructed by pushing elements onto a list. At the end of
278 the routine, the list is flattened into a string via join();
282 my %dyn_scores_code = (
283 oclc => sub { return $_[0]->{oclc}[0] ? 1 : 0 },
284 dlc => sub { return scalar($_[1]->subfield( '040', 'a')) =~ /dlc/io ? 1 : 0 },
285 num_650 => sub { return sprintf( '%04d', scalar( $_[1]->field('650') ) ) },
286 num_tags=> sub { return sprintf( '%04d', scalar( $_[1]->fields ) ) },
288 my $enc = substr($_[1]->leader, 17, 1) || 'u';
289 my %levels = ( ' ' => 9, 1 => 8, 2 => 7, 3 => 6, 4 => 5, 5 => 4,
290 6 => 3, 7 => 2, 8 => 1, 'u' => 0, 'z' => 0 );
291 return $levels{$enc} || 0;
297 my ($marc, $record) = @_;
301 for ( @{ $conf->{dyn_scores} } ) {
302 push @score, $dyn_scores_code{$_}->($marc, $record);
303 $json .= $_ . ':' . $score[-1] . ',';
305 chop($json); # get rid of the trailing comma
309 my $compact = join('', @score);
310 $marc->{score} = "$compact\t$json";
313 =head2 dump_fingerprints
317 sub dump_fingerprints {
320 if ($conf->{fingerprints}{baseline}) {
321 print OF join("\t", $marc->{score}, $marc->{id}, 'baseline',
322 $marc->{item_form}, $marc->{date1}, $marc->{record_type},
323 $marc->{bib_lvl}, $marc->{title}), "\n";
326 if ($conf->{fingerprints}{oclc} and scalar @{$marc->{oclc} }) {
327 for (@{$marc->{oclc} }) {
328 print OF join("\t", $marc->{score}, $marc->{id}, "oclc",
329 $marc->{item_form}, $marc->{date1},
330 $marc->{record_type}, $marc->{bib_lvl},
331 $marc->{title}, $_, "\n");
335 if ($conf->{fingerprints}{isbn}) {
336 if ((scalar @{ $marc->{isbns} } > 0) and $marc->{pages}) {
337 foreach my $isbn ( @{ $marc->{isbns}} ) {
338 print OF join("\t", $marc->{score}, $marc->{id}, "isbn",
339 $marc->{item_form}, $marc->{date1},
340 $marc->{record_type},
341 $marc->{bib_lvl}, $marc->{title},
342 $isbn, $marc->{pages}), "\n";
347 if ($conf->{fingerprints}{edition} and $marc->{edition}) {
348 print OF join("\t", $marc->{score}, $marc->{id}, "edition",
349 $marc->{item_form}, $marc->{date1},
350 $marc->{record_type}, $marc->{bib_lvl},
351 $marc->{title}, $marc->{edition}), "\n";
354 if ($conf->{fingerprints}{issn} and $marc->{issn}) {
355 print OF join("\t", $marc->{score}, $marc->{id}, "issn",
356 $marc->{item_form}, $marc->{date1},
357 $marc->{record_type}, $marc->{bib_lvl},
358 $marc->{title}, $marc->{issn}), "\n";
361 if ($conf->{fingerprints}{lccn} and $marc->{lccn}) {
362 print OF join("\t", $marc->{score}, $marc->{id}, "lccn",
363 $marc->{item_form}, $marc->{date1},
364 $marc->{record_type}, $marc->{bib_lvl},
365 $marc->{title}, $marc->{lccn}) ,"\n";
368 if ($conf->{fingerprints}{accomp} and $marc->{accomp}) {
369 print OF join("\t", $marc->{score}, $marc->{id}, "accomp",
370 $marc->{item_form}, $marc->{date1},
371 $marc->{record_type}, $marc->{bib_lvl},
372 $marc->{title}, $marc->{accomp}) ,"\n";
375 if ($conf->{fingerprints}{authpub} and $marc->{author} and
376 $marc->{publisher} and $marc->{pubyear} and $marc->{pages}) {
377 print OF join("\t", $marc->{score}, $marc->{id}, "authpub",
378 $marc->{item_form}, $marc->{date1},
379 $marc->{record_type}, $marc->{bib_lvl},
380 $marc->{title}, $marc->{author},
381 $marc->{publisher}, $marc->{pubyear},
382 $marc->{pages}), "\n";
388 =head2 dump_exception
390 Write line of exception report
396 unless (defined $marc) {
397 print XF "Undefined record at line $count; likely bad XML\n";
400 print XF "Record ", $marc->{id}, " did not make the cut: ";
401 print XF "Missing item_form. " unless ($marc->{item_form});
402 unless (defined $marc->{date1})
403 { print XF "Missing date1. " }
405 { print XF "Invalid date1: ", $marc->{date1}, " "
406 unless ($marc->{date1} =~ /\d{4}/); }
407 print XF "Missing record_type. " unless ($marc->{record_type});
408 print XF "Missing bib_lvl. " unless ($marc->{bib_lvl});
409 print XF "Missing title. " unless ($marc->{title});
416 Performs boring script initialization. Handles argument parsing,
425 # set mode on existing filehandles
426 binmode(STDIN, ':utf8');
428 my $rc = GetOptions( $c,
442 show_help() unless $rc;
443 show_help() if ($c->{help});
445 # check fingerprints list for validity
446 if ($c->{fingerprints}) {
448 my %valid_fps = ( oclc => 1, isbn => 1, issn => 1, lccn => 1,
449 edition => 1, accomp => 1, authpub => 1,
450 baseline => 1, crap => 1,
452 for (split /,/, $c->{fingerprints}) {
453 die "Invalid fingerprint '$_'\n" unless $valid_fps{$_};
456 $c->{fingerprints} = \%fps
458 $c->{fingerprints} = {oclc => 1, isbn => 1, edition => 1, issn => 1,
459 lccn => 1, accomp => 1, authpub => 1};
461 # check scores list for validity
464 my %valid_scores = ( oclc => 1, dlc => 1, num_650 => 1,
465 num_tags => 1, enc_lvl => 1,
467 for (split /,/, $c->{scores}) {
468 die "Invalid score mode '$_'\n" unless $valid_scores{$_};
471 $c->{dyn_scores} = [split /,/, $c->{scores}];
472 $c->{scores} = \%scores;
474 $c->{scores} = {oclc => 1, dlc => 1, num_650 => 1,
475 num_tags => 1, enc_lvl => 1};
476 $c->{dyn_scores} = [ qw/oclc dlc num_650 num_tags enc_lvl/ ];
479 # set defaults if told to do so
480 if ($c->{incoming}) {
481 $c->{tag} = 903 unless defined $c->{tag};
482 $c->{subfield} = 'a' unless defined $c->{subfield};
483 $c->{marctype} = 'XML' unless defined $c->{marctype};
484 $c->{output} = 'incoming.fp' unless defined $c->{output};
485 $c->{exception} = 'incoming.ex' unless defined $c->{exception};
486 } elsif ($c->{incumbent}) {
487 $c->{tag} = 901 unless defined $c->{tag};
488 $c->{subfield} = 'c' unless defined $c->{subfield};
489 $c->{marctype} = 'XML' unless defined $c->{marctype};
490 $c->{output} = 'incumbent.fp' unless defined $c->{output};
491 $c->{exception} = 'incumbent.ex' unless defined $c->{exception};
494 my @keys = keys %{$c};
495 show_help() unless (@ARGV and @keys);
496 for my $key ('tag', 'subfield', 'output', 'exception')
497 { push @missing, $key unless $c->{$key} }
499 print "Required option: ", join(', ', @missing), " missing!\n";
505 =head2 progress_ticker
509 sub progress_ticker {
510 return if $conf->{quiet};
511 printf("\r> %d recs seen; %d processed", $count, $scount);
512 printf(" (%d/s)", ($count / (time - $start + 1)))
513 if ($count % 500 == 0);
518 Display usage message when things go wrong
524 Usage is: $0 [REQUIRED ARGS] [OPTIONS] <filelist>
526 --tag=N -t Which tag to use
527 --subfield=X -s Which subfield to use
528 --output=<file> -o Output filename
529 --exceptions=<file> -x Exception report filename
531 --incoming '-t 903 -s a -o incoming.fp -x incoming.ex'
532 --incumbent '-t 901 -s c -o incumbent.fp -x incumbent.ex'
534 --fingerprints=LIST Fingerprints to generate, comma separated
535 Default: oclc,isbn,edition,issn,lccn,accomp,authpub
537 --scores=LIST Scores to calculate, comma separated
538 Default: oclc,dlc,num_650,num_tags,enc_level
539 --quiet -q Don't write status messages to STDOUT