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

use GD::Graph::bars;
use Lingua::EN::Opinion;

my $input_file = shift || die "Usage: perl $0 /some/file.txt\n";
my $width      = shift || 800;
my $height     = shift || 400;

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

$opinion->nrc_sentiment();

my %index;

for my $score ( @{ $opinion->nrc_scores } ) {
    for my $emotion ( keys %$score ) {
        next if $emotion eq 'positive' || $emotion eq 'negative';
        $index{$emotion} += $score->{$emotion};
    }
}

my @data = (
    [ sort keys %index ],
    [ map { $index{$_} } sort keys %index ],
);

my $graph = GD::Graph::bars->new( $width, $height );

$graph->set(
    title   => 'NRC Sentiment',
    x_label => 'Emotions',
    y_label => 'Value',
) or die "Can't set graph: ", $graph->error;

my $gd = $graph->plot(\@data) or die $graph->error;

print $gd->png;
