removed eval from fingerprints... Thanks phasefx!
[migration-tools.git] / fingerprints.pl
1 #!/usr/bin/perl
2 use open ':utf8';
3 use MARC::Batch;
4 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
5 use MARC::Field;
6 use Unicode::Normalize;
7
8 my $count = 0; 
9 my $which = $ARGV[0];
10 my $id_tag = $ARGV[1]; my $id_subfield = $ARGV[2];
11
12 binmode(STDOUT, ':utf8');
13 binmode(STDIN, ':utf8');
14
15 my $M;
16
17 foreach $argnum ( 3 .. $#ARGV ) {
18
19         print STDERR "Processing " . $ARGV[$argnum] . "\n";
20
21     open $M, '<:utf8', $ARGV[$argnum];
22
23         my $batch = MARC::Batch->new('XML',$M);
24         $batch->strict_off();
25         $batch->warnings_off();
26
27     my $record = 1;
28         while ( $record ) {
29             $count++;
30             $record = $batch->next();
31
32             my $id = $record->field($id_tag);
33             if (!$id) {
34                 print STDERR "ERROR: This record is missing a $id_tag field.\n" . $record->as_formatted() . "\n=====\n";
35                 next;
36             }
37             $id = $id->as_string($id_subfield);
38             print STDERR "WARNINGS: Record id " . $id . " : " .  join(":",@warnings) . " : continuing...\n" if ( @warnings );
39
40             my $leader = $record->leader();
41             my $record_type = substr($leader,6,1);
42             my $bib_lvl = substr($leader,7,1);
43
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);
48             my $item_form;
49                 if ( $record_type =~ /[gkroef]/ ) { # MAP, VIS
50                     $item_form = substr($my_008,29,1) if ($my_008);
51                 } else {
52                     $item_form = substr($my_008,23,1) if ($my_008);
53                 }
54
55             my $title = $record->field('245'); 
56                 if ( $title ) { $title = $title->subfield('a'); }
57             
58             my @isbns = ();
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; } } }
63
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'); }
68             my $author;
69                 if ($record->field('100')) { $author = $record->field('100')->subfield('a'); }
70                 if (! $author ) {
71                     if ($record->field('110')) { $author = $record->field('110')->subfield('a'); }
72                 }
73                 if (! $author ) {
74                     if ($record->field('111')) { $author = $record->field('111')->subfield('a'); }
75                 }
76             my $desc = $record->field('300');
77                 if ( $desc ) { $desc = $desc->subfield('a'); }
78             my $pages;
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 );
83                 if ( $pubyear ) { 
84                     if ( $pubyear =~ /(\d\d\d\d)/ ) { $pubyear = $1; } else { $pubyear = ''; }
85                 }
86             my $edition = $record->field('250');
87                 if ( $edition ) { $edition = $edition->subfield('a'); }
88
89             # NORMALIZE
90             if ($record_type == ' ') { $record_type = 'a'; }
91             if ($title) {
92                 $title = NFD($title); $title =~ s/[\x{80}-\x{ffff}]//go;
93                 $title = lc($title);
94                 $title =~ s/\W+$//go;
95             }
96             if ($author) {
97                 $author = NFD($author); $author =~ s/[\x{80}-\x{ffff}]//go;
98                 $author = lc($author);
99                 $author =~ s/\W+$//go;
100                 if ($author =~ /^(\w+)/) {
101                     $author = $1;
102                 }
103             }
104             if ($publisher) {
105                 $publisher = NFD($publisher); $publisher =~ s/[\x{80}-\x{ffff}]//go;
106                 $publisher = lc($publisher);
107                 $publisher =~ s/\W+$//go;
108                 if ($publisher =~ /^(\w+)/) {
109                     $publisher = $1;
110                 }
111             }
112
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) {
116
117                 if ($which eq "primary") {
118                     print STDOUT join("\t",$id,$item_form,$date1,$record_type,$bib_lvl,$title) . "\n"; 
119                 } else {
120                 
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"; 
125                         }
126                     }
127
128                     # case b : edition
129                     if ($edition) {
130                         print STDOUT join("\t",$id,"case b",$item_form,$date1,$record_type,$bib_lvl,$title,$edition) . "\n"; 
131                     }
132
133                     # case c : issn
134                     if ($issn) {
135                         print STDOUT join("\t",$id,"case c",$item_form,$date1,$record_type,$bib_lvl,$title,$issn) . "\n"; 
136                     }
137
138                     # case d : lccn
139                     if ($lccn) {
140                         print STDOUT join("\t",$id,"case d",$item_form,$date1,$record_type,$bib_lvl,$title,$lccn) . "\n"; 
141                     }
142
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"; 
146                     }
147
148                 }
149
150             } else {
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);
157                 print STDERR "\n";
158
159             }
160         }
161     print STDERR "Processed $count records\n";
162 }