-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolis-tally.pl
executable file
·53 lines (53 loc) · 1.62 KB
/
polis-tally.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env perl
use JSON::XS;
use Text::CSV_XS qw( csv );
my $aoa = csv(in => 'comments.csv');
my (@agree, @disagree);
open my $matrix, '<', 'participants-votes.csv' or die $!;
while (<$matrix>) {
chomp;
my ($participant,$group_id,$n_comments,$n_votes,$n_agree,$n_disagree,@x) = split /,/, $_;
for my $idx (0..$#x) {
next unless length $_;
$agree[$idx]++ if $x[$idx] eq 1;
$disagree[$idx]++ if $x[$idx] eq -1;
}
}
my @result;
for my $idx (0..$#agree) {
$agree[$idx] //= 0;
$disagree[$idx] //= 0;
my ($timestamp,$comment_id,$author_id,$agrees,$disagrees,$moderated,$comment_body) = @{
(grep { $_->[1] eq $idx } @$aoa)[0]
};
$comment_body =~ s/\s*$//;
push @result, {
idx => $idx,
n_agree => $agree[$idx],
n_disagree => $disagree[$idx],
comment_body => $comment_body,
percentage => int(($agree[$idx] / ($agree[$idx] + $disagree[$idx])) * 10000) / 100,
} if ($agree[$idx] + $disagree[$idx]) >= 5;
}
@result = sort { $b->{percentage} <=> $a->{percentage} } @result;
my $id = 0;
$_->{id} = ++$id for @result;
my $out = shift;
unless ($out) {
print JSON::XS->new->pretty(1)->canonical(1)->encode(\@result);
exit;
}
{
my $json = $out;
$json =~ s/(?:\.csv|\.json)?$/.json/;
open my $fh, '>:utf8', $json or die $!;
print $fh JSON::XS->new->pretty(1)->canonical(1)->encode(\@result);
close $fh;
}
{
my $csv = $out;
$csv =~ s/(?:\.csv|\.json)?$/.csv/;
my @cols = sort keys %{ $result[0] };
my $aoa = [ \@cols, map { [ @{$_}{@cols}] } @result ];
csv(in => $aoa, out => $csv, encoding => 'utf8');
}