silence un-init string warning
[migration-tools.git] / match_fingerprints.pl
1 #!/usr/bin/perl
2
3 my $dataset = $ARGV[0];
4
5 my $match_to = $ARGV[1];
6 my $match_these = $ARGV[2];
7 my $match_to_score = $ARGV[3];
8 my $match_these_score = $ARGV[4];
9
10 print "match_to: $match_to match_these: $match_these\n";
11
12 my %pines;
13 my %incoming;
14 my %match;
15 my %candidate_match;
16 my %score;
17
18 open FILE, $match_to;
19 while (my $line = <FILE>) {
20     chomp $line;
21     my @fields = split(/\t/,$line);
22     my $id = shift @fields;
23     my $fp = join '^', @fields;
24     if (! defined $pines{ $fp }) { $pines{ $fp } = []; }
25     push @{ $pines{ $fp } }, $id;
26 }
27 close FILE;
28
29 open FILE, $match_these;
30 while (my $line = <FILE>) {
31     chomp $line;
32     my @fields = split(/\t/,$line);
33     my $id = shift @fields;
34     my $fp = join '^', @fields;
35     if (! defined $incoming{ $fp }) { $incoming{ $fp } = []; }
36     push @{ $incoming{ $fp } }, $id;
37 }
38 close FILE;
39
40 foreach my $file ( $match_to_score, $match_from_score ) {
41     open FILE, $file;
42     while (my $line = <FILE>) {
43         chomp $line;
44         my @fields = split(/\|/,$line);
45         my $id = shift @fields; $id =~ s/\D//g;
46         my $holdings = shift @fields; $holdings =~ s/\D//g;
47         my $subtitle = shift @fields; $subtitle =~ s/^\s+//; $subtitle =~ s/\s+$//;
48         $score{ $id } = [ $holdings, $subtitle ];
49     }
50     close FILE;
51 }
52
53 open RECORD_IDS, ">match.record_ids";
54 foreach my $fp ( keys %incoming ) {
55
56     if (defined $pines{ $fp }) { # match!
57         foreach my $id ( @{ $incoming{ $fp } } ) {
58             print RECORD_IDS "$id\n";
59             if ( ! defined $candidate_match{ $id } )
60               { $candidate_match{ $id } = []; }
61             push @{ $candidate_match{ $id } }, $fp;
62         }
63     }
64 }
65 close RECORD_IDS;
66
67 foreach my $id ( keys %candidate_match ) {
68     my $subtitle;
69     if (defined $score{ $id })
70       { $subtitle = $score{ $id }[1]; }
71
72     my @fps = @{ $candidate_match{ $id } };
73     my @candidate_pines = ();
74
75     my $subtitle_matched = 0;
76     my $highest_holdings = 0;
77     my $best_pines_id;
78
79     foreach my $fp ( @fps ) {
80         foreach my $pines_id ( @{ $pines{ $fp } } )  {
81             my $pines_subtitle;
82             if (defined $score{ $pines_id })
83               { $pines_subtitle = $score{ $pines_id }[1]; }
84             my $pines_holdings;
85             if (defined $score{ $pines_id })
86               { $pines_holdings = $score{ $pines_id }[0]; }
87             if ($pines_subtitle eq $subtitle) {
88                 if (! $subtitle_matched) {
89                     $subtitle_matched = 1;
90                     $best_pines_id = $pines_id;
91                     $highest_holdings = -1;
92                 }
93             } else {
94                 if ($subtitle_matched) { next; }
95             }
96             if ( $pines_holdings > $highest_holdings ) {
97                 $highest_holdings = $pines_holdings;
98                 $best_pines_id = $pines_id;
99             }
100         }
101     }
102     print RECORD_IDS "$best_pines_id\n";
103     if (! defined $match{ $best_pines_id } )
104       { $match{ $best_pines_id } = [ $best_pines_id ]; }
105     push @{ $match{ $best_pines_id } }, $id;
106 }
107
108
109
110 open GROUPINGS, ">match.groupings";
111 foreach my $k ( keys %match ) {
112     print GROUPINGS join("^",
113                          "checking",
114                          $dataset,
115                          $match{ $k }[0],
116                          join(",",@{ $match{ $k } }),
117                          join(",",@{ $match{ $k } })
118                         ) . "\n";
119
120 }
121 close GROUPINGS;
122
123