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
15 open FP, '<', $ARGV[0];
17 my @fields = split "\t", $_;
18 my $fp = populate_fingerprint(@fields);
19 rank_fingerprint($fp);
25 sub populate_fingerprint {
30 $fp{compact} = shift @fields;
31 $fp{json} = shift @fields;
32 $fp{id} = shift @fields;
34 my $stripped = join('', @fields);
35 $stripped =~ s/[^A-Za-z0-9]//g;
36 $fp{sha1} = sha1_base64($stripped);
38 # populate records hash
39 $recs{ $fp{id} }{ $fp{sha1} } = {};
45 sub rank_fingerprint {
48 my $sha1 = $fp->{sha1};
50 my $islead = $recs{$id}{lead};
51 unless (defined $islead and $islead != 0) {
52 # only process records which haven't already been set as a sub
53 unless ($fps{$sha1}) {
54 # haven't seen this fp before. create a new hashref with the current
56 $fps{$sha1} = { lead => { id => $id,
57 score => $fp->{compact} },
59 $recs{$id}{$sha1}{lead} = 1;
61 # have seen this fp. push record id onto matchlist
62 push @{ $fps{$sha1}{recs} }, $id;
63 # and set this record as lead if it scores higher than current lead
64 if ($fp->{compact} > $fps{$sha1}{lead}{score}) {
65 # $recs{ $fps{$sha1}{lead}{id} }{$sha1}{lead} = 0;
66 # $recs{ $id }{$sha1}{lead} = 1;
67 $recs{ $fps{$sha1}{lead}{id} }{lead} = 0;
68 $recs{ $id }{lead} = 1;
69 $fps{$sha1}{lead}{id} = $id;
70 $fps{$sha1}{lead}{score} = $fp->{compact};
79 open OUT, '>', $conf->{output}
80 or die "Can't open ", $conf->{output}, "$!\n";
81 for my $id (keys %recs) {
82 next unless $recs{$id}{lead};
83 for my $sha1 ( keys %{$recs{$id}} ) {
84 for my $subid ( @{$fps{$sha1}{recs}} ) {
85 next if ($id == $subid);
86 next if defined $used{$subid};
88 print OUT "$id\t$subid\n";
98 # set mode on existing filehandles
99 binmode(STDIN, ':utf8');
101 my $rc = GetOptions( $c,
105 show_help() unless $rc;
106 show_help() if ($c->{help});
108 my @keys = keys %{$c};
109 show_help() unless (@ARGV and @keys);
110 for my $key ('output')
111 { push @missing, $key unless $c->{$key} }
113 print "Required option: ", join(', ', @missing), " missing!\n";
120 Usage is: compress_fingerprints -o OUTPUTFILE INPUTFILE