first cut
[csv-join-similarity] / upari.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 # apt install libtext-csv-perl
8 use Text::CSV;
9 use Data::Dump qw(dump);
10
11 my @files = qw( 1.csv 2.csv 3.csv 4.csv );
12
13 my $stat;
14 my $keys;
15
16 foreach my $nr ( 1 .. 4 ) {
17         my $file = "$nr.csv";
18         warn "# $file\n";
19
20         my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 });
21         open my $fh, "<:encoding(utf8)", $file or die "$file: $!";
22         while (my $row = $csv->getline ($fh)) {
23                 $stat->{lines}->{$nr}++;
24                 $stat->{ $file }->{lines}++;
25                 my $id = join('-',
26                         uc $row->[0],
27                         $row->[1],
28                         $row->[2],
29                 );
30
31                 my $num = $row->[0]; $num =~ s/\D//g;
32
33                 $stat->{num}->{len}->{ length($num) }++;
34
35                 my $key_id = $row->[1] . '-' . $row->[2] . 's' . int( $row->[4] );
36
37                 if ( $nr == 1 ) {
38                         push @{ $keys->{ $key_id }->{ $num } }, uc $row->[0];
39                 }
40                 if ( $num && $nr > 1 && ! exists $stat->{_}->{ $id } ) {
41                         $stat->{error}->{new_exact}->{$nr}++;
42                         if ( exists $keys->{ $key_id }->{ $num } ) {
43                                 my @found = @{ $keys->{ $key_id }->{ $num } };
44                                 if ( $#found == 0 ) {
45                                         $id = $found[0];
46                                         print "MAPPED $nr $row->[0] to $id\n";
47                                         $stat->{mapped}->{$nr}++;
48                                 } elsif ( $#found > 0 ) {
49                                         print "ALIAS $nr $num into ",dump( @found ),$/;
50                                         $stat->{alias}->{$nr}++;
51                                 }
52                         } else {
53                                 $stat->{error}->{new_num}->{$nr}++;
54                                 #print "ERROR $id new in $file\n";
55                         }
56                 }
57
58                 $stat->{exists}->{$nr}++ if exists $stat->{_}->{ $id };
59
60                 push @{ $stat->{_}->{ $id } }, $nr;
61         }
62         close $fh;
63
64 }
65
66 print "# total = ",scalar keys %{ $stat->{_} }, $/;
67 foreach my $id ( keys %{ $stat->{_} } ) {
68         $stat->{count}->{ scalar @{ $stat->{_}->{$id} } }++;
69         $stat->{count_total}++;
70 }
71 print "# stat = ",dump( $stat );
72 #print "# keys = ",dump( $keys );
73