#!/usr/bin/perl -w
##-------------------------------------------------------------##
# $Author: oravecz $
# $Date: $
# $Header: $
#
# Desc: script to make various evaluation and lists on output of make_associates.pl
#
# Bugs:
# probably a lot
# and a lot more
# Todo:
#  -
# Change log:
#
#_______________________________________________________________##
use strict;
if ($ENV{OSTYPE} =~ m/linux/i) {
require 5.004 or die "We need at least 5.004!";
use POSIX qw(locale_h);
} else {
  die "Alas! We need Linux...";
}
setlocale(LC_CTYPE, "hu_HU");
use locale;

use Getopt::Std;
my $debug = 0;
my %Opt;
my $usage = <<EUS ;

Script to evaluate HU associate list as output of make_associates.pl.

Usage: $0 [-h|d] [associate list file]
 d          print out debugging info
 h          print out usage info
EUS


getopts('hd', \%Opt) or do {print STDERR "$usage"; exit 1};
$debug = 1 if $Opt{'d'} ;
if ($Opt{'h'}) {print STDERR "$usage";exit 1};

$/ = "===============\n";
print STDERR "Processing Descriptors ...\n";

my $count = 0;
my (%nil_one_comp,%nil_two_comp,%one_two_comp);
my $nil_one_gmatch = 0;
my $nil_two_gmatch = 0;
my $one_two_gmatch = 0;

while (<>) {
  $count++;
  my $nil = 0;
  my $one = 0;
  my $two = 0;
  my $nil_one_match = 0;
  my $nil_two_match = 0;
  my $one_two_match = 0;
  my (%nil,%one,%two);

  print STDERR "$count\r";
  my @lines = split(/\n/,$_);
  print shift @lines, "\n";
  foreach my $line (@lines) {
    my ($token,$weight);
    last if $line =~ /===============/;
    if ($line =~ /NO TRANSLATION/) {
      # get English token where there is no trans
      ($token,$weight) = $line =~ /ASSOC\(e=(.*?)\).*\t(\S+)/;
    } else {
      ($token,$weight) = $line =~ /ASSOC.*?:: (.*)\t(\S+)/;
    }
    if ($line =~ /0-ASSOC/) {
      $nil++;
      $nil{$token}{'freq'}++;
      $nil{$token}{'weight'} += $weight;
    } elsif ($line =~ /1-ASSOC/) {
      $one++;
      $one{$token}{'freq'}++;
      $one{$token}{'weight'} += $weight;
    } elsif ($line =~ /2-ASSOC/) {
      $two++;
      $two{$token}{'freq'}++;
      $two{$token}{'weight'} += $weight;
    }
    print "TOKEN: '$token'\n" if $debug;
  }
  # print out required lists
  print_out(\%nil,"0");
  print_out(\%one,"1");
  print_out(\%two,"2");

  # calculate per descriptor stat
  # 0->1 MATCHES:
  foreach my $token (keys %one) {
    if (exists($nil{$token})) {
      $nil_one_match++;
    }
  }
  $nil_one_gmatch += $nil_one_match;
  # 0->2 MATCHES:
  foreach my $token (keys %two) {
    if (exists($nil{$token})) {
      $nil_two_match++;
    }
  }
  $nil_two_gmatch += $nil_two_match;
  # 1->2 MATCHES:
  foreach my $token (keys %two) {
    if (exists($one{$token})) {
      $one_two_match++;
    }
  }
  $one_two_gmatch += $one_two_match;
  
  # print out stats:
  print "STAT=> 0: $nil\t1: $one\t2: $two\n";
  print "STAT=> ";
  # 0->1:
  if ($one == 0 or $nil == 0) {
    print "0->1: 00%";
  } else {
    print "0->1: ($nil_one_match/$one) ", 100 * $nil_one_match / $one, "%";
    # we compared 0 and 1, so we can increment their global counters:
    $nil_one_comp{'nil'} += $nil;
    $nil_one_comp{'one'} += $one;
  }
  # 0->2,1->2:
  if ($two == 0) {
    print " | 0->2: 00%";
    # 1->2:
    print " | 1->2: 00%\n";
  } else {
    if ($nil == 0) {
      print " | 0->2:  00%";
    } else {
      print " | 0->2: ($nil_two_match/$two) ", 100 * $nil_two_match / $two, "%";
      # we compared 0 and 2, so we can increment their global counters:
    $nil_two_comp{'nil'} += $nil;
    $nil_two_comp{'two'} += $two;
    }
    # 1->2:
    if ($one == 0) {
      print " | 1->2: 00%\n";
    } else {
      print " | 1->2: ($one_two_match/$two) ", 100 * $one_two_match / $two, "%\n";
      # we compared 1 and 2, so we can increment their global counters:
    $one_two_comp{'one'} += $one;
    $one_two_comp{'two'} += $two;
    }
  }
  # end of stats
  print "===============\n";
}

# calculate overall stat
print "GSTAT=> 0->1: ($nil_one_gmatch/$nil_one_comp{'one'}) ",
  100 * $nil_one_gmatch / $nil_one_comp{'one'}, "%";
print " | 0->2: ($nil_two_gmatch/$nil_two_comp{'two'}) ",
  100 * $nil_two_gmatch / $nil_two_comp{'two'}, "%";
print " | 1->2: ($one_two_gmatch/$one_two_comp{'two'}) ",
  100 * $one_two_gmatch / $one_two_comp{'two'}, "%\n";

print STDERR "$count descriptors processed.\n";


sub print_out {
  my ($href,$asso) = @_;
  foreach my $token (keys %$href) {
    print "$asso-ASSOC:: $token\t",
      $href->{$token}{'weight'} / $href->{$token}{'freq'},
	"\n";
  }
}
