6 use Digest::SHA1 qw(sha1_base64);
9 my $conf = {}; # configuration hashref
12 my %fps = (); # records matching each fingerprint (and the lead)
13 my %recs = (); # fingerprints belonging to each record
14 my $lastscore = 0; # previous fingerprint's score
16 open FP, '<', $ARGV[0] or die "Can't open input file: $!\n";
18 print "Loading and ranking fingerprints\n";
20 my @fields = split "\t", $_;
21 my $fp = populate_fingerprint(@fields);
22 rank_fingerprint($fp);
24 print "Writing matchset to disk\n";
29 sub populate_fingerprint {
31 my %fp = (); # zero fingerprint hash each time thru
33 # populate fp hash -- first the simple data
34 $fp{compact} = shift @fields;
35 $fp{json} = shift @fields;
36 $fp{id} = shift @fields;
37 # then smash everything else together, remove non-Roman characters, and
38 # generate a SHA1 hash to represent it
39 my $stripped = join('', @fields);
40 $stripped =~ s/[^A-Za-z0-9]//g;
41 $fp{sha1} = sha1_base64($stripped);
43 # make sure file is sorted properly
44 # actually, the input can be sorted *either* way and produce identical
45 # results, but a descending sort produces lower runtime
46 if ($lastscore and $fp{compact} > $lastscore) {
47 print "Input file is sorted improperly or unsorted.\n";
48 die "Sort descending (sort -ru) and rerun this script.\n";
50 $lastscore = $fp{compact};
52 # populate records hash
53 $recs{ $fp{id} }{ $fp{sha1} } = {};
59 sub rank_fingerprint {
62 my $sha1 = $fp->{sha1};
64 my $islead = $recs{$id}{lead};
66 # only process records which haven't already been set as a sub
67 unless (defined $islead and $islead) {
68 unless ($fps{$sha1}) {
69 # haven't seen this fp before. create a new hashref with the current
71 $fps{$sha1} = { lead => { id => $id,
72 score => $fp->{compact} },
76 # have seen this fp. push record id onto matchlist
77 push @{ $fps{$sha1}{recs} }, $id;
78 if ($fp->{compact} > $fps{$sha1}{lead}{score}) {
79 # and set this record as lead if it scores higher than current lead
80 $recs{ $fps{$sha1}{lead}{id} }{lead} = 0; # unset current
81 $recs{ $id }{lead} = 1; # set new as lead
82 $fps{$sha1}{lead}{id} = $id;
83 $fps{$sha1}{lead}{score} = $fp->{compact};
92 Writes out a 2-column file of lead and subordinate records.
98 open OUT, '>', $conf->{output}
99 or die "Can't open ", $conf->{output}, "$!\n";
100 for my $id (keys %recs) {
101 next if defined $used{$id};
103 next unless $recs{$id}{lead};
104 for my $sha1 ( keys %{$recs{$id}} ) {
105 for my $subid ( @{$fps{$sha1}{recs}} ) {
106 next if ($id == $subid);
107 next if defined $used{$subid};
109 print OUT "$id\t$subid\n";
119 # set mode on existing filehandles
120 binmode(STDIN, ':utf8');
122 my $rc = GetOptions( $c,
126 show_help() unless $rc;
127 show_help() if ($c->{help});
129 my @keys = keys %{$c};
130 show_help() unless (@ARGV and @keys);
131 for my $key ('output')
132 { push @missing, $key unless $c->{$key} }
134 print "Required option: ", join(', ', @missing), " missing!\n";
141 Usage is: compress_fingerprints -o OUTPUTFILE INPUTFILE