Sentiment analysis with a social media text in PHP

Do you understand human emotion? Me neither. But machines can! We’re going to get into something really tricky, called “Sentiment Analysis.” When you think about it — computers learning not just to understand what we say, but what we mean!

To start, what is sentiment analysis?

Sentiment analysis is simply the process of working out (statistically) whether a piece of text is positive, negative or neutral. The majority of sentiment analysis approaches take one of two forms: polarity-based, where pieces of texts are classified as either positive or negative, or valence-based, where the intensity of the sentiment is taken into account. For example, the words ‘good’ and ‘excellent’ would be treated the same in a polarity-based approach, whereas ‘excellent’ would be treated as more positive than ‘good’ in a valence-based approach.

So, a few days back at work, I was fortunate enough to see some excellent presentations by a group of data scientists at Experian regarding the analytics work they do which made me develop the PHP Sentiment Analyzer package that makes use of VADER (Valence Aware Dictionary and sentiment Reasoner).

Let me talk you through how it works and how you can get up and running with it in this post!

How does PHP sentiment analyzer package work?

PHP sentiment analyzer makes use of VADER which belongs to a type of sentiment analysis that is based on lexicons of sentiment-related words. In this approach, each of the words in the lexicon is rated as to whether it is positive or negative, and in many cases, how positive or negative.

When PHP sentiment analyzer analyses a piece of text it checks to see if any of the words in the text are present in the VADER lexicon. For example, the sentence “The food is good and the atmosphere is nice” has two words in the VADER lexicon (good and nice) with ratings of 1.9 and 1.8 respectively.

Now, let’s come to the good part, how can we make use of PHP sentiment analyzer package. Its quite easy and straight forward,

First things first, we include the package via composer:

composer require davmixcool/php-sentiment-analyzer

Usage

We initialize the package.

Use Sentiment\Analyzer;
$analyzer = new Analyzer();

Finally, we’ll use the getSentiment() method to get the sentiment metrics for a piece of text.

$sentence = "I just got a call from David - does he realize it's Sunday?";
$result = $analyzer->getSentiment($sentence);
print_r($result);

##Output

['neg'=> 0, 'neu'=> 1, 'pos'=> 0, 'compound'=> 0]

You can see that PHP sentiment analyzer rates this sentence as neutral. What about if we change the last part of the sentence?

$sentence = "I just got a call from David - i don't like it when he calls on Sundays.";
$result = $analyzer->getSentiment($sentence);
print_r($result);

##Output

['neg'=> 0.116, 'neu'=> 0.884, 'pos'=> 0, 'compound'=> -0.1128]

Now PHP sentiment analyzer is rating it as negative, picking up the “don’t” as useful sentiment-related information.

Applications of Sentiment Analysis

Sentiment analysis has applications across a range of industries — it’s great for anything where you can get unstructured opinion data about a service or product. One application of sentiment analysis is for companies that have Twitter or other social media accounts to receive feedback. Obviously it’s bad business for these companies to leave negative feedback unanswered too long, and sentiment analysis can give them a quick way to find and prioritize these unhappy customers.

Final remarks

I hope this has been a useful introduction to a very powerful and easy to use sentiment analysis package in PHP — as you can see the implementation is very straightforward and it can be applied to quite a wide range of contexts.