PHP Classes

Sentiment Analysis in PHP Part 2: Applying the Solution in Practice - PHP Sentiment Analyzer package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Sentiment Analyzer PHP Sentiment Analyzer   Blog PHP Sentiment Analyzer package blog   RSS 1.0 feed RSS 2.0 feed   Blog Sentiment Analysis in...  
  Post a comment Post a comment   See comments See comments (9)   Trackbacks (0)  

Author:

Viewers: 397

Last month viewers: 5

Package: PHP Sentiment Analyzer

In the first part of this article we learned about the theory of analyzing a text to determine the sentiments expressed by the user that wrote it.

Read this article to learn to how to implement a sentiment analysis application in practice based on the PHP Sentiment Analyzer class.




Loaded Article

Contents

Introduction

Training Algorithms to Recognize Sentiments with the insertTestData() Method

Identifying Sentiments with the analyzeSentence() Method

Analyzing Whole Documents using the analyzeDocument() Method

Conclusion


Introduction

The previous article about Sentiment Analysis focused mostly on how it works in theory and the necessary algorithms that can be used to build your own sentiment analyzer.

This article is about how to use a Sentiment Analyzer class that was built precisely to provide a ready to use solution that can be adapted to any type of application that can benefit from a sentiment analyzer component.

It is a relatively simple class that contains just three methods for training the algorithm with a list of negative and positive sentiments, analyzing a sentence, and analyzing a whole document to determine the sentiment of the author.

Training Algorithms to Recognize Sentiments with the insertTestData() Method

The insertTestData method is responsible for training the algorithm. It accepts three distinct parameters.

The first parameter, which must be of a string data type, should be the absolute or relative file path to the training sets. Currently it only accepts training data in a plain text file.

You can choose to supply your own training data or use the one that are been distributed with the class. This training sets can be found in the trainingSet directory. The file with an extension of .neg contains the negative sentiments and the .pos file contains the positive sentiments.

The next parameter should be either “Positive” or “Negative” depending on the type of test data that is being supplied. If you are training the algorithm with data from the .neg file, this parameter should be “Negative” or “Positive” if you are training it from the .pos file.

The last parameter is optional. If you do not want the class to read all lines in the training data file, enter an integer here to specify a a limit number of lines that will be read to train the algorithm.

The method returns a Boolean value indicating whether the algorithm was successfully trained or not.

An example usage of this method would be:

<?php

 if (insertTestData( "testDataLocation/testDataFile", "Negative", 100)
 {
  echo "The class was trained successfully With negative data";
 }
 else
 {
  echo "The training process with negative data was unsuccessful";
 }

?>

Identifying Sentiments with the analyzeSentence() Method

The analyzeSentence() method is responsible for performing sentimental analysis on a sentence. It accepts a single string parameter which should be the sentence to analyze.

It returns an array which contains two indexes. The first index is the result of the analysis, the sentimental score of the supplied sentence. The second index is actually an array itself and it contains percentage scores for both the positivity and negativity of the sentence.

A very simple illustration of how this method could be used is shown below:

<?php

 // include the class, train the algorithm using necessary methods
 // assuming the class object is called: $sentimentAnalyzer, then

 $resultofanalysis = $sentimentAnalyzer -> analyzeSentence( "My name is Samuel Adeshina, I dont know why but I love coding");

 // $resultofanalysis is an array which looks like this:

 // array( "sentiment" => "positive", "accuracy" => array("positivity" => 85.87, "negativity" => 14.10))

?>

Analyzing Whole Documents using the analyzeDocument() Method

This method is completely similar to the analyzeSentence method. Instead of accepting a sentence as parameter, it accepts the absolute or relative path of a document, a plain text file or a rich text file. It also returns an array as result, similar to the one returned by the analyzeSentence method.

Conclusion

The PHP Sentiment Analyzer Class has been rigorously tested and it comes with a very concise and well commented example script that demonstrates how it can be used.

As you may see, it can be used for generic applications that can benefit from understanding the sentiments of the authors of text documents.

If you want to understand the theory behind the algorithm that it implements, you can just go and read the first part of the article. It explains everything you should know about sentiment analysis and why you should consider adding it to your knowledge toolbox.

If you liked this article and want to ask a question about sentiment analysis in PHP, post a comment here.




You need to be a registered user or login to post a comment

1,611,062 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

4. Arabic - Entisar Eljali (2017-11-08 14:19)
use it for Arabic... - 0 replies
Read the whole comment and replies

3. sentiment analyzir - mohsin azam (2015-09-07 04:21)
help for sentiment analyzir... - 2 replies
Read the whole comment and replies

2. How to build by own trainingSet for Spanish language - Javier Ergui Blaskovich (2015-09-03 04:13)
How to build by own trainingSet for Spanish language... - 1 reply
Read the whole comment and replies

1. a question about artificial intelligence - behnamy (2015-09-01 18:03)
a question about artificial intelligence... - 2 replies
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (9)   Trackbacks (0)  
  All package blogs All package blogs   PHP Sentiment Analyzer PHP Sentiment Analyzer   Blog PHP Sentiment Analyzer package blog   RSS 1.0 feed RSS 2.0 feed   Blog Sentiment Analysis in...