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

use GD::Graph::Cartesian;
use Lingua::EN::Opinion;
use Math::CMA qw( central_moving_averages );

my $input_file = shift || die "Usage: perl $0 /some/file.txt\n";
my $cma        = shift || 0;  # To use central_moving_averages
my $param      = shift || 1;  # distance for cma, bins for averaged_score
my $width      = shift || 800;
my $height     = shift || 400;
my $margin     = 20;

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

$opinion->analyze();

my $averaged = $cma ? [ central_moving_averages( $param, $opinion->scores ) ]
                    : $opinion->averaged_score($param);

my $obj = GD::Graph::Cartesian->new(
    height   => $height,
    width    => $width,
    borderx  => $margin,
    bordery  => $margin,
    iconsize => 4,
);

my $counter = 0;

my ( $last_x, $last_y );

for my $score ( @$averaged ) {
    $counter++;

    if ( defined $last_x && defined $last_y ) {
        $obj->addLine( $last_x, $last_y, $counter, $score )
    }

    ( $last_x, $last_y ) = ( $counter, $score );

    $obj->addPoint( $counter, $score );
}

my ( $x0, $x1, $y0, $y1 ) = ( $obj->_minmaxx, $obj->_minmaxy );
$obj->addRectangle( $x0, $y0, $x1, $y1 );

print $obj->draw;
