4 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
6 use Unicode::Normalize;
10 my $id_tag = $ARGV[1]; my $id_subfield = $ARGV[2];
12 binmode(STDOUT, ':utf8');
13 binmode(STDIN, ':utf8');
17 foreach $argnum ( 3 .. $#ARGV ) {
19 print STDERR "Processing " . $ARGV[$argnum] . "\n";
21 open $M, '<:utf8', $ARGV[$argnum];
23 my $batch = MARC::Batch->new('XML',$M);
25 $batch->warnings_off();
30 $record = $batch->next();
32 my $id = $record->field($id_tag);
34 print STDERR "ERROR: This record is missing a $id_tag field.\n" . $record->as_formatted() . "\n=====\n";
37 $id = $id->as_string($id_subfield);
38 print STDERR "WARNINGS: Record id " . $id . " : " . join(":",@warnings) . " : continuing...\n" if ( @warnings );
40 my $leader = $record->leader();
41 my $record_type = substr($leader,6,1);
42 my $bib_lvl = substr($leader,7,1);
44 my $my_008 = $record->field('008');
45 $my_008 = $my_008->as_string() if ($my_008);
46 my $date1 = substr($my_008,7,4) if ($my_008);
47 my $date2 = substr($my_008,11,4) if ($my_008);
49 if ( $record_type =~ /[gkroef]/ ) { # MAP, VIS
50 $item_form = substr($my_008,29,1) if ($my_008);
52 $item_form = substr($my_008,23,1) if ($my_008);
55 my $title = $record->field('245');
56 if ( $title ) { $title = $title->subfield('a'); }
59 my @isbns_020; if ($record->field('020')) { @isbns_020 = $record->field('020'); }
60 foreach my $f ( @isbns_020 ) { if ($f->subfield('a')) { if ( $f->subfield('a')=~/(\S+)/ ) { push @isbns, $1; } } }
61 my @isbns_024; if ($record->field('024')) { @isbns_024 = $record->field('024'); }
62 foreach my $f ( @isbns_024 ) { if ($f->subfield('a')) { if ( $f->subfield('a')=~/(\S+)/ ) { push @isbns, $1; } } }
64 my $issn = $record->field('022');
65 if ( $issn ) { $issn = $issn->subfield('a'); }
66 my $lccn = $record->field('010');
67 if ( $lccn ) { $lccn = $lccn->subfield('a'); }
69 if ($record->field('100')) { $author = $record->field('100')->subfield('a'); }
71 if ($record->field('110')) { $author = $record->field('110')->subfield('a'); }
74 if ($record->field('111')) { $author = $record->field('111')->subfield('a'); }
76 my $desc = $record->field('300');
77 if ( $desc ) { $desc = $desc->subfield('a'); }
79 if ($desc =~ /(\d+)/) { $pages = $1; }
80 my $my_260 = $record->field('260');
81 my $publisher = $my_260->subfield('b') if ( $my_260 );
82 my $pubyear = $my_260->subfield('c') if ( $my_260 );
84 if ( $pubyear =~ /(\d\d\d\d)/ ) { $pubyear = $1; } else { $pubyear = ''; }
86 my $edition = $record->field('250');
87 if ( $edition ) { $edition = $edition->subfield('a'); }
90 if ($record_type == ' ') { $record_type = 'a'; }
92 $title = NFD($title); $title =~ s/[\x{80}-\x{ffff}]//go;
97 $author = NFD($author); $author =~ s/[\x{80}-\x{ffff}]//go;
98 $author = lc($author);
99 $author =~ s/\W+$//go;
100 if ($author =~ /^(\w+)/) {
105 $publisher = NFD($publisher); $publisher =~ s/[\x{80}-\x{ffff}]//go;
106 $publisher = lc($publisher);
107 $publisher =~ s/\W+$//go;
108 if ($publisher =~ /^(\w+)/) {
113 # SPIT OUT FINGERPRINTS FROM THE "LOIS ALGORITHM"
114 # If we're not getting good matches, we may want to change this. The same thing goes for some other fields.
115 if ($item_form && ($date1 =~ /\d\d\d\d/) && $record_type && $bib_lvl && $title) {
117 if ($which eq "primary") {
118 print STDOUT join("\t",$id,$item_form,$date1,$record_type,$bib_lvl,$title) . "\n";
121 # case a : isbn and pages
122 if (scalar(@isbns)>0 && $pages) {
123 foreach my $isbn ( @isbns ) {
124 print STDOUT join("\t",$id,"case a",$item_form,$date1,$record_type,$bib_lvl,$title,$isbn,$pages) . "\n";
130 print STDOUT join("\t",$id,"case b",$item_form,$date1,$record_type,$bib_lvl,$title,$edition) . "\n";
135 print STDOUT join("\t",$id,"case c",$item_form,$date1,$record_type,$bib_lvl,$title,$issn) . "\n";
140 print STDOUT join("\t",$id,"case d",$item_form,$date1,$record_type,$bib_lvl,$title,$lccn) . "\n";
143 # case e : author, publisher, pubyear, pages
144 if ($author && $publisher && $pubyear && $pages) {
145 print STDOUT join("\t",$id,"case e",$item_form,$date1,$record_type,$bib_lvl,$title,$author,$publisher,$pubyear,$pages) . "\n";
151 print STDERR "Record " . $id . " did not make the cut: ";
152 print STDERR "Missing item_form. " unless ($item_form);
153 print STDERR "Missing valid date1. " unless ($date1 =~ /\d\d\d\d/);
154 print STDERR "Missing record_type. " unless ($record_type);
155 print STDERR "Missing bib_lvl. " unless ($bib_lvl);
156 print STDERR "Missing title. " unless ($title);
161 print STDERR "Processed $count records\n";