8 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
10 use Unicode::Normalize;
12 my $conf = {}; # configuration hashref
18 open OF, '>', $conf->{output};
20 open XF, '>', $conf->{exception};
23 for my $file (@ARGV) {
24 print XF "Processing $file\n";
25 open my $records, '<:utf8', $file;
27 my $batch = MARC::Batch->new('XML', $records);
29 $batch->warnings_off();
31 while ( my $record = $batch->next ) {
32 $count++; progress_ticker();
34 my $id = $record->field($conf->{tag});
36 print XF "ERROR: Record $count in $file is missing a",
37 $conf->{tag}, "field.\n", $record->as_formatted(), "\n=====\n";
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);
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 $marc{date1} = substr($my_008,7,4) if ($my_008);
77 $marc{date2} = substr($my_008,11,4) if ($my_008); # UNUSED
80 if ( $marc{record_type} =~ /[gkroef]/ ) { # MAP, VIS
81 $marc{item_form} = substr($my_008,29,1) if ($my_008);
83 $marc{item_form} = substr($my_008,23,1) if ($my_008);
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+)/ );
95 for my $rec_field (100, 110, 111) {
96 if ($record->field($rec_field)) {
97 $marc{author} = $record->field($rec_field)->subfield('a');
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');
115 (defined $marc{pubyear} and $marc{pubyear} =~ /(\d{4})/) ? $1 : '';
122 =head2 normalize_marc
124 Gently massages your data.
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;
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;
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;
161 =head2 dump_fingerprints
165 sub dump_fingerprints {
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";
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";
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";
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";
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";
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";
220 =head2 dump_exception
222 Write line of exception report
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});
241 Performs boring script initialization. Handles argument parsing,
250 # set mode on existing filehandles
251 binmode(STDIN, ':utf8');
253 my $rc = GetOptions( $c,
262 show_help() unless $rc;
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}
270 print "Required option: ", join(', ', @missing), " missing!\n";
274 show_help() if ($c->{help});
278 =head2 progress_ticker
282 sub progress_ticker {
283 return if $conf->{quiet};
285 if ($count % 100 == 0) {
287 print " $count \n" unless ($count % 1400);
288 } elsif ($count % 20 == 0) {
295 Display usage message when things go wrong
301 Usage is: fingerprinter [REQUIRED ARGS] [OPTIONS] <filelist>
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
309 --quiet -q Don't write status messages to STDOUT