Introduction to Sentiment analysis using PHP language

Are you emotionally intelligent? Do you struggle to understand human emotions? Its quite difficult for brands to understand customer views and opinions expressed on social media platforms such as Twitter. Sentiment analysis has simplified the way we understand our customer opinions and we can use this analysis to respond to our customers

Sentiment analysis

It is a statistical method of identifying whether a certain word is positive, negative or neutral. The sentiment may be polarized or valance based. Polarized based is used to categorize whether a piece of text is positive, or negative while in valance based, the intensity of the text is emphasized. For example, good and great are positive sentiments and would be treated equally in Polarized based while great will be treated as more positive in valance based.

VADER Sentiment Analysis

VADER (Valence Aware Dictionary and Sentiment Reasoner) is a sentiment analysis tool based on lexicons of words and not only does it classify a word as either positive or negative but also the intensity of its positivity or negativity.

We will use a PHP package called PHP Sentiment Analyzer.

1. composer require davmixcool/php-sentiment-analyzer
2. require __DIR__ . '/vendor/autoload.php';
3. Use Sentiment\Analyzer;
4. $analyzer = new Analyzer();
5. $result = $analyzer->getSentiment($sentence);
6. $sentence = "John is a great, hardworking and smart kid."; 
7. print_r($result); 


Explanation of the code:

Line 1: Install the package using composer

Line 2: Require the Analyzer class

Line 3 and 4: Initialize the package.


After running the code, you get the following output:

  Array ( [neg] => 0 [neu] => 0.339 [pos] => 0.661 [compound] => 0.8316 )  

The analyzer is rating it as Positive, rating the words great and smart as positive.


Applications of sentiment analysis:

Sentiment analysis has a wide range of applications. Below are some of them:

  • Stock and forex market
  • Politics
  • Clients Feedback reviews and much more.

In conclusion, it’s now possible to get data driven insights from social media and other news platform.




Share on:

Related Posts