use strict;
my $verbose = 0;

my $filepath = "h:\\itaproj\\code\\meetme-conf-rec-8000-1234462646.22.txt";
my $csvfileout = "h:\\itaproj\\code\\meetme-conf-rec-8000-1234462646.22_counts.csv";
open F1, $filepath;
open F2, ">$csvfileout";
my %p1words = ();
my %p2words = ();
my %wordscount = ();

while(my $tempText = <F1>)
{
    $tempText =~ m/(p\d*)\s*:\s*(.*)/i;
    my $sentence = $2;
    my $user = $1;
    if($sentence)
    {
        print "Processing sentence : $sentence\n";
        my $tempHash = getWordCountHash($sentence);
        
        foreach my $word (keys %{$tempHash})
        {   
            $wordscount{$word} ++;
            if($user eq "p1")
            {
                $p1words{$word} ++;
            }
            else
            {
                $p2words{$word} ++;
            }
        }
    }
}
print F2 "Word,Total,CountP1,CountP2,Score\n";
print "word\t\tTot\tP1\tP2\tScore\n";
foreach my $word (keys %wordscount)
{
    my $count1 = $p1words{$word};
    $count1 = 0 if(not($p1words{$word}));
    my $count2 = $p2words{$word};
    $count2 = 0 if(not($p2words{$word}));
    my $total = $wordscount{$word};
    my $score = abs(($count1-$count2)/$total);
    
    print F2 "$word,$total,$count1,$count2,$score\n";
}

close(F2);
close(F1);

#Converts the given sentence to a data structure used for internal purposes.
#Input is the sentence.
#Output is a reference to hash containing (word,count).
sub getWordCountHash
{
    print "Inside getWordCountHash ...\n" if($verbose);
    
    my $sentence = shift;
    my @words = split(/[^A-Za-z0-9]/, $sentence);
    my %wchash = ();
    
    foreach my $temp (@words)
    {
        print "Current Term = $temp ...\n" if ($verbose);
        my $key = lc($temp);
        
        if($key eq "")
        {
            next;            
        }
        
        if($wchash{$key})
        {
            $wchash{$key} += 1;
        }
        else
        {
            $wchash{$key} = 1;
        }
    }
    return \%wchash;
}