The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Lingua::EN::Opinion - Measure the emotional sentiment of text

VERSION

version 0.1203

SYNOPSIS

  use Lingua::EN::Opinion;
  my $opinion = Lingua::EN::Opinion->new( file => '/some/file.txt', stem => 1 );
  $opinion->analyze();
  my $score = $opinion->averaged_score(5);
  my $sentiment = $opinion->get_word('foo');
  $sentiment = $opinion->get_sentence('Mary had a little lamb.');
  # OR
  $opinion = Lingua::EN::Opinion->new( text => 'Mary had a little lamb...' );
  $opinion->nrc_sentiment();
  # And now do something cool with $opinion->nrc_scores...
  $sentiment = $opinion->nrc_get_word('foo');
  $sentiment = $opinion->nrc_get_sentence('Mary had a little lamb.');

DESCRIPTION

A Lingua::EN::Opinion object measures the emotional sentiment of text.

Please see the eg/ and t/ scripts for example usage.

The write-up illustrating results can be found at http://techn.ology.net/book-of-revelation-sentiment-analysis/

ATTRIBUTES

file

The text file to analyze.

text

A text string to analyze instead of a text file.

stem

Boolean flag to indicate that word stemming should take place.

For example, "horses" becomes "horse" and "hooves" becomes "hoof."

This is the proper way to use this module.

stemmer

Require the WordNet::QueryData and WordNet::stem modules to stem each word of the provided file or text.

* These modules must be installed and working to use this feature.

This is a computed result. Providing this in the constructor will be ignored.

sentences

Computed result.

scores

Computed result.

nrc_scores

Computed result.

positive

Computed result.

negative

Computed result.

emotion

Computed result.

METHODS

new()

  $opinion = Lingua::EN::Opinion->new(%arguments);

Create a new Lingua::EN::Opinion object.

analyze()

  $opinion->analyze();

Measure the positive/negative emotional sentiment of text. This method sets the scores and sentences attributes.

averaged_score()

  $averaged = $opinion->averaged_score($bins);

Compute the averaged score given a number of (integer) bins (default: 10).

This reduces the amount of "noise" in the original signal. As such, it loses information detail.

For example, if there are 400 sentences, bins of 10 will result in 40 data points. Each point will be the mean of each successive bin-sized set of points in the analyzed score.

nrc_sentiment()

  $opinion->nrc_sentiment();

Compute the NRC sentiment of the given text.

This is given by a 0/1 list of these 10 emotional elements:

  anger
  anticipation
  disgust
  fear
  joy
  negative
  positive
  sadness
  surprise
  trust

This method sets the nrc_scores and sentences attributes.

get_word()

  $sentiment = $opinion->get_word($word);

Get the positive/negative sentiment for a given word. Return a HashRef of positive/negative keys. If the word does not exist, return undef.

nrc_get_word()

  $sentiment = $opinion->nrc_get_word($word);

Get the NRC emotional sentiment for a given word. Return a HashRef of the NRC emotions. If the word does not exist, return undef.

get_sentence()

  $values = $opinion->get_sentence($sentence);

Return the positive/negative values for the words of the given sentence.

nrc_get_sentence()

  $values = $opinion->nrc_get_sentence($sentence);

Return the NRC emotion values for each word of the given sentence.

SEE ALSO

Moo

File::Slurper

Lingua::EN::Sentence

Statistics::Lite

Try::Tiny

https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html#lexicon

http://saifmohammad.com/WebPages/NRC-Emotion-Lexicon.htm

http://techn.ology.net/book-of-revelation-sentiment-analysis/

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Gene Boggs.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.