PHP Classes
elePHPant
Icontem

PHP Sentiment Analyzer: Determine the type of sentiments in a given text

Recommend this page to a friend!
  Info   View files Example   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum (7)   Blog (2)    
Last Updated Ratings Unique User Downloads Download Rankings
2016-01-04 (4 years ago) RSS 2.0 feedStarStarStarStar 64%Total: 1,198 This week: 4All time: 3,122 This week: 79Up
Version License PHP version Categories
sentiment-analyzer 1.0.5GNU General Publi...5Algorithms, PHP 5, Text processing, A...
Description Author

This class can determine the type of sentiments in a given text.

It can be trained with given text files that express either negative or positive sentiments considering the words of each sentence in the training texts.

Then the class can analyze given text string and determine if it expresses positive, negative or neutral sentiments using a Naive Bayes algorithm approach.

The class returns an array with details that inform the type of sentiment that was detected, as well accuracy factors towards positivity or negativity.

Recommendations
Innovation Award
PHP Programming Innovation award nominee
July 2015
Number 4


Prize: One book of choice by Packt
When a user types a message, for instance in a social network or in an email message, it is useful to determine if the user is expressing a positive or negative sentiment, in case you want to react accordingly.

This class can analyze a text and determine whether the sentiment expressed by the user that wrote it is positive or negative.

This can be useful to process large amounts of text messages in order to give more attention to those that may express negative sentiments in result of problems that need to be solved with greater urgency.

Manuel Lemos
  Performance   Level  
Name: Samuel Adeshina <contact>
Classes: 19 packages by
Country: Nigeria Nigeria
Innovation award
Innovation award
Nominee: 7x

Winner: 1x

 

Details

PHP-Sentiment-Analyzer

ABOUT

> Sentiment Analysis is the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer's attitude towards a particular topic, product, etc., is positive, negative, or neutral.

> This package is an implementation of the Naive Bayes Algorithm To Determine the sentiment of a particular statement, a book review, chat, speech and so on. It marks a sentence as positive, negative or neutral depending on the kind of words that are used, this can help in automatically selecting a review, comment or chat that has the best intentions in any situation.

HOW-TO

We instantiate the SentimentAnalyzerTest class below by passing in the SentimentAnalyzer object (class) found in the file: 'SentimentAnalyzer.class.php'.

This class must be injected as a dependency into the constructor as shown below


$sat = new SentimentAnalyzerTest(new SentimentAnalyzer());

Training The Sentiment Analysis Algorithm with words found in the trainingSet directory

The File 'data.neg' contains a list of sentences that's been marked 'Negative'. We use the words in this file to train the algorithm on how a negative sentence/sentiment might be structured.

Likewise, the file 'data.pos' contains a list of 'Positive' sentences and the words are also used to train the algorithm on how to score a sentence or document as 'Positive'.

The trainAnalyzer method below accepts three parameters:

+ param 1: The Location of the file where the training data are located
+ param 2: Used to describe the 'type' of file [param 1] is; used to indicate
		   whether the supplied file contians positive words or not
+ param 3: Enter a less than or equal to 0 here if you want all lines in the
		   file to be used as a training set. Enter any other number if you want to
		   use exactly those number of lines to train the algorithm

$sat->trainAnalyzer('../trainingSet/data.neg', 'negative', 5000); //training with negative data
$sat->trainAnalyzer('../trainingSet/data.pos', 'positive', 5000); //trainign with positive data

The analyzeSentence method accepts a sentence as parameter and score it as a positive, negative or neutral sentiment. it returns an array that looks like this:

array
(
	'sentiment' => '[the sentiment value returned]',
	'accuracy' => array
					(
						'positivity'=> 'A floating point number showing us the probability of the sentence being positive',
						'negativity' => 'A floating point number showing us the probability of the sentence being negative',
					),
)

An example is shown below:

$sentence1 = 'while the performances are often engaging , this loose collection of largely improvised numbers would probably have worked better as a one-hour tv documentary . '; $sentence2 = 'edited and shot with a syncopated style mimicking the work of his subjects , pray turns the idea of the documentary on its head , making it rousing , invigorating fun lacking any mtv puffery . ';

$sentimentAnalysisOfSentence1 = $sat->analyzeSentence($sentence1);

$resultofAnalyzingSentence1 = $sentimentAnalysisOfSentence1['sentiment'];
$probabilityofSentence1BeingPositive = $sentimentAnalysisOfSentence1['accuracy']['positivity'];
$probabilityofSentence1BeingNegative = $sentimentAnalysisOfSentence1['accuracy']['negativity'];

$sentimentAnalysisOfSentence2 = $sat->analyzeSentence($sentence2);

$resultofAnalyzingSentence2 = $sentimentAnalysisOfSentence2['sentiment'];
$probabilityofSentence2BeingPositive = $sentimentAnalysisOfSentence1['accuracy']['positivity'];
$probabilityofSentence2BeingNegative = $sentimentAnalysisOfSentence1['accuracy']['negativity'];`

The AnalyzeDocument method accepts the path to a text file as parameter. It analyzes the file and scores it as either a positive or a negative sentiment. It also returns an array with the same keys as the analyzeSentence method.

An example is demonstrated below

$documentLocation = '../trainingSet/review.txt';
$sentimentAnalysisOfDocument = $sat->analyzeDocument($documentLocation);
$resultofAnalyzingDocument = $sentimentAnalysisOfDocument['sentiment'];
$probabilityofDocumentBeingPositive = $sentimentAnalysisOfDocument['accuracy']['positivity'];
$probabilityofDocumentBeingNegative = $sentimentAnalysisOfDocument['accuracy']['negativity'];
  Files folder image Files  
File Role Description
Files folder imagesrc (3 files, 1 directory)
Files folder imagetrainingSet (5 files)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Data Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imagetest (2 files)
  Plain text file SentimentAnalyzer.class.php Class Class source
  Plain text file SentimentAnalyzer.php Class Class source
  Accessible without login Plain text file test.php Example Example script

  Files folder image Files  /  src  /  test  
File Role Description
  Accessible without login Plain text file presentation.php Example Example script
  Accessible without login Plain text file style.php Example Example script

  Files folder image Files  /  trainingSet  
File Role Description
  Accessible without login Plain text file data.neg Data Auxiliary data
  Accessible without login Plain text file data.pos Data Auxiliary data
  Accessible without login Plain text file data.README.1.0.txt Data Auxiliary data
  Accessible without login Plain text file delimit.txt Data Documentation
  Accessible without login Plain text file review.txt Data Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:1,198
This week:4
All time:3,122
This week:79Up
User Ratings User Comments (2)
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:-
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:848