first go-round
[migration-tools.git] / compress_fingerprints
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use open ':utf8';
5
6 use Digest::SHA1 qw(sha1_base64);
7 use Getopt::Long;
8
9 my $conf  = {}; # configuration hashref
10 initialize($conf);
11
12 my %fps  = (); # records matching each fingerprint (and the lead)
13 my %recs = (); # fingerprints belonging to each record
14
15 open FP, '<', $ARGV[0];
16 while (<FP>) {
17     my @fields = split "\t", $_;
18     my $fp = populate_fingerprint(@fields);
19     rank_fingerprint($fp);
20 }
21 dump_records();
22
23
24
25 sub populate_fingerprint {
26     my @fields = @_;
27     my %fp = ();
28
29     # populate fp hash
30     $fp{compact} = shift @fields;
31     $fp{json}    = shift @fields;
32     $fp{id}      = shift @fields;
33     $fp{sha1}    = sha1_base64( join('', @fields) );
34
35     # populate records hash
36     $recs{ $fp{id} }{ $fp{sha1} } = { exist => 1 };
37
38     return \%fp;
39 }
40
41
42 sub rank_fingerprint {
43     my ($fp) = @_;
44
45     my $sha1 = $fp->{sha1};
46     my $id   = $fp->{id};
47     unless ($fps{$sha1}) {
48         # haven't seen this fp before. create a new hashref with the current
49         # record as lead
50         $fps{$sha1} = { lead => { id    => $id,
51                                   score => $fp->{compact} },
52                         recs => [ $id ] };
53         $recs{$id}{$sha1}{lead} = 1;
54     } else {
55         # have seen this fp. push record id onto matchlist
56         push @{ $fps{$sha1}{recs} }, $id;
57         # and set this record as lead if it scores higher than current lead
58         if ($fp->{compact} > $fps{$sha1}{lead}{score}) {
59             $recs{ $fps{$sha1}{lead}{id} }{$sha1}{lead} = 0;
60             $recs{ $id }{$sha1}{lead} = 1;
61             $fps{$sha1}{lead}{id}    = $id;
62             $fps{$sha1}{lead}{score} = $fp->{compact};
63         }
64     }
65 }
66
67
68 sub dump_fingerprints {
69     open OUT, '>', $conf->{output}
70       or die "Can't open ", $conf->{output}, "$!\n";
71     for my $id (keys %recs) {
72         for my $sha1 ( keys %{$recs{$id}} ) {
73             next unless $recs{$id}{$sha1}{lead};
74             for my $subid ( @{$fps{$sha1}{recs}} )
75               { print OUT "$id\t$subid\n" }
76         }
77     }
78 }
79
80 sub initialize {
81     my ($c) = @_;
82     my @missing = ();
83
84     # set mode on existing filehandles
85     binmode(STDIN, ':utf8');
86
87     my $rc = GetOptions( $c,
88                          'output|o=s',
89                          'help|h',
90                        );
91     show_help() unless $rc;
92     show_help() if ($c->{help});
93
94     my @keys = keys %{$c};
95     show_help() unless (@ARGV and @keys);
96     for my $key ('output')
97       { push @missing, $key unless $c->{$key} }
98     if (@missing) {
99         print "Required option: ", join(', ', @missing), " missing!\n";
100         show_help();
101     }
102 }
103
104 sub show_help {
105     print <<HELP;
106 Usage is: compress_fingerprints -o OUTPUTFILE INPUTFILE
107 HELP
108 exit;
109 }