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

use Lingua::EN::Opinion;
use List::Util qw( max );

my $input_file = shift || die "Usage: perl $0 /some/file.txt\n";
my $emotion    = shift || 'anger,anticipation,disgust,fear,joy,sadness,surprise,trust';

my @emotions = split /,/, $emotion;

my $opinion = Lingua::EN::Opinion->new( file => $input_file );
$opinion->nrc_sentiment();

my %score;
@score{ @{ $opinion->sentences } } = @{ $opinion->nrc_scores };

my %max;
for my $score ( values %score ) {
    for my $item ( keys %$score ) {
        push @{ $max{$item} }, $score->{$item};
    }
}
for my $item ( keys %max ) {
    $max{$item} = max( @{ $max{$item} } );
}

for my $value ( @emotions ) {
    my $i = 0;
    print "Sentences with the most $value:\n";
    for my $sentence ( @{ $opinion->sentences } ) {
        next if $score{$sentence}->{$value} < $max{$value};
        $i++;
        print "\t$i. $sentence\n\n";
    }
}

printf "known: %d, unknown: %d, known/total: %.2f\n",
    $opinion->familiarity->{known},
    $opinion->familiarity->{unknown},
    $opinion->familiarity->{known} / ( $opinion->familiarity->{known} + $opinion->familiarity->{unknown} );
