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

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

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

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

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

my $size = @{ $opinion->nrc_scores };

my $ymax = 0;

my @data = ( [ 1 .. $size ] );
for my $value ( @emotions ) {
    my @scores = map { $_->{$value} } @{ $opinion->nrc_scores };
    my $max = max(@scores);
    $ymax = $max if $max > $ymax;
    push @data, \@scores;
}

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

$graph->set( 
    x_label       => 'Sentence',
    y_label       => 'Emotion',
    title         => ucfirst($emotion) . ' Over Time',
    transparent   => 0,
    dclrs         => [qw( blue red green orange brown purple black gray )],
    y_max_value   => $ymax,
    x_min_value   => 0,
    x_max_value   => $size,
    x_tick_number => $size / 10,
);

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

print $gd->png;
