#!/usr/bin/perl -w
# sentimentweights: compute sentiment weights based on annotation
# usage: bin/sentimentweights
# 20120312 erikt(at)xs4all.nl

$command = $0;
$command = "bin/grep.count.pl.test";
$infile = "sentiment/logfile.selected"; # suffix will be added later
%nonnegative = ();
%negative = ();
@order = qw(pvv vvd cda pvda sp gl d66 cu pvdd sgp 50plus);

# read the nonnegative counts per party
open(INFILE,"$command < $infile.nonnegative |") or 
   die "$command: cannot read nonnegative weights\n";
while (<INFILE>) {
   $line = $_;
   chomp($line);
   ($_,$_,$count,$party) = split(/\s+/,$line);
   $nonnegative{$party} = $count;
}
close(INFILE);

# read the negative counts per party
open(INFILE,"$command < $infile.negative |") or 
   die "$command: cannot read negative weights\n";
while (<INFILE>) {
   $line = $_;
   chomp($line);
   ($_,$_,$count,$party) = split(/\s+/,$line);
   $negative{$party} = $count;
}
close(INFILE);
 
# compute weights
foreach $party (@order) {
   if (not defined $nonnegative{$party}) { $nonnegative{$party} = 0; }
   if (not defined $negative{$party}) { $negative{$party} = 0; }
   if ($nonnegative{$party}+$negative{$party} == 0) {
      print "1 $party\n";
   } else {
      printf "%0.2f %s %d/(%d+%d)\n",
         $nonnegative{$party}/($nonnegative{$party}+$negative{$party}),$party,
         $nonnegative{$party},$negative{$party},$nonnegative{$party};
   }
}

exit(0);
