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 $marc{tag008} = $my_008->as_string() if ($my_008);
78 if (defined $marc{tag008}) {
79 unless (length $marc{tag008} == 40) {
80 $marc{tag008} = $marc{tag008} . ('|' x (40 - length($marc{tag008})));
81 print XF ">> Short 008 padded to ",length($marc{tag008})," at rec $count\n";
83 $marc{date1} = substr($marc{tag008},7,4) if ($marc{tag008});
84 $marc{date2} = substr($marc{tag008},11,4) if ($marc{tag008}); # 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;
92 print XF ">> using 260c as date1 at rec $count\n";
97 if ( $marc{record_type} =~ /[gkroef]/ ) { # MAP, VIS
98 $marc{item_form} = substr($marc{tag008},29,1) if ($marc{tag008});
100 $marc{item_form} = substr($marc{tag008},23,1) if ($marc{tag008});
104 my @isbns = $record->field('020') if $record->field('020');
105 push @isbns, $record->field('024') if $record->field('024');
106 for my $f ( @isbns ) {
107 push @{ $marc{isbns} }, $1 if ( defined $f->subfield('a') and
108 $f->subfield('a')=~/(\S+)/ );
112 for my $rec_field (100, 110, 111) {
113 if ($record->field($rec_field)) {
114 $marc{author} = $record->field($rec_field)->subfield('a');
121 push @{ $marc{oclc} }, $record->field('001')->as_string()
122 if ($record->field('001') and $record->field('003') and
123 $record->field('003')->as_string() =~ /OCo{0,1}LC/);
124 for ($record->field('035')) {
125 my $oclc = $_->subfield('a');
126 push @{ $marc{oclc} }, $oclc
127 if (defined $oclc and $oclc =~ /\(OCoLC\)/ and $oclc =~/([0-9]+)/);
130 # "Accompanying material" and check for "copy" (300)
131 if ($record->field('300')) {
132 $marc{accomp} = $record->field('300')->subfield('e');
133 $marc{tag300a} = $record->field('300')->subfield('a');
136 # issn, lccn, title, desc, pages, pub, pubyear, edition
137 $marc{lccn} = $record->field('010')->subfield('a') if $record->field('010');
138 $marc{issn} = $record->field('022')->subfield('a') if $record->field('022');
139 $marc{desc} = $record->field('300')->subfield('a') if $record->field('300');
140 $marc{pages} = $1 if (defined $marc{desc} and $marc{desc} =~ /(\d+)/);
141 $marc{title} = $record->field('245')->subfield('a')
142 if $record->field('245');
143 $marc{edition} = $record->field('250')->subfield('a')
144 if $record->field('250');
145 if ($record->field('260')) {
146 $marc{publisher} = $record->field('260')->subfield('b');
147 $marc{pubyear} = $record->field('260')->subfield('c');
149 (defined $marc{pubyear} and $marc{pubyear} =~ /(\d{4})/) ? $1 : '';
156 =head2 normalize_marc
158 Gently massages your data.
165 $marc->{record_type }= 'a' if ($marc->{record_type} eq ' ');
166 if ($marc->{title}) {
167 $marc->{title} = NFD($marc->{title});
168 $marc->{title} =~ s/[\x{80}-\x{ffff}]//go;
169 $marc->{title} = lc($marc->{title});
170 $marc->{title} =~ s/\W+$//go;
172 if ($marc->{author}) {
173 $marc->{author} = NFD($marc->{author});
174 $marc->{author} =~ s/[\x{80}-\x{ffff}]//go;
175 $marc->{author} = lc($marc->{author});
176 $marc->{author} =~ s/\W+$//go;
177 if ($marc->{author} =~ /^(\w+)/) {
178 $marc->{author} = $1;
181 if ($marc->{publisher}) {
182 $marc->{publisher} = NFD($marc->{publisher});
183 $marc->{publisher} =~ s/[\x{80}-\x{ffff}]//go;
184 $marc->{publisher} = lc($marc->{publisher});
185 $marc->{publisher} =~ s/\W+$//go;
186 if ($marc->{publisher} =~ /^(\w+)/) {
187 $marc->{publisher} = $1;
197 Checks MARC record to see if neccessary fingerprinting data is
204 return 1 if ($marc->{item_form} and ($marc->{date1} =~ /\d{4}/) and
205 $marc->{record_type} and $marc->{bib_lvl} and $marc->{title});
212 Assign a score to the record based on various criteria.
214 Score is constructed by pushing elements onto a list, via a dispatch
215 table. This allows order of fingerprints in the output file to be
221 my ($marc, $record) = @_;
225 #----------------------------------
226 # static criteria scoring
227 #----------------------------------
228 $marc->{misc_score} = 999;
229 $marc->{age_score} = 999999999999;
231 # -1 if 008 has been padded, -2 if it doesn't exist
233 { $marc->{misc_score}-- if ($marc->{tag008} =~ /\|$/) }
235 { $marc->{misc_score} -= 2 }
236 # -1 if date has been pulled from 260
237 $marc->{misc_score}-- if $marc->{fudgedate};
238 # -1 if this is a copy record
239 $marc->{misc_score}--
240 if (defined $marc->{tag300a} and $marc->{tag300a} =~ /copy/i);
242 # subtract record id if we want older records to win
243 #$marc->{age_score} -= $marc->{id} unless ($conf->{newwins});
244 # handle arbitrary adjustments
245 $marc->{age_score} = 1;
246 if ($conf->{'arbitrarily-lose-above'}) {
247 $marc->{age_score} = 0
248 if ($marc->{id} >= $conf->{'arbitrarily-lose-above'});
250 if ($conf->{'arbitrarily-lose-below'}) {
251 $marc->{age_score} = 0
252 if ($marc->{id} <= $conf->{'arbitrarily-lose-below'});
255 #----------------------------------
256 # dynamic calculated scoring
257 #----------------------------------
259 oclc => sub { return $marc->{oclc}[0] ? 1 : 0 },
261 if ($record->field('040') and $record->field('040')->subfield('a'))
262 { return scalar($record->subfield( '040', 'a')) =~ /dlc/io ? 1 : 0 }
266 if ($record->field('650')) {
267 # can't say "scalar $record->field('650')"; MARC::Record
268 # behaves differently in list/scalar contexts
269 my @tags = $record->field('650');
270 return sprintf("%04d", scalar @tags)
271 } else { return '0000' }
273 num_tags=> sub { return sprintf( '%04d', scalar( $record->fields ) ) },
275 my $enc = substr($record->leader, 17, 1) || 'u';
276 my %levels = ( ' ' => 9, 1 => 8, 2 => 7, 3 => 6, 4 => 5, 5 => 4,
277 6 => 3, 7 => 2, 8 => 1, 'u' => 0, 'z' => 0 );
278 return $levels{$enc} || 0;
282 #----------------------------------
283 # assemble and store scores
284 #----------------------------------
285 for ( @{ $conf->{dyn_scores} } ) {
286 push @score, $scores_code{$_}->($marc, $record);
287 $json .= $_ . ':' . $score[-1] . ',';
289 $json .= 'misc:' . $marc->{misc_score} . '}';
291 my $compact = join('', $marc->{age_score}, $marc->{misc_score}, @score);
292 $marc->{score} = "$compact\t$json";
295 =head2 dump_fingerprints
299 sub dump_fingerprints {
302 if ($conf->{fingerprints}{baseline}) {
303 print OF join("\t", $marc->{score}, $marc->{id}, 'baseline',
304 $marc->{item_form}, $marc->{date1}, $marc->{record_type},
305 $marc->{bib_lvl}, $marc->{title}), "\n";
308 if ($conf->{fingerprints}{oclc} and scalar @{$marc->{oclc} }) {
309 for (@{$marc->{oclc} }) {
310 print OF join("\t", $marc->{score}, $marc->{id}, "oclc",
311 $marc->{item_form}, $marc->{date1},
312 $marc->{record_type}, $marc->{bib_lvl},
313 $marc->{title}, $_, "\n");
317 if ($conf->{fingerprints}{isbn}) {
318 if ((scalar @{ $marc->{isbns} } > 0) and $marc->{pages}) {
319 foreach my $isbn ( @{ $marc->{isbns}} ) {
320 print OF join("\t", $marc->{score}, $marc->{id}, "isbn",
321 $marc->{item_form}, $marc->{date1},
322 $marc->{record_type},
323 $marc->{bib_lvl}, $marc->{title},
324 $isbn, $marc->{pages}), "\n";
329 if ($conf->{fingerprints}{edition} and $marc->{edition}) {
330 print OF join("\t", $marc->{score}, $marc->{id}, "edition",
331 $marc->{item_form}, $marc->{date1},
332 $marc->{record_type}, $marc->{bib_lvl},
333 $marc->{title}, $marc->{edition}), "\n";
336 if ($conf->{fingerprints}{issn} and $marc->{issn}) {
337 print OF join("\t", $marc->{score}, $marc->{id}, "issn",
338 $marc->{item_form}, $marc->{date1},
339 $marc->{record_type}, $marc->{bib_lvl},
340 $marc->{title}, $marc->{issn}), "\n";
343 if ($conf->{fingerprints}{lccn} and $marc->{lccn}) {
344 print OF join("\t", $marc->{score}, $marc->{id}, "lccn",
345 $marc->{item_form}, $marc->{date1},
346 $marc->{record_type}, $marc->{bib_lvl},
347 $marc->{title}, $marc->{lccn}) ,"\n";
350 if ($conf->{fingerprints}{accomp} and $marc->{accomp}) {
351 print OF join("\t", $marc->{score}, $marc->{id}, "accomp",
352 $marc->{item_form}, $marc->{date1},
353 $marc->{record_type}, $marc->{bib_lvl},
354 $marc->{title}, $marc->{accomp}) ,"\n";
357 if ($conf->{fingerprints}{authpub} and $marc->{author} and
358 $marc->{publisher} and $marc->{pubyear} and $marc->{pages}) {
359 print OF join("\t", $marc->{score}, $marc->{id}, "authpub",
360 $marc->{item_form}, $marc->{date1},
361 $marc->{record_type}, $marc->{bib_lvl},
362 $marc->{title}, $marc->{author},
363 $marc->{publisher}, $marc->{pubyear},
364 $marc->{pages}), "\n";
370 =head2 dump_exception
372 Write line of exception report
378 unless (defined $marc) {
379 print XF "Undefined record at line $count; likely bad XML\n";
382 print XF "Record ", $marc->{id}, " excluded: ";
383 print XF "missing item_form; " unless ($marc->{item_form});
384 unless (defined $marc->{date1})
385 { print XF "missing date1; " }
387 { print XF "invalid date1: '", $marc->{date1}, "'; "
388 unless ($marc->{date1} =~ /\d{4}/); }
389 print XF "missing record_type; " unless ($marc->{record_type});
390 print XF "missing bib_lvl; " unless ($marc->{bib_lvl});
391 print XF "missing title " unless ($marc->{title});
398 Performs boring script initialization. Handles argument parsing,
407 # set mode on existing filehandles
408 binmode(STDIN, ':utf8');
410 my $rc = GetOptions( $c,
419 'arbitrarily-lose-above=i',
420 'arbitrarily-lose-below=i',
425 show_help() unless $rc;
426 show_help() if ($c->{help});
428 # check fingerprints list for validity
429 if ($c->{fingerprints}) {
431 my %valid_fps = ( oclc => 1, isbn => 1, issn => 1, lccn => 1,
432 edition => 1, accomp => 1, authpub => 1,
433 baseline => 1, crap => 1,
435 for (split /,/, $c->{fingerprints}) {
436 die "Invalid fingerprint '$_'\n" unless $valid_fps{$_};
439 $c->{fingerprints} = \%fps
441 $c->{fingerprints} = {oclc => 1, isbn => 1, edition => 1, issn => 1,
442 lccn => 1, accomp => 1, authpub => 1};
445 # check scores list for validity
448 my %valid_scores = ( oclc => 1, dlc => 1, num_650 => 1,
449 num_tags => 1, enc_lvl => 1,
451 for (split /,/, $c->{scores}) {
452 die "Invalid score mode '$_'\n" unless $valid_scores{$_};
455 $c->{dyn_scores} = [split /,/, $c->{scores}];
456 $c->{scores} = \%scores;
458 $c->{scores} = {oclc => 1, dlc => 1, num_650 => 1,
459 num_tags => 1, enc_lvl => 1};
460 $c->{dyn_scores} = [ qw/oclc dlc num_650 num_tags enc_lvl/ ];
464 $c->{tag} = 903 unless defined $c->{tag};
465 $c->{subfield} = 'a' unless defined $c->{subfield};
466 $c->{marctype} = 'XML' unless defined $c->{marctype};
468 $c->{output} = join('.',$c->{prefix},'fp');
469 $c->{exception} = join('.',$c->{prefix},'fp','ex');
472 my @keys = keys %{$c};
473 show_help() unless (@ARGV and @keys);
474 for my $key ('tag', 'subfield', 'output', 'exception')
475 { push @missing, $key unless $c->{$key} }
477 print "Required option: ", join(', ', @missing), " missing!\n";
483 =head2 progress_ticker
487 sub progress_ticker {
488 return if $conf->{quiet};
489 printf("\r> %d recs seen; %d processed", $count, $scount);
490 printf(" (%d/s)", ($count / (time - $start + 1)))
491 if ($count % 500 == 0);
496 Display usage message when things go wrong
502 Usage is: $0 [REQUIRED ARGS] [OPTIONS] <filelist>
504 --output=<FILE> -o Output filename
505 --exceptions=<FILE> -x Exception report filename
507 --prefix=<PREFIX>> -p Shared prefix for output/exception files. Will
508 produce PREFIX.fp and PREFIX.fp.ex
510 --tag=N -t Which tag to use (default 903)
511 --subfield=X -s Which subfield to use (default 'a')
512 --quiet -q Don't write status messages to STDOUT
514 --fingerprints=LIST Fingerprints to generate, comma separated
515 Default: oclc,isbn,edition,issn,lccn,accomp,authpub
518 --scores=LIST Scores to calculate, comma separated
519 Default: oclc,dlc,num_650,num_tags,enc_level
520 --newwins New record IDs score higher (default is old wins)
521 --arbitrarily-lose-above
522 --arbitrarily-lose-below
523 --arbitrarily-decrease-score-by
524 Modify fingerprint scoring of records whose EG id is above or below a
525 given value, inclusive (so 5 is <= 5 or >= 5) such that they lose.
527 --marctype=TYPE Defaults to 'XML'