8 use Unicode::Normalize;
10 my $conf = {}; # configuration hashref
17 open OF, '>', $conf->{output};
19 open XF, '>', $conf->{exception};
22 for my $file (@ARGV) {
23 print XF "Processing $file\n";
24 open my $records, '<:utf8', $file;
25 my $batch = undef; my $record = undef;
27 $batch = MARC::Batch->new('XML', $records);
29 $batch->warnings_off();
31 while ( eval { $record = $batch->next } ) {
33 $count++; progress_ticker();
34 unless ( defined $record )
35 { dump_exception($marc); next; }
37 my $id = $record->field($conf->{tag});
39 print XF "ERROR: Record $count in $file is missing a ",
40 $conf->{tag}, " field.\n", $record->as_formatted(), "\n=====\n";
44 $marc = populate_marc($record, $id);
45 $marc = normalize_marc($marc);
46 unless (marc_isvalid($marc))
47 { dump_exception($marc); next; }
48 dump_fingerprints($marc);
51 print "\nProcessed $count records\n" unless $conf->{quiet};
57 Constructs a hash containing the relevant MARC data for a record and
58 returns a reference to it.
63 my ($record, $id) = @_;
64 my %marc = (); $marc{isbns} = [];
67 $marc{id} = $id->as_string($conf->{subfield});
69 # record_type, bib_lvl
70 $marc{record_type} = substr($record->leader, 6, 1);
71 $marc{bib_lvl} = substr($record->leader, 7, 1);
74 my $my_008 = $record->field('008');
75 $my_008 = $my_008->as_string() if ($my_008);
76 unless (defined $my_008 and length $my_008 == 40)
77 { print XF ">> Bad 008 length in rec ",$marc{id},"\n"; return \%marc }
78 $marc{date1} = substr($my_008,7,4) if ($my_008);
79 $marc{date2} = substr($my_008,11,4) if ($my_008); # UNUSED
82 if ( $marc{record_type} =~ /[gkroef]/ ) { # MAP, VIS
83 $marc{item_form} = substr($my_008,29,1) if ($my_008);
85 $marc{item_form} = substr($my_008,23,1) if ($my_008);
89 my @isbns = $record->field('020') if $record->field('020');
90 push @isbns, $record->field('024') if $record->field('024');
91 for my $f ( @isbns ) {
92 push @{ $marc{isbns} }, $1 if ( defined $f->subfield('a') and
93 $f->subfield('a')=~/(\S+)/ );
97 for my $rec_field (100, 110, 111) {
98 if ($record->field($rec_field)) {
99 $marc{author} = $record->field($rec_field)->subfield('a');
104 # "Accompanying material" (300e)
105 $marc{accomp} = $record->field('300')->subfield('e')
106 if $record->field('300');
108 # issn, lccn, title, desc, pages, pub, pubyear, edition
109 $marc{lccn} = $record->field('010')->subfield('a') if $record->field('010');
110 $marc{issn} = $record->field('022')->subfield('a') if $record->field('022');
111 $marc{desc} = $record->field('300')->subfield('a') if $record->field('300');
112 $marc{pages} = $1 if (defined $marc{desc} and $marc{desc} =~ /(\d+)/);
113 $marc{title} = $record->field('245')->subfield('a')
114 if $record->field('245');
115 $marc{edition} = $record->field('250')->subfield('a')
116 if $record->field('250');
117 if ($record->field('260')) {
118 $marc{publisher} = $record->field('260')->subfield('b');
119 $marc{pubyear} = $record->field('260')->subfield('c');
121 (defined $marc{pubyear} and $marc{pubyear} =~ /(\d{4})/) ? $1 : '';
128 =head2 normalize_marc
130 Gently massages your data.
137 $marc->{record_type }= 'a' if ($marc->{record_type} eq ' ');
138 if ($marc->{title}) {
139 $marc->{title} = NFD($marc->{title});
140 $marc->{title} =~ s/[\x{80}-\x{ffff}]//go;
141 $marc->{title} = lc($marc->{title});
142 $marc->{title} =~ s/\W+$//go;
144 if ($marc->{author}) {
145 $marc->{author} = NFD($marc->{author});
146 $marc->{author} =~ s/[\x{80}-\x{ffff}]//go;
147 $marc->{author} = lc($marc->{author});
148 $marc->{author} =~ s/\W+$//go;
149 if ($marc->{author} =~ /^(\w+)/) {
150 $marc->{author} = $1;
153 if ($marc->{publisher}) {
154 $marc->{publisher} = NFD($marc->{publisher});
155 $marc->{publisher} =~ s/[\x{80}-\x{ffff}]//go;
156 $marc->{publisher} = lc($marc->{publisher});
157 $marc->{publisher} =~ s/\W+$//go;
158 if ($marc->{publisher} =~ /^(\w+)/) {
159 $marc->{publisher} = $1;
169 Checks MARC record to see if neccessary fingerprinting data is
176 return 1 if ($marc->{item_form} and ($marc->{date1} =~ /\d{4}/) and
177 $marc->{record_type} and $marc->{bib_lvl} and $marc->{title});
182 =head2 dump_fingerprints
186 sub dump_fingerprints {
189 if ($conf->{runtype} eq "primary") {
190 print OF join("\t",$marc->{id}, $marc->{item_form},
191 $marc->{date1}, $marc->{record_type},
192 $marc->{bib_lvl}, $marc->{title}), "\n";
194 if ((scalar @{ $marc->{isbns} } > 0) && $marc->{pages}) {
195 # case a : isbn and pages
196 foreach my $isbn ( @{ $marc->{isbns}} ) {
197 print OF join("\t", $marc->{id}, "case a",
198 $marc->{item_form}, $marc->{date1},
199 $marc->{record_type},
200 $marc->{bib_lvl}, $marc->{title},
201 $isbn, $marc->{pages}), "\n";
205 if ($marc->{edition}) { # case b : edition
206 print OF join("\t", $marc->{id}, "case b",
207 $marc->{item_form}, $marc->{date1},
208 $marc->{record_type}, $marc->{bib_lvl},
209 $marc->{title}, $marc->{edition}), "\n";
212 if ($marc->{issn}) { # case c : issn
213 print OF join("\t", $marc->{id}, "case c",
214 $marc->{item_form}, $marc->{date1},
215 $marc->{record_type}, $marc->{bib_lvl},
216 $marc->{title}, $marc->{issn}), "\n";
219 if ($marc->{lccn}) { # case d : lccn
220 print OF join("\t", $marc->{id}, "case d",
221 $marc->{item_form}, $marc->{date1},
222 $marc->{record_type}, $marc->{bib_lvl},
223 $marc->{title}, $marc->{lccn}) ,"\n";
226 if ($marc->{accomp}) { # case e : accomp
227 print OF join("\t", $marc->{id}, "case d",
228 $marc->{item_form}, $marc->{date1},
229 $marc->{record_type}, $marc->{bib_lvl},
230 $marc->{title}, $marc->{accomp}) ,"\n";
233 # case z : author, publisher, pubyear, pages
234 if ($marc->{author} and $marc->{publisher} and $marc->{pubyear}
235 and $marc->{pages}) {
236 print OF join("\t", $marc->{id}, "case e",
237 $marc->{item_form}, $marc->{date1},
238 $marc->{record_type}, $marc->{bib_lvl},
239 $marc->{title}, $marc->{author},
240 $marc->{publisher}, $marc->{pubyear},
241 $marc->{pages}), "\n";
248 =head2 dump_exception
250 Write line of exception report
256 unless (defined $marc) {
257 print XF "Undefined record at line $count; likely bad XML\n";
260 print XF "Record ", $marc->{id}, " did not make the cut: ";
261 print XF "Missing item_form. " unless ($marc->{item_form});
262 unless (defined $marc->{date1})
263 { print XF "Missing date1. " }
265 { print XF "Invalid date1: ", $marc->{date1}, " "
266 unless ($marc->{date1} =~ /\d{4}/); }
267 print XF "Missing record_type. " unless ($marc->{record_type});
268 print XF "Missing bib_lvl. " unless ($marc->{bib_lvl});
269 print XF "Missing title. " unless ($marc->{title});
276 Performs boring script initialization. Handles argument parsing,
285 # set mode on existing filehandles
286 binmode(STDIN, ':utf8');
288 my $rc = GetOptions( $c,
297 show_help() unless $rc;
299 my @keys = keys %{$c};
300 show_help() unless (@ARGV and @keys);
301 for my $key ('runtype', 'tag', 'subfield', 'output', 'exception')
302 { push @missing, $key unless $c->{$key} }
304 print "Required option: ", join(', ', @missing), " missing!\n";
308 show_help() if ($c->{help});
312 =head2 progress_ticker
316 sub progress_ticker {
317 return if $conf->{quiet};
318 printf("> %d (%d/s)\r", $count, ($count / (time - $start + 1)))
319 if ($count % 100 == 0);
324 Display usage message when things go wrong
330 Usage is: fingerprinter [REQUIRED ARGS] [OPTIONS] <filelist>
332 --runtype=(primary|full) -r Do 'primary' or 'full' fingerprinting
333 --tag=N -t Which tag to use
334 --subfield=X -s Which subfield to use
335 --output=<file> -o Output filename
336 --exceptions=<file> -x Exception report filename
338 --quiet -q Don't write status messages to STDOUT