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_isvalid($marc))
44 { dump_exception($marc); next; }
45 dump_fingerprints($marc);
48 print "\nProcessed $count records\n" unless $conf->{quiet};
54 Constructs a hash containing the relevant MARC data for a record and
55 returns a reference to it.
60 my ($record, $id) = @_;
61 my %marc = (); $marc{isbns} = [];
64 $marc{id} = $id->as_string($conf->{subfield});
66 # record_type, bib_lvl
67 $marc{record_type} = substr($record->leader, 6, 1);
68 $marc{bib_lvl} = substr($record->leader, 7, 1);
71 my $my_008 = $record->field('008');
72 $my_008 = $my_008->as_string() if ($my_008);
73 $marc{date1} = substr($my_008,7,4) if ($my_008);
74 $marc{date2} = substr($my_008,11,4) if ($my_008); # UNUSED
77 if ( $marc{record_type} =~ /[gkroef]/ ) { # MAP, VIS
78 $marc{item_form} = substr($my_008,29,1) if ($my_008);
80 $marc{item_form} = substr($my_008,23,1) if ($my_008);
84 my @isbns = $record->field('020') if $record->field('020');
85 push @isbns, $record->field('024') if $record->field('024');
86 for my $f ( @isbns ) {
87 push @{ $marc{isbns} }, $1 if ( defined $f->subfield('a') and
88 $f->subfield('a')=~/(\S+)/ );
92 for my $rec_field (100, 110, 111) {
93 if ($record->field($rec_field)) {
94 $marc{author} = $record->field($rec_field)->subfield('a');
99 # issn, lccn, title, desc, pages, pub, pubyear, edition
100 $marc{lccn} = $record->field('010')->subfield('a') if $record->field('010');
101 $marc{issn} = $record->field('022')->subfield('a') if $record->field('022');
102 $marc{desc} = $record->field('300')->subfield('a') if $record->field('300');
103 $marc{pages} = $1 if (defined $marc{desc} and $marc{desc} =~ /(\d+)/);
104 $marc{title} = $record->field('245')->subfield('a')
105 if defined $record->field('245');
106 $marc{edition} = $record->field('250')->subfield('a')
107 if $record->field('250');
108 if ($record->field('260')) {
109 $marc{publisher} = $record->field('260')->subfield('b');
110 $marc{pubyear} = $record->field('260')->subfield('c');
112 (defined $marc{pubyear} and $marc{pubyear} =~ /(\d{4})/) ? $1 : '';
119 =head2 normalize_marc
121 Gently massages your data.
128 $marc->{record_type }= 'a' if ($marc->{record_type} eq ' ');
129 if ($marc->{title}) {
130 $marc->{title} = NFD($marc->{title});
131 $marc->{title} =~ s/[\x{80}-\x{ffff}]//go;
132 $marc->{title} = lc($marc->{title});
133 $marc->{title} =~ s/\W+$//go;
135 if ($marc->{author}) {
136 $marc->{author} = NFD($marc->{author});
137 $marc->{author} =~ s/[\x{80}-\x{ffff}]//go;
138 $marc->{author} = lc($marc->{author});
139 $marc->{author} =~ s/\W+$//go;
140 if ($marc->{author} =~ /^(\w+)/) {
141 $marc->{author} = $1;
144 if ($marc->{publisher}) {
145 $marc->{publisher} = NFD($marc->{publisher});
146 $marc->{publisher} =~ s/[\x{80}-\x{ffff}]//go;
147 $marc->{publisher} = lc($marc->{publisher});
148 $marc->{publisher} =~ s/\W+$//go;
149 if ($marc->{publisher} =~ /^(\w+)/) {
150 $marc->{publisher} = $1;
160 Checks MARC record to see if neccessary fingerprinting data is
167 return 1 if ($marc->{item_form} and ($marc->{date1} =~ /\d{4}/) and
168 $marc->{record_type} and $marc->{bib_lvl} and $marc->{title});
173 =head2 dump_fingerprints
177 sub dump_fingerprints {
180 if ($conf->{runtype} eq "primary") {
181 print OF join("\t",$marc->{id}, $marc->{item_form},
182 $marc->{date1}, $marc->{record_type},
183 $marc->{bib_lvl}, $marc->{title}), "\n";
185 if ((scalar @{ $marc->{isbns} } > 0) && $marc->{pages}) {
186 # case a : isbn and pages
187 foreach my $isbn ( @{ $marc->{isbns}} ) {
188 print OF join("\t", $marc->{id}, "case a",
189 $marc->{item_form}, $marc->{date1},
190 $marc->{record_type},
191 $marc->{bib_lvl}, $marc->{title},
192 $isbn, $marc->{pages}), "\n";
196 if ($marc->{edition}) { # case b : edition
197 print OF join("\t", $marc->{id}, "case b",
198 $marc->{item_form}, $marc->{date1},
199 $marc->{record_type}, $marc->{bib_lvl},
200 $marc->{title}, $marc->{edition}), "\n";
203 if ($marc->{issn}) { # case c : issn
204 print OF join("\t", $marc->{id}, "case c",
205 $marc->{item_form}, $marc->{date1},
206 $marc->{record_type}, $marc->{bib_lvl},
207 $marc->{title}, $marc->{issn}), "\n";
210 if ($marc->{lccn}) { # case d : lccn
211 print OF join("\t", $marc->{id}, "case d",
212 $marc->{item_form}, $marc->{date1},
213 $marc->{record_type}, $marc->{bib_lvl},
214 $marc->{title}, $marc->{lccn}) ,"\n";
217 # case e : author, publisher, pubyear, pages
218 if ($marc->{author} and $marc->{publisher} and $marc->{pubyear}
219 and $marc->{pages}) {
220 print OF join("\t", $marc->{id}, "case e",
221 $marc->{item_form}, $marc->{date1},
222 $marc->{record_type}, $marc->{bib_lvl},
223 $marc->{title}, $marc->{author},
224 $marc->{publisher}, $marc->{pubyear},
225 $marc->{pages}), "\n";
232 =head2 dump_exception
234 Write line of exception report
240 print XF "Record ", $marc->{id}, " did not make the cut: ";
241 print XF "Missing item_form. " unless ($marc->{item_form});
242 print XF "Missing valid date1. "
243 unless (defined $marc->{date1} and $marc->{date1} =~ /\d{4}/);
244 print XF "Missing record_type. " unless ($marc->{record_type});
245 print XF "Missing bib_lvl. " unless ($marc->{bib_lvl});
246 print XF "Missing title. " unless ($marc->{title});
253 Performs boring script initialization. Handles argument parsing,
262 # set mode on existing filehandles
263 binmode(STDIN, ':utf8');
265 my $rc = GetOptions( $c,
274 show_help() unless $rc;
276 my @keys = keys %{$c};
277 show_help() unless (@ARGV and @keys);
278 for my $key ('runtype', 'tag', 'subfield', 'output', 'exception') {
279 push @missing, $key unless $c->{$key}
282 print "Required option: ", join(', ', @missing), " missing!\n";
286 show_help() if ($c->{help});
290 =head2 progress_ticker
294 sub progress_ticker {
295 return if $conf->{quiet};
297 if ($count % 100 == 0) {
299 print " $count \n" unless ($count % 1400);
300 } elsif ($count % 20 == 0) {
307 Display usage message when things go wrong
313 Usage is: fingerprinter [REQUIRED ARGS] [OPTIONS] <filelist>
315 --runtype=(primary|full) -r Do 'primary' or 'full' fingerprinting
316 --tag=N -t Which tag to use
317 --subfield=X -s Which subfield to use
318 --output=<file> -o Output filename
319 --exceptions=<file> -x Exception report filename
321 --quiet -q Don't write status messages to STDOUT