seed kmig-*iconv with mig-*iconv
[migration-tools.git] / fingerprinter
index 55b5f33..8442970 100755 (executable)
@@ -1,4 +1,21 @@
 #!/usr/bin/perl
+
+# Copyright 2009-2012, Equinox Software, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
 use strict;
 use warnings;
 use open ':utf8';
@@ -8,6 +25,7 @@ use MARC::Batch;
 use Unicode::Normalize;
 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
 use Equinox::Migration::SubfieldMapper;
+use Equinox::Migration::Utils qw/normalize_oclc_number/;
 
 my $conf  = {}; # configuration hashref
 my $count = 0; my $scount = 0;
@@ -16,14 +34,13 @@ $| = 1;
 
 initialize($conf);
 
-open OF, '>', $conf->{output};
-open XF, '>', $conf->{exception};
+open OF, '>', $conf->{output} or die "$0: cannot open output file $conf->{output}: $!\n";
+open XF, '>', $conf->{exception} or die "$0: cannot open exception file $conf->{exception}: $!\n";
 
 for my $file (@ARGV) {
     print XF "Processing $file\n";
-    my $batch = undef; my $record = undef;
 
-    $batch = MARC::Batch->new($conf->{marctype}, $file);
+    my $batch = MARC::Batch->new($conf->{marctype}, $file);
     $batch->strict_off();
     $batch->warnings_off();
 
@@ -58,7 +75,7 @@ for my $file (@ARGV) {
 
         # if everything looks good, score it and dump fingerprints
         score_marc($marc, $record);
-        dump_fingerprints($marc);
+        dump_fingerprints_score_id($marc);
         $scount++; progress_ticker();
     }
 }
@@ -85,6 +102,8 @@ sub populate_marc {
 
     # date1, date2
     my $my_008 = $record->field('008');
+    my @my_007 = $record->field('007');
+    my $my_006 = $record->field('006');
     $marc{tag008} = $my_008->as_string() if ($my_008);
     if (defined $marc{tag008}) {
         unless (length $marc{tag008} == 40) {
@@ -106,12 +125,35 @@ sub populate_marc {
             }
         }
     }
+    $marc{tag006} = $my_006->as_string() if ($my_006);
+    $marc{tag007} = \@my_007 if (@my_007);
+    $marc{audioformat}='';
+    $marc{videoformat}='';
+    foreach(@my_007)
+    {
+        if(substr($_->data(),0,1) eq 's' && $marc{audioformat} eq '')
+        {
+            $marc{audioformat} = substr($_->data(),3,1) unless (length $_->data() < 4);
+        }
+        elsif(substr($_->data(),0,1) eq 'v' && $marc{videoformat} eq '')
+        {
+            $marc{videoformat} = substr($_->data(),4,1) unless (length $_->data() < 5);
+        }
+    }
+    #print "$marc{audioformat}\n";
+    #print "$marc{videoformat}\n";
 
     # item_form
+    $marc{item_form}='';
     if ( $marc{record_type} =~ /[gkroef]/ ) { # MAP, VIS
-        $marc{item_form} = substr($marc{tag008},29,1) if ($marc{tag008});
+        $marc{item_form} = substr($marc{tag008},29,1) if ($marc{tag008} && (length $marc{tag008} > 29 ));
     } else {
-        $marc{item_form} = substr($marc{tag008},23,1) if ($marc{tag008});
+        $marc{item_form} = substr($marc{tag008},23,1) if ($marc{tag008} && (length $marc{tag008} > 23 ));
+    }
+    #fall through to 006 if 008 doesn't have info for item form
+    if ($marc{item_form} eq '|')
+    {
+        $marc{item_form} = substr($marc{tag006},6,1) if ($marc{tag006} && (length $marc{tag006} > 6 ));
     }
 
     # isbns
@@ -132,13 +174,19 @@ sub populate_marc {
 
     # oclc
     $marc{oclc} = [];
-    push @{ $marc{oclc} }, $record->field('001')->as_string()
-      if ($record->field('001') and $record->field('003') and
-          $record->field('003')->as_string() =~ /OCo{0,1}LC/);
+    if ($record->field('001') &&
+        $record->field('003') &&
+        $record->field('003')->as_string() =~ /OCo{0,1}LC/ &&
+        defined normalize_oclc_number($record->field('001')->as_string())) {
+        push @{ $marc{oclc} }, normalize_oclc_number($record->field('001')->as_string());
+    }
     for ($record->field('035')) {
         my $oclc = $_->subfield('a');
-        push @{ $marc{oclc} }, $oclc
-          if (defined $oclc and $oclc =~ /\(OCoLC\)/ and $oclc =~/([0-9]+)/);
+        if (defined $oclc &&
+            ($oclc =~ /\(OCoLC\)/ || $oclc =~ /(ocm|ocl7|ocn|on)/) &&
+            defined normalize_oclc_number($oclc)) {
+            push @{ $marc{oclc} }, normalize_oclc_number($oclc);
+        }
     }
 
     if ($record->field('999')) {
@@ -159,6 +207,10 @@ sub populate_marc {
     $marc{pages} = $1 if (defined $marc{desc} and $marc{desc} =~ /(\d+)/);
     $marc{title} = $record->field('245')->subfield('a')
       if $record->field('245');
+    $marc{title} .= ' ' . $record->field('245')->subfield('b')
+      if ($record->field('245') and 
+          $record->field('245')->subfield('b') and 
+          not $conf->{ignoresubtitle});
     $marc{edition} = $record->field('250')->subfield('a')
       if $record->field('250');
     if ($record->field('260')) {
@@ -353,11 +405,11 @@ sub dump_fingerprints {
         }
     }
 
-    if ($conf->{fingerprints}{edition} and $marc->{edition}) {
+    if ($conf->{fingerprints}{edition} and $marc->{edition} and $marc->{author}) {
         print OF join("\t", $marc->{score}, $marc->{id}, "edition",
                       $marc->{item_form}, $marc->{date1},
                       $marc->{record_type}, $marc->{bib_lvl},
-                      $marc->{title}, $marc->{edition}), "\n";
+                      $marc->{title}, $marc->{author}, $marc->{edition}), "\n";
     }
 
     if ($conf->{fingerprints}{issn} and $marc->{issn}) {
@@ -392,6 +444,227 @@ sub dump_fingerprints {
     }
 }
 
+sub dump_fingerprints_score_id {
+    my ($marc) = @_;
+
+    if ($conf->{fingerprints}{baseline}) {
+        print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, 'baseline',
+                      $marc->{item_form}, $marc->{date1}, $marc->{record_type},
+                      $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat}, $marc->{title}), "\n";
+    }
+
+    if ($conf->{fingerprints}{oclc} and scalar @{$marc->{oclc} }) {
+        for (@{$marc->{oclc} }) {
+            print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "oclc",
+                          $marc->{item_form}, $marc->{date1},
+                          $marc->{record_type}, $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat},
+                          $marc->{title}, $_, "\n");
+        }
+    }
+
+    if ($conf->{fingerprints}{koha_bib_id} and exists $marc->{koha_bib_id}) {
+        print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "z_koha_bib_id",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type},
+                      $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat}, $marc->{title},
+                      $marc->{koha_bib_id}), "\n";
+    }
+
+    if ($conf->{fingerprints}{isbn}) {
+        if ((scalar @{ $marc->{isbns} } > 0) and $marc->{pages}) {
+            foreach my $isbn ( @{ $marc->{isbns}} ) {
+                print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "isbn",
+                              $marc->{item_form}, $marc->{date1},
+                              $marc->{record_type},
+                              $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat}, $marc->{title},
+                              $isbn, $marc->{pages}), "\n";
+            }
+        }
+    }
+
+    if ($conf->{fingerprints}{edition} and $marc->{edition} and $marc->{author}) {
+        print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "edition",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat},
+                      $marc->{title}, $marc->{author}, $marc->{edition}), "\n";
+    }
+
+    if ($conf->{fingerprints}{issn} and $marc->{issn}) {
+        print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "issn",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat},
+                      $marc->{title}, $marc->{issn}), "\n";
+    }
+
+    if ($conf->{fingerprints}{lccn} and $marc->{lccn}) {
+        print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "lccn",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat},
+                      $marc->{title}, $marc->{lccn}) ,"\n";
+    }
+
+    if ($conf->{fingerprints}{accomp} and $marc->{accomp}) {
+        print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "accomp",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat},
+                      $marc->{title}, $marc->{accomp}) ,"\n";
+    }
+
+    if ($conf->{fingerprints}{authpub} and $marc->{author} and
+        $marc->{publisher} and $marc->{pubyear} and $marc->{pages}) {
+        print OF join("\t", sortvalfromid($marc->{id}),"json", $marc->{id}, "authpub",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},$marc->{audioformat},$marc->{videoformat},
+                      $marc->{title}, $marc->{author},
+                      $marc->{publisher}, $marc->{pubyear},
+                      $marc->{pages}), "\n";
+    }
+}
+
+sub sortvalfromid
+{
+    my $sortval = shift;
+    while(length($sortval)<17)
+    {
+        $sortval = '0'.$sortval;
+    }
+    return $sortval;
+}
+
+sub dump_fingerprints_hash_score {
+    my ($marc) = @_;
+
+    if ($conf->{fingerprints}{baseline}) {
+        my $string = join("", 'baseline',
+                      $marc->{item_form}, $marc->{date1}, $marc->{record_type},
+                      $marc->{bib_lvl}, $marc->{title});
+        $string =~ s/[^A-Za-z0-9]//g;
+        $string = sha1_base64($string);
+        print OF join("\t", $string,"json", $marc->{id}, 'baseline',
+                      $marc->{item_form}, $marc->{date1}, $marc->{record_type},
+                      $marc->{bib_lvl}, $marc->{title}), "\n";
+    }
+
+    if ($conf->{fingerprints}{oclc} and scalar @{$marc->{oclc} }) {
+        for (@{$marc->{oclc} }) {
+            my $string = join("", "oclc",
+                          $marc->{item_form}, $marc->{date1},
+                          $marc->{record_type}, $marc->{bib_lvl},
+                          $marc->{title}, $_);
+            $string =~ s/[^A-Za-z0-9]//g;
+            $string = sha1_base64($string);
+            print OF join("\t", $string,"json", $marc->{id}, "oclc",
+                          $marc->{item_form}, $marc->{date1},
+                          $marc->{record_type}, $marc->{bib_lvl},
+                          $marc->{title}, $_, "\n");
+        }
+    }
+
+    if ($conf->{fingerprints}{koha_bib_id} and exists $marc->{koha_bib_id}) {
+        my $string = join("", "z_koha_bib_id",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type},
+                      $marc->{bib_lvl}, $marc->{title},
+                      $marc->{koha_bib_id});
+        $string =~ s/[^A-Za-z0-9]//g;
+        $string = sha1_base64($string);
+        print OF join("\t", $string,"json", $marc->{id}, "z_koha_bib_id",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type},
+                      $marc->{bib_lvl}, $marc->{title},
+                      $marc->{koha_bib_id}), "\n";
+    }
+
+    if ($conf->{fingerprints}{isbn}) {
+        if ((scalar @{ $marc->{isbns} } > 0) and $marc->{pages}) {
+            foreach my $isbn ( @{ $marc->{isbns}} ) {
+                my $string = join("", "isbn",
+                              $marc->{item_form}, $marc->{date1},
+                              $marc->{record_type},
+                              $marc->{bib_lvl}, $marc->{title},
+                              $isbn, $marc->{pages});
+                $string =~ s/[^A-Za-z0-9]//g;
+                $string = sha1_base64($string);
+                print OF join("\t", $string,"json", $marc->{id}, "isbn",
+                              $marc->{item_form}, $marc->{date1},
+                              $marc->{record_type},
+                              $marc->{bib_lvl}, $marc->{title},
+                              $isbn, $marc->{pages}), "\n";
+            }
+        }
+    }
+
+    if ($conf->{fingerprints}{edition} and $marc->{edition} and $marc->{author}) {
+        my $string = join("", "edition",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{author}, $marc->{edition});
+        $string =~ s/[^A-Za-z0-9]//g;
+        $string = sha1_base64($string);
+        print OF join("\t", $string,"json", $marc->{id}, "edition",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{author}, $marc->{edition}), "\n";
+    }
+
+    if ($conf->{fingerprints}{issn} and $marc->{issn}) {
+        my $string = join("", "issn",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{issn});
+        $string =~ s/[^A-Za-z0-9]//g;
+        $string = sha1_base64($string);
+        print OF join("\t", $string,"json", $marc->{id}, "issn",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{issn}), "\n";
+    }
+
+    if ($conf->{fingerprints}{lccn} and $marc->{lccn}) {
+        my $string = join("", "lccn",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{lccn});
+        $string =~ s/[^A-Za-z0-9]//g;
+        $string = sha1_base64($string);
+        print OF join("\t", $string,"json", $marc->{id}, "lccn",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{lccn}) ,"\n";
+    }
+
+    if ($conf->{fingerprints}{accomp} and $marc->{accomp}) {
+        my $string = join("", "accomp",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{accomp});
+        $string =~ s/[^A-Za-z0-9]//g;
+        $string = sha1_base64($string);
+        print OF join("\t", $string,"json", $marc->{id}, "accomp",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{accomp}) ,"\n";
+    }
+
+    if ($conf->{fingerprints}{authpub} and $marc->{author} and
+        $marc->{publisher} and $marc->{pubyear} and $marc->{pages}) {
+        my $string = join("", "authpub",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{author},
+                      $marc->{publisher}, $marc->{pubyear},
+                      $marc->{pages});
+        $string =~ s/[^A-Za-z0-9]//g;
+        $string = sha1_base64($string);
+        print OF join("\t", $string,"json", $marc->{id}, "authpub",
+                      $marc->{item_form}, $marc->{date1},
+                      $marc->{record_type}, $marc->{bib_lvl},
+                      $marc->{title}, $marc->{author},
+                      $marc->{publisher}, $marc->{pubyear},
+                      $marc->{pages}), "\n";
+    }
+}
+
 
 
 =head2 dump_exception
@@ -489,6 +762,7 @@ sub initialize {
                          'arbitrarily-lose-below=i',
                          'newwins',
                          'excludelist=s',
+                         'ignoresubtitle|i',
                          'quiet|q',
                          'help|h',
                        );
@@ -584,9 +858,10 @@ Req'd Arguments
   --prefix=<PREFIX>>   -p  Shared prefix for output/exception files. Will
                            produce PREFIX.fp and PREFIX.fp.ex
 Options
-  --tag=N       -t  Which tag to use (default 903)
-  --subfield=X  -s  Which subfield to use (default 'a')
-  --quiet       -q  Don't write status messages to STDOUT
+  --tag=N           -t  Which tag to use (default 903)
+  --subfield=X      -s  Which subfield to use (default 'a')
+  --quiet           -q  Don't write status messages to STDOUT
+  --ignoresubtitle  -i  Ignore 245\$b and construct the title from 245\$a alone.
 
   --fingerprints=LIST  Fingerprints to generate, comma separated
                        Default: oclc,isbn,edition,issn,lccn,accomp,authpub