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: http://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 http://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 Data Scientist, Software Engineer, Statistics & Machine Learning enthusiast, co-founder of WebSEOAnalytics.com and author of Datumbox Machine Learning Framework. Learn more

Latest Comments
  1. Imen Touati

    Hi Vasilis,
    I’m Phd Student interested in sentiment analysis, i admire the articles you posted about sentiment analysis tools on tweeter and facebook that you established but i want to know if your tools depend on language or no and what are technics you used. Thanks

    • 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. Geekyguy

    I have downloaded your project and tried to use it. I have passed the Datumbox API Key, facebook APP ID and app secret to this below line of index.php file
    $FacebookSentimentAnalysis = new FacebookSentimentAnalysis(“Daumbox API”,”FB App ID”,”FB App Scret”);
    when i run the file it is giving errors.
    Such as :
    > Fatal error: Uncaught exception ‘Exception’ with message ‘Facebook needs the CURL PHP extension.’ in C:\wamp\www\facebook-sentiment-analysis-master\lib\facebook-php-sdk\src\base_facebook.php on line 19
    > Exception: Facebook needs the CURL PHP extension. in C:\wamp\www\facebook-sentiment-analysis-master\lib\facebook-php-sdk\src\base_facebook.php on line 19
    > and some more errors in index.php file with resoect include_once ! how to debug these ?? can you please help ?

    • 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.

  3. Taneem

    Hey,
    Very nice post. I’m looking forward to build a similar tool in java. Is it possible to use your API for Java?

    • 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. :)

  4. nisha

    hi i want to know which machine learning techniques are used in Datumbox api..? and for language detection which model is used?

  5. Tom Smiths

    Hi,

    First of all, congratulations for your work, this is a great system and everyone should thank you for making it open source. I was wondering if you can point me to the direction on how to develop the sentiment analysis through your datumbox java framework, without using the rest api. I would want to develop my thesis with a huge amount of data and a webservice doesn’t seem reliable.

    Thank you for everything!
    Tom Smiths

    • 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

      • Tom Smiths

        I will when I finish it. Thank you again for sharing your knowledge so openly!

  6. Yousef ElSammaa

    Hi,
    Thank you very much for this insightful article. I have a question regarding the Facebook Api. Which kinds of posts will it fetch, will it fetch posts from the users profile or from his feed. If I want (with the permission of the user) to be able to go through all the posts in his feed and possibly of the people in his friend list and use sentiment analysis to looks for certain keywords, will that be possible with the Facebook API?

    • Vasilis Vryniotis

      Hi,

      This is more of a question on how-to use Facebook API rather than Machine Learning. I would advise you to post it on their forums as they are more experienced with their latest updates.


Leave a Reply

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


× five = 15

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>