new edit check for 2+ chars in subfield code
[migration-tools.git] / compress_fingerprints
index 3d96126..4300429 100755 (executable)
@@ -10,7 +10,8 @@ my $conf  = {}; # configuration hashref
 initialize($conf);
 
 my %fps  = (); # records matching each fingerprint (and the lead)
-my %recs = (); # fingerprints belonging to each record
+my @recs = (); # fingerprints belonging to each record
+my %seen = (); # records we've already seen
 my $lastscore = 0; # previous fingerprint's score
 
 open FP, '<', $ARGV[0] or die "Can't open input file: $!\n";
@@ -41,17 +42,12 @@ sub populate_fingerprint {
     $fp{sha1}    = sha1_base64($stripped);
 
     # make sure file is sorted properly
-    # actually, the input can be sorted *either* way and produce identical
-    # results, but a descending sort produces lower runtime
-    if ($lastscore and $fp{compact} > $lastscore) {
+    if ($lastscore and ($fp{compact} > $lastscore)) {
         print "Input file is sorted improperly or unsorted.\n";
-        die "Sort descending (sort -ru) and rerun this script.\n";
+        die "Sort descending (sort -r) and rerun this script.\n";
     }
     $lastscore = $fp{compact};
 
-    # populate records hash
-    $recs{ $fp{id} }{ $fp{sha1} } = {};
-
     return \%fp;
 }
 
@@ -61,28 +57,19 @@ sub rank_fingerprint {
 
     my $sha1 = $fp->{sha1};
     my $id   = $fp->{id};
-    my $islead = $recs{$id}{lead};
-
-    # only process records which haven't already been set as a sub
-    unless (defined $islead and $islead) {
-        unless ($fps{$sha1}) {
-            # haven't seen this fp before. create a new hashref with the current
-            # record as lead
-            $fps{$sha1} = { lead => { id    => $id,
-                                      score => $fp->{compact} },
-                            recs => [ $id ] };
-            $recs{$id}{lead} = 1;
+
+    # only process records which haven't already been seen
+    unless (defined $seen{$id}) {
+        unless (defined $fps{$sha1}) {
+            # haven't seen this fp before. create a new listref to hold subs
+            # and stow the hash of the fingerprint that we're lead of
+            $fps{$sha1} = [];
+            push @recs, {id => $id, sha1 => $sha1};
         } else {
             # have seen this fp. push record id onto matchlist
-            push @{ $fps{$sha1}{recs} }, $id;
-            if ($fp->{compact} > $fps{$sha1}{lead}{score}) {
-                # and set this record as lead if it scores higher than current lead
-                $recs{ $fps{$sha1}{lead}{id} }{lead} = 0; # unset current
-                $recs{ $id }{lead} = 1;                   # set new as lead
-                $fps{$sha1}{lead}{id}    = $id;
-                $fps{$sha1}{lead}{score} = $fp->{compact};
-            }
+            push @{ $fps{$sha1} }, $id;
         }
+        $seen{$id} = 1;
     }
 }
 
@@ -97,18 +84,9 @@ sub dump_records {
     my %used = ();
     open OUT, '>', $conf->{output}
       or die "Can't open ", $conf->{output}, "$!\n";
-    for my $id (keys %recs) {
-        #next if defined $used{$id};
-        $used{$id} = 1;
-        next unless $recs{$id}{lead};
-        for my $sha1 ( keys %{$recs{$id}} ) {
-            for my $subid ( @{$fps{$sha1}{recs}} ) {
-                next if ($id == $subid);
-                next if defined $used{$subid};
-                $used{$subid} = 1;
-                print OUT "$id\t$subid\n";
-            }
-        }
+    for my $rec (@recs) {
+        print OUT $rec->{id}, "\t$_\n"
+          for ( @{ $fps{ $rec->{sha1} } } );
     }
 }