7 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
9 use Unicode::Normalize;
14 my $id_subfield = shift;
16 binmode(STDOUT, ':utf8');
17 binmode(STDIN, ':utf8');
19 for my $file (@ARGV) {
21 print STDERR "Processing $file\n";
23 open my $M, '<:utf8', $file;
25 my $batch = MARC::Batch->new('XML',$M);
27 $batch->warnings_off();
29 while ( my $record = $batch->next ) {
32 my $id = $record->field($id_tag);
34 print STDERR "ERROR: This record is missing a $id_tag field.\n"
35 . $record->as_formatted() . "\n=====\n";
38 $id = $id->as_string($id_subfield);
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);
50 if ( $record_type =~ /[gkroef]/ ) { # MAP, VIS
51 $item_form = substr($my_008,29,1) if ($my_008);
53 $item_form = substr($my_008,23,1) if ($my_008);
56 my $title = $record->field('245');
57 if ( $title ) { $title = $title->subfield('a'); }
61 if ($record->field('020')) { @isbns_020 = $record->field('020'); }
62 foreach my $f ( @isbns_020 ) {
63 if ($f->subfield('a')) {
64 if ( $f->subfield('a')=~/(\S+)/ ) {
70 if ($record->field('024')) { @isbns_024 = $record->field('024'); }
71 foreach my $f ( @isbns_024 ) {
72 if ($f->subfield('a')) {
73 if ( $f->subfield('a')=~/(\S+)/ ) {
79 my $issn = $record->field('022');
80 $issn = $issn->subfield('a') if $issn;
82 my $lccn = $record->field('010');
83 $lccn = $lccn->subfield('a') if $lccn;
86 if ($record->field('100'))
87 { $author = $record->field('100')->subfield('a'); }
89 $author = $record->field('110')->subfield('a')
90 if ($record->field('110'));
91 $author = $record->field('111')->subfield('a')
92 if ($record->field('111'));
95 my $desc = $record->field('300');
96 if ( $desc ) { $desc = $desc->subfield('a'); }
99 if (defined $desc and $desc =~ /(\d+)/) { $pages = $1; }
101 my $my_260 = $record->field('260');
102 my $publisher = $my_260->subfield('b') if ( $my_260 );
103 my $pubyear = $my_260->subfield('c') if ( $my_260 );
105 if ( $pubyear =~ /(\d\d\d\d)/ )
111 my $edition = $record->field('250');
112 if ( $edition ) { $edition = $edition->subfield('a'); }
115 $record_type = 'a' if ($record_type eq ' ');
117 $title = NFD($title); $title =~ s/[\x{80}-\x{ffff}]//go;
119 $title =~ s/\W+$//go;
122 $author = NFD($author); $author =~ s/[\x{80}-\x{ffff}]//go;
123 $author = lc($author);
124 $author =~ s/\W+$//go;
125 if ($author =~ /^(\w+)/) {
130 $publisher = NFD($publisher); $publisher =~ s/[\x{80}-\x{ffff}]//go;
131 $publisher = lc($publisher);
132 $publisher =~ s/\W+$//go;
133 if ($publisher =~ /^(\w+)/) {
138 # SPIT OUT FINGERPRINTS FROM THE "LOIS ALGORITHM"
139 # If we're not getting good matches, we may want to change this.
140 # The same thing goes for some other fields.
141 if ($item_form && ($date1 =~ /\d\d\d\d/)
142 && $record_type && $bib_lvl && $title) {
143 if ($runtype eq "primary") {
145 join("\t",$id,$item_form,$date1,$record_type,$bib_lvl,$title)
148 # case a : isbn and pages
149 if (scalar(@isbns)>0 && $pages) {
150 foreach my $isbn ( @isbns ) {
152 join("\t", $id, "case a", $item_form, $date1,
153 $record_type, $bib_lvl, $title, $isbn, $pages)
160 join("\t", $id, "case b", $item_form, $date1,
161 $record_type, $bib_lvl, $title,$edition), "\n";
165 print STDOUT join("\t", $id, "case c", $item_form, $date1,
166 $record_type, $bib_lvl, $title, $issn)
171 print STDOUT join("\t", $id, "case d", $item_form, $date1,
172 $record_type, $bib_lvl, $title, $lccn)
175 # case e : author, publisher, pubyear, pages
176 if ($author && $publisher && $pubyear && $pages) {
177 print STDOUT join("\t", $id, "case e", $item_form, $date1,
178 $record_type, $bib_lvl, $title, $author,
179 $publisher, $pubyear, $pages), "\n";
183 print STDERR "Record " . $id . " did not make the cut: ";
184 print STDERR "Missing item_form. " unless ($item_form);
185 print STDERR "Missing valid date1. " unless ($date1 =~ /\d\d\d\d/);
186 print STDERR "Missing record_type. " unless ($record_type);
187 print STDERR "Missing bib_lvl. " unless ($bib_lvl);
188 print STDERR "Missing title. " unless ($title);
192 print STDERR "Processed $count records\n";