223bae869b815dda375bc415502941d69b152b5a
[migration-tools.git] / fingerprints.pl
1 #!/usr/bin/perl
2 use MARC::Batch;
3 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
4 use MARC::Field;
5 use Unicode::Normalize;
6
7 my $count = 0; 
8 my $which = $ARGV[0];
9 my $id_tag = $ARGV[1]; my $id_subfield = $ARGV[2];
10
11 binmode(STDOUT, ':utf8');
12 binmode(STDIN, ':utf8');
13
14 foreach $argnum ( 3 .. $#ARGV ) {
15
16         print STDERR "Processing " . $ARGV[$argnum] . "\n";
17
18         my $batch = MARC::Batch->new('XML',$ARGV[$argnum]);
19         $batch->strict_off();
20         $batch->warnings_off();
21
22     my $record = 1;
23         while ( $record ) {
24         eval {
25             $count++;
26             $record = $batch->next();
27
28             my $id = $record->field($id_tag);
29             if (!$id) {
30                 print STDERR "ERROR: This record is missing a $id_tag field.\n" . $record->as_formatted() . "\n=====\n";
31                 next;
32             }
33             $id = $id->as_string($id_subfield);
34             print STDERR "WARNINGS: Record id " . $id . " : " .  join(":",@warnings) . " : continuing...\n" if ( @warnings );
35
36             my $leader = $record->leader();
37             my $record_type = substr($leader,6,1);
38             my $bib_lvl = substr($leader,7,1);
39
40             my $my_008 = $record->field('008');
41                 $my_008 = $my_008->as_string() if ($my_008);
42             my $date1 = substr($my_008,7,4) if ($my_008);
43             my $date2 = substr($my_008,11,4) if ($my_008);
44             my $item_form;
45                 if ( $record_type =~ /[gkroef]/ ) { # MAP, VIS
46                     $item_form = substr($my_008,29,1) if ($my_008);
47                 } else {
48                     $item_form = substr($my_008,23,1) if ($my_008);
49                 }
50
51             my $title = $record->field('245'); 
52                 if ( $title ) { $title = $title->subfield('a'); }
53             
54             my @isbns = ();
55             my @isbns_020; if ($record->field('020')) { @isbns_020 = $record->field('020'); }
56             foreach my $f ( @isbns_020 ) { if ($f->subfield('a')) { if ( $f->subfield('a')=~/(\S+)/ ) { push @isbns, $1; } } }
57             my @isbns_024; if ($record->field('024')) { @isbns_024 = $record->field('024'); }
58             foreach my $f ( @isbns_024 ) { if ($f->subfield('a')) { if ( $f->subfield('a')=~/(\S+)/ ) { push @isbns, $1; } } }
59
60             my $issn = $record->field('022');
61                 if ( $issn ) { $issn = $issn->subfield('a'); }
62             my $lccn = $record->field('010');
63                 if ( $lccn ) { $lccn = $lccn->subfield('a'); }
64             my $author;
65                 if ($record->field('100')) { $author = $record->field('100')->subfield('a'); }
66                 if (! $author ) {
67                     if ($record->field('110')) { $author = $record->field('110')->subfield('a'); }
68                 }
69                 if (! $author ) {
70                     if ($record->field('111')) { $author = $record->field('111')->subfield('a'); }
71                 }
72             my $desc = $record->field('300');
73                 if ( $desc ) { $desc = $desc->subfield('a'); }
74             my $pages;
75                 if ($desc =~ /(\d+)/) { $pages = $1; }
76             my $my_260 = $record->field('260');
77             my $publisher = $my_260->subfield('b') if ( $my_260 );
78             my $pubyear = $my_260->subfield('c') if ( $my_260 );
79                 if ( $pubyear ) { 
80                     if ( $pubyear =~ /(\d\d\d\d)/ ) { $pubyear = $1; } else { $pubyear = ''; }
81                 }
82             my $edition = $record->field('250');
83                 if ( $edition ) { $edition = $edition->subfield('a'); }
84
85             # NORMALIZE
86             if ($record_type == ' ') { $record_type = 'a'; }
87             if ($title) {
88                 $title = NFD($title); $title =~ s/[\x{80}-\x{ffff}]//go;
89                 $title = lc($title);
90                 $title =~ s/\W+$//go;
91             }
92             if ($author) {
93                 $author = NFD($author); $author =~ s/[\x{80}-\x{ffff}]//go;
94                 $author = lc($author);
95                 $author =~ s/\W+$//go;
96                 if ($author =~ /^(\w+)/) {
97                     $author = $1;
98                 }
99             }
100             if ($publisher) {
101                 $publisher = NFD($publisher); $publisher =~ s/[\x{80}-\x{ffff}]//go;
102                 $publisher = lc($publisher);
103                 $publisher =~ s/\W+$//go;
104                 if ($publisher =~ /^(\w+)/) {
105                     $publisher = $1;
106                 }
107             }
108
109             # SPIT OUT FINGERPRINTS FROM THE "LOIS ALGORITHM"
110             # If we're not getting good matches, we may want to change this.  The same thing goes for some other fields.
111             if ($item_form && ($date1 =~ /\d\d\d\d/) && $record_type && $bib_lvl && $title) {
112
113                 if ($which eq "primary") {
114                     print STDOUT join("\t",$id,$item_form,$date1,$record_type,$bib_lvl,$title) . "\n"; 
115                 } else {
116                 
117                     # case a : isbn and pages
118                     if (scalar(@isbns)>0 && $pages) {
119                         foreach my $isbn ( @isbns ) {
120                             print STDOUT join("\t",$id,"case a",$item_form,$date1,$record_type,$bib_lvl,$title,$isbn,$pages) . "\n"; 
121                         }
122                     }
123
124                     # case b : edition
125                     if ($edition) {
126                         print STDOUT join("\t",$id,"case b",$item_form,$date1,$record_type,$bib_lvl,$title,$edition) . "\n"; 
127                     }
128
129                     # case c : issn
130                     if ($issn) {
131                         print STDOUT join("\t",$id,"case c",$item_form,$date1,$record_type,$bib_lvl,$title,$issn) . "\n"; 
132                     }
133
134                     # case d : lccn
135                     if ($lccn) {
136                         print STDOUT join("\t",$id,"case d",$item_form,$date1,$record_type,$bib_lvl,$title,$lccn) . "\n"; 
137                     }
138
139                     # case e : author, publisher, pubyear, pages
140                     if ($author && $publisher && $pubyear && $pages) {
141                         print STDOUT join("\t",$id,"case e",$item_form,$date1,$record_type,$bib_lvl,$title,$author,$publisher,$pubyear,$pages) . "\n"; 
142                     }
143
144                 }
145
146             } else {
147                 print STDERR "Record " . $id . " did not make the cut: ";
148                 print STDERR "Missing item_form. " unless ($item_form);
149                 print STDERR "Missing valid date1. " unless ($date1 =~ /\d\d\d\d/);
150                 print STDERR "Missing record_type. " unless ($record_type);
151                 print STDERR "Missing bib_lvl. " unless ($bib_lvl);
152                 print STDERR "Missing title. " unless ($title);
153                 print STDERR "\n";
154
155             }
156         }
157         print STDERR "Trapped exception for MARC::Batch->next on record $count: $@\n" if ($@);
158         }
159     print STDERR "Processed $count records\n";
160 }