#!/usr/bin/env perl
use strict;
use warnings;

open(my $in, "<", "$ARGV[0].csv") or die "can't open $ARGV[0]";
open(OUT, ">","$ARGV[0]_new_rank.csv") or die "can't open $ARGV[0]_rank.csv";
open(WORD, ">", "$ARGV[0].word") or die "can't open word list\n";
open(DATA, ">", "$ARGV[0].in") or die "can't open data file\n";

my @lines = <$in>;
chomp(@lines);
my %similarity_hash;
my $new_thres = 0.60;
my %pair_comment;
my %pair_hash;
#my $support_thres= 50;
#my %support_hash;

line:foreach (@lines){
	my @split = split ',', $_;
	#my $score = $split[2]*$split[3];
	#split[2] is similarity, split[3] is support
#	if ($split[3] > $support_thres) {
#		$support_hash{"$split[0],$split[1]"} = $split[3];	
#		$pair_hash{"$split[0],$split[1]"} = "$split[2],$split[3]";
#	} elsif ( $split[2] > $new_thres) {
#		$similarity_hash{"$split[0],$split[1]"} = $split[2];
#		$pair_hash{"$split[0],$split[1]"} = "$split[2],$split[3]";
#	} else {
#		next line;
#	}
	#my $support = scoreSupport($split[3]);
	#my $score = $split[2]*$support;
	my $score = scoreSupport($split[2], $split[3]);
	$similarity_hash{"$split[0],$split[1]"} = $score;
	$pair_hash{"$split[0],$split[1]"} = "$split[2],$split[3]";
	my $comments = "";	
	for(my $count = 4; $count <@split; $count++){
		$comments = "$comments,$split[$count]";	
	}
	$pair_comment{"$split[0],$split[1]"} = $comments;
}
#my @support_keys = reverse sort { $support_hash{$a} <=> $support_hash{$b} } keys %support_hash;
my @score_keys = reverse sort { $similarity_hash{$a} <=> $similarity_hash{$b} } keys %similarity_hash;
print "bazinga!\n";

#if($num < $limit){
#foreach(@support_keys) {
#		print OUT "$_,$pair_hash{$_},$pair_comment{$_}\n";
#}
foreach(@score_keys){
		print OUT "$_,$similarity_hash{$_},$pair_hash{$_},$pair_comment{$_}\n";
		print WORD "$_,$pair_comment{$_}\n";
		print DATA "$pair_hash{$_}\n";
}
#} else {
#	for(my $count=0;$count<$limit;$count++){
#		print OUT "$sorted_keys[$count]\t$pair_hash{$sorted_keys[$count]}\n";
#	}
#}
close $in;
close OUT;

sub scoreSupport {
	my ($thres,$su) = @_;
#	if ($su < 10 ) {
#		return $thres;
#	}
	#$su = exp($su)/(1 + exp($su));
	my $score = $thres*(log($su)/log(10)+1);
	return exp($score)/(1 + exp($score));

#Old ranking function
#	if ($su <= 10 ) {
#		return $thres;
#	} else {
#		return $thres*log($su)/log(10);
#	}
}
