use strict;
use warnings;

sub start($) {
  my $name = shift(@_);
  my $out;
  open($out, q{>}, qq{out/$name.csv}) or die($name);
  print($out qq{,pCorrect,freq\n});
  return $out;
}

sub main() {
  my $engCSV = start(q{eng});
  my $porCSV = start(q{por});
  my $engLgCSV = start(q{engLg});
  my $porLgCSV = start(q{porLg});
  while (defined(my $line = <>)) {
    #print($line);
    if ($line =~ m{^"(.)",([^,]+),([^,]+),([^,]+),([^,]+),}) {
      my $letter = $1;
      next if $letter =~ m{[a]};
      my $engCorrect = $2;
      my $porCorrect = $3;
      my $engFreq = $4;
      my $porFreq = $5;
      my $engLgFreq = log(1 + $engFreq);
      print($engCSV qq{$letter,$engCorrect,$engFreq\n});
      print($engLgCSV qq{$letter,$engCorrect,$engLgFreq\n});
      if ($porCorrect !~ m{Err}) {
        my $porLgFreq = log(1 + $porFreq);
        print($porCSV qq{$letter,$porCorrect,$porFreq\n});
        print($porLgCSV qq{$letter,$porCorrect,$porLgFreq\n});
      }
    }
  }
}

main();
