June 30, 2016 1:34 am

An introduction to sentiment analysis with phpInsight – Creating a Sentiment Analysis app

We will be building a simple app which takes from the user the id/name of a Facebook page (the name written in the URL when visiting a Facebook page) and shows some statistics related to the sentiment of the last 100 posts of that Facebook page. It shows whether the page is positive, negative or neutral, how many of the last one hundred posts are considered positive, negative or neutral and it pinpoint the total distribution of positivity, negativity and neutrality in those posts.

An introduction to Facebook’s PHP SDK- Creating a Sentiment Analysis app

[wpdm_file id=175]

In the first part, we saw how we can get the last 100 posts of any Facebook page using Facebook’s PHP SDK. Now, we are going to analyze the sentiment of those posts.

We continue with the Analysis.php file and the pageAnalysis class.

We create a getSentiment method which first instantiates the phpInsight library.

See Also: An introduction to Facebook’s PHP SDK- Creating a Sentiment Analysis app

private function getSentiment() {

        $sentiment = new \PHPInsight\Sentiment();

Then, we iterate over each post of the page that we have collected and save its score and category in an array so that we gather the totality of the sentiments. The score method of phpInsight gives us numeric scores of the different sentiments (positive, negative and neutral) of the input text while the categorise method pinpoints the dominant sentiment of the input text (either positive, negative or neutral).

foreach ($this->posts->data as $post) {

if (isset($post->message)) {

$this->sentiments[] = $sentiment->score($post->message);

$this->dominants[] = $sentiment->categorise($post->message);



Then, we aggregate and count the total number of positive, negative and neutral posts using PHP’s built-in function array_count_values. If there is nothing there we return an error to show to the user.

$this->dominantSentiments = array_count_values($this->dominants);

if (!$this->dominantSentiments || !count($this->dominantSentiments)) {

return $this->error = "No posts/sentiments found!";

//TODO: log errors


Afterwards, we save the dominant sentiment of all posts in a class property. We fetch the dominant sentiment across all posts (whether they are mostly positive, negative or neutral) by searching for the key that is repeated the most. We counted the repetitions with array_count_values and saved the results in dominantSentiments, and now we are just getting the key in dominantSentiments that has the highest value where the value associated with the key is the number of repetitions

$this->dominantSentiment = array_search( max($this->dominantSentiments), $this->dominantSentiments);

Then, we loop over the sentiment scores and gather up an array that would contain the total distribution of positive, negativity and neutrality within the sentiment scores of all texts. We also store the number of sentiments to indicate what the maximum distribution of a sentiment could be.

$this->maximumTotalSentiment = count($this->sentiments);

for ($i = 0; $i < count($this->sentiments);$i++) {

$positivity += isset($this->sentiments[$i]['pos']) ?  $this->sentiments[$i]['pos'] : 0;

$negativity += isset($this->sentiments[$i]['neg']) ?  $this->sentiments[$i]['neg'] : 0 ;

$neutrality += isset($this->sentiments[$i]['neu']) ?  $this->sentiments[$i]['neu'] : 0;


$this->totalSentiments = array("pos" => $positivity, "neg" => $negativity, "neu" => $neutrality);

As the properties that we create are public – we would be able to fetch the resulting values from outside of the class which satisfies our illustrative purposes. In the next piece, we are going to display all those results using Twig templates.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Tutorial Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *