grrrr
[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 my $lastscore = 0; # previous fingerprint's score
15
16 open FP, '<', $ARGV[0] or die "Can't open input file: $!\n";
17
18 print "Loading and ranking fingerprints\n";
19 while (<FP>) {
20     my @fields = split "\t", $_;
21     my $fp = populate_fingerprint(@fields);
22     rank_fingerprint($fp);
23 }
24 print "Writing matchset to disk\n";
25 dump_records();
26
27
28
29 sub populate_fingerprint {
30     my @fields = @_;
31     my %fp = (); # zero fingerprint hash each time thru
32
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);
42
43     # make sure file is sorted properly
44     if ($lastscore and $fp{compact} > $lastscore) {
45         print "Input file is sorted improperly or unsorted.\n";
46         die "Sort descending (sort -ru) and rerun this script.\n";
47     }
48     $lastscore = $fp{compact};
49
50     # populate records hash
51     $recs{ $fp{id} }{ $fp{sha1} } = {};
52
53     return \%fp;
54 }
55
56
57 sub rank_fingerprint {
58     my ($fp) = @_;
59
60     my $sha1 = $fp->{sha1};
61     my $id   = $fp->{id};
62     my $islead = $recs{$id}{lead};
63
64     # only process records which haven't already been set as a sub
65     unless (defined $islead and $islead) {
66         unless ($fps{$sha1}) {
67             # haven't seen this fp before. create a new hashref with the current
68             # record as lead
69             $fps{$sha1} = { lead => { id    => $id,
70                                       score => $fp->{compact} },
71                             recs => [ $id ] };
72             $recs{$id}{lead} = 1;
73         } else {
74             # have seen this fp. push record id onto matchlist
75             push @{ $fps{$sha1}{recs} }, $id;
76             if ($fp->{compact} > $fps{$sha1}{lead}{score}) {
77                 # and set this record as lead if it scores higher than current lead
78                 $recs{ $fps{$sha1}{lead}{id} }{lead} = 0; # unset current
79                 $recs{ $id }{lead} = 1;                   # set new as lead
80                 $fps{$sha1}{lead}{id}    = $id;
81                 $fps{$sha1}{lead}{score} = $fp->{compact};
82             }
83         }
84     }
85 }
86
87
88 =head2 dump_records
89
90 Writes out a 2-column file of lead and subordinate records. If
91 posttest is enabled, a scan is also done to ensure that no recordid
92 appears as both a subordinate and lead.
93
94 =cut
95
96 sub dump_records {
97     my %used = ();
98     open OUT, '>', $conf->{output}
99       or die "Can't open ", $conf->{output}, "$!\n";
100     for my $id (keys %recs) {
101         next unless $recs{$id}{lead};
102         for my $sha1 ( keys %{$recs{$id}} ) {
103             for my $subid ( @{$fps{$sha1}{recs}} ) {
104                 next if ($id == $subid);
105                 next if defined $used{$subid};
106                 $used{$subid} = 1;
107                 print OUT "$id\t$subid\n";
108             }
109         }
110     }
111 }
112
113 sub initialize {
114     my ($c) = @_;
115     my @missing = ();
116
117     # set mode on existing filehandles
118     binmode(STDIN, ':utf8');
119
120     my $rc = GetOptions( $c,
121                          'output|o=s',
122                          'help|h',
123                        );
124     show_help() unless $rc;
125     show_help() if ($c->{help});
126
127     my @keys = keys %{$c};
128     show_help() unless (@ARGV and @keys);
129     for my $key ('output')
130       { push @missing, $key unless $c->{$key} }
131     if (@missing) {
132         print "Required option: ", join(', ', @missing), " missing!\n";
133         show_help();
134     }
135 }
136
137 sub show_help {
138     print <<HELP;
139 Usage is: compress_fingerprints -o OUTPUTFILE INPUTFILE
140 HELP
141 exit;
142 }