How to build your own Facebook Sentiment Analysis Tool

facebook-sentiment-analysis2In this article we will discuss how you can build easily a simple Facebook Sentiment Analysis tool capable of classifying public posts (both from users and from pages) as positive, negative and neutral. We are going to use Facebook’s Graph API Search and the Datumbox API 1.0v. Similar to the Twitter Sentiment Analysis tool that we built few months back, this implementation is written in PHP nevertheless you can build very easily your own tool in the computer language of your choice.

Update: The Datumbox Machine Learning Framework is now open-source and free to download. If you want to build a Sentiment Analysis classifier without hitting the API limitations, use the com.datumbox.applications.nlp.TextClassifier class.

The complete PHP code of the tool can be found on Github.

How Facebook Sentiment Analysis works?

As we discussed in previous articles, performing Sentiment Analysis requires using advanced Machine Learning and Natural Language Processing techniques. In the previous posts we saw in detail several  Text Classifiers such as the Naive Bayes, the Softmax Regression and the Max Entropy, we discussed the importance of using Feature Selection in text classification problems and finally we saw how one can develop an implementation of the Multinomial Naive Bayes classifier in JAVA.

Performing Sentiment Analysis on Facebook does not differ significantly to what we discussed in the past. In a nutshell, we need to fetch the facebook posts and extract their content and then we tokenize them in order to extract their keyword combinations. Afterwards we perform feature selection to keep only the n-grams that are important for the classification problem and we train our classifier to identify the positive, negative and neutral posts.

The above process is significantly simplified by using the Datumbox’s Machine Learning API. All that one needs to do to perform sentiment analysis on Facebook is call the Graph API search to extract the posts of interest, extract their text and call the Datumbox Sentiment Analysis API to get their classification.

Building the Facebook Sentiment Analysis tool

In order to build the Facebook Sentiment Analysis tool you require two things: To use Facebook API in order to fetch the public posts and to evaluate the polarity of the posts based on their keywords. For the first task we will use the Facebook’s Graph API search and for the second the Datumbox API 1.0v.

We will speed the development of the tool by using 2 classes: The Facebook PHP SDK which will easily allow us to access the Graph search and the Datumbox PHP-API-Client. Once again the most complicated task in the process is creating a Facebook Application which will allow us to fetch the posts from Facebook; the Datumbox integration is a piece of cake.

Creating your own Facebook Application

facebook-sentiment-analysisUnfortunately Facebook made it mandatory to authenticate before accessing their Graph Search API. Thankfully they provide a very easy to use SDK which takes care most of the technical details of the integration. Still before using it you must create by using your Facebook Account a new Facebook application.

The process is simple. Go to Facebook Developers page (you will need to register if you have never written a Facebook Application in the past). Click on Apps on the menu and select “Create New App”.

In the popup window fill in the Display Name of your application, the Namespace, select a Category and click Create App. Once the Application is created go to the main page of your Application and select Dashboard. This is where you will get your AppID and the App Secret values. Copy those values in a safe place since we will need them later.

Next go to the Settings of your application and click “+ App Platform” on the bottom of the page. On the popup up select “Website” and then on the Site URL address put the URL of the location where you will upload your tool (Example: https://localhost/). Click “Save Changes” and you are done!

Get your Datumbox API key

To access the Datumbox API sign up for a free account and visit your API Credentials panel to get your API Key.

Developing the Facebook Sentiment Analysis class

Finally all we need to do is write a simple class that integrates the two APIs. First calls the Facebook Graph Search, authenticates, fetches the posts and then passes them to Datumbox API to retrieve their polarity.

Here is the code of the class along with the necessary comments.

<?php
include_once(dirname(__FILE__).'/DatumboxAPI.php');
include_once(dirname(__FILE__).'/facebook-php-sdk/src/facebook.php');
class FacebookSentimentAnalysis {
    
    protected $datumbox_api_key; //Your Datumbox API Key. Get it from https://www.datumbox.com/apikeys/view/
    
    protected $app_id; //Your Facebook APP Id. Get it from https://developers.facebook.com/ 
    protected $app_secret; //Your Facebook APP Id. Get it from https://developers.facebook.com/
    
    /**
    * The constructor of the class
    * 
    * @param string $datumbox_api_key   Your Datumbox API Key
    * @param string $app_id             Your Facebook App Id
    * @param string $app_secret         Your Facebook App Secret
    * 
    * @return FacebookSentimentAnalysis  
    */
    public function __construct($datumbox_api_key, $app_id, $app_secret){
        $this->datumbox_api_key=$datumbox_api_key;
        
        $this->app_id=$app_id;
        $this->app_secret=$app_secret;
    }
    
    /**
    * This function fetches the fb posts list and evaluates their sentiment
    * 
    * @param array $facebookSearchParams The Facebook Search Parameters that are passed to Facebook API. Read more here https://developers.facebook.com/docs/reference/api/search/
    * 
    * @return array
    */
    public function sentimentAnalysis($facebookSearchParams) {
        $posts=$this->getPosts($facebookSearchParams);
        
        return $this->findSentiment($posts);
    }
    
    /**
    * Calls the Open Graph Search method of the Facebook API for particular Graph API Search Parameters and returns the list of posts that match the search criteria.
    * 
    * @param mixed $facebookSearchParams The Facebook Search Parameters that are passed to Facebook API. Read more here https://developers.facebook.com/docs/reference/api/search/
    * 
    * @return array $posts
    */
    protected function getPosts($facebookSearchParams) {
        //Use the Facebook SDK Client
        $Client = new Facebook(array(
          'appId'  => $this->app_id,
          'secret' => $this->app_secret,
        ));

        // Get User ID
        $user = $Client->getUser();

        //if Use is not set, redirect to login page
        if(!$user) {
            header('Location: '.$Client->getLoginUrl());
            die();
        }
        
        $posts = $Client->api('/search', 'GET', $facebookSearchParams); //call the service and get the list of posts
        
        unset($Client);
        
        return $posts;
    }
    
    /**
    * Finds the Sentiment for a list of Facebook posts.
    * 
    * @param array $posts List of posts coming from Facebook's API
    * 
    * @param array $posts
    */
    protected function findSentiment($posts) {
        $DatumboxAPI = new DatumboxAPI($this->datumbox_api_key); //initialize the DatumboxAPI client
        
        $results=array();
        if(!isset($posts['data'])) {
            return $results;
        }
        
        foreach($posts['data'] as $post) { //foreach of the posts that we received
            $message=isset($post['message'])?$post['message']:'';
            
            if(isset($post['caption'])) {
                $message.=("\n\n".$post['caption']);
            }
            if(isset($post['description'])) {
                $message.=("\n\n".$post['description']);
            }
            if(isset($post['link'])) {
                $message.=("\n\n".$post['link']);
            }
            
            $message=trim($message);
            if($message!='') {
                $sentiment=$DatumboxAPI->SentimentAnalysis(strip_tags($message)); //call Datumbox service to get the sentiment
                
                if($sentiment!=false) { //if the sentiment is not false, the API call was successful.
                    $tmp = explode('_',$post['id']);
                    if(!isset($tmp[1])) {
                        $tmp[1]='';
                    }
                    $results[]=array( //add the post message in the results
                        'id'=>$post['id'],
                        'user'=>$post['from']['name'],
                        'text'=>$message,
                        'url'=>'https://www.facebook.com/'.$tmp[0].'/posts/'.$tmp[1],
                        'sentiment'=>$sentiment,
                    );
                }
            }
        }
        
        unset($posts);
        unset($DatumboxAPI);
        
        return $results;
    }
}

As you can see above on the constructor we pass the keys which are required to access the 2 APIs. On the public method sentimentAnalysis() we initialize the Facebook Client, we authenticate and we retrieve the list of posts. Note that if you have not yet authorized your application or if you are not logged in to Facebook with your account, you will be redirected to Facebook.com to login and authorize the app (it’s your app, no worries about privacy issues). Once the list of posts is retrieved they are passed to Datumbox API to get their polarity.

You are good to go! You are ready to use this class to perform Sentiment Analysis on Facebook. You can download the complete PHP code of the Facebook Sentiment Analysis tool from Github.

Using and Expanding the Implementation

To use the provided tool you need to create the Facebook Application as described above and then configure it by modifying the config.php file. In this file you will need to put the Datumbox API key, the Facebook App Id and Secret that you copied earlier.

Finally in the previous post we have built a standalone Twitter Sentiment Analysis tool. It will not take you more than 10 minutes to merge the 2 implementations and create a single tool which is capable of fetching posts both from Facebook and Twitter and presenting the results in a single report.

If you enjoyed the article please take a minute to share it on Facebook or Twitter! 🙂

About 

My name is Vasilis Vryniotis. I'm a Machine Learning Engineer and a Data Scientist. Learn more

Latest Comments
  1. Vasilis Vryniotis

    Hi @Imen,

    Currently the classifiers at Datumbox are trained only on English datasets; soon there will be support in more languages. Nevertheless note that only statistical Machine Learning techniques are used so the algorithms are not language specific. All the techniques that are used in Datumbox are described on this blog. I will not post you the URLs because literally 90% of the articles here are about text classification (with Sentiment Analysis in mind). Just have a look on the previous posts and if you have questions post your comments.

    Goodluck!

  2. Vasilis Vryniotis

    Hi Taneem,

    Of course it is! All you need to do is generate web requests and parse JSON replies. It’s also within my plans to write a JAVA sample client but have not got the time yet to do this. If you build the tool and you plan to open-source it, send us an email and we will feature it on our blog. 🙂

  3. Vasilis Vryniotis

    @Geekyguy,

    I think the error message is clear. You don’t have the CURL PHP extension installed. Given that this is not a problem on the installation of your side, the best place to seek for help on these matters is either look on Google or post on a forum.

  4. Vasilis Vryniotis

    Hi Tom,

    Thanks for your comment. In this blog you will find lots of articles on the topic of sentiment analysis. All the methods described are supported by the framework. This framework powers up also the API of datumbox so building a good classifier will be straight-forward by using the code.

    If you build something interesting, I would appreciate it if you share it on your blog. 🙂

    Best Regards,
    Vasilis

  5. Swagazette

    Hi ,
    Nice tutorial BTW!
    So we are fetching data from a single page on Facebook by this method ( Say BMW facebook page) ? Or are we fetching data about anyone who posts something with a hash tag (#BMW) in any page on facebook. ?

  6. srivachan m s

    I want a idea to start my sentimental analaysis project with a channel and related program in that channel

    so please gave me some idea for start my implementation

  7. Guy

    Hi,
    Nice post! I’m thinking tweaking around with Datumbox…
    But can the same method be also used to analyze the sentiment of comments?
    Also, the Facebook SDK is continually updating 🙁


Leave a Reply

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

Captcha * Time limit is exhausted. Please reload the CAPTCHA.