Thursday, January 15, 2026

Find out how to construct your individual Fb Sentiment Evaluation Software


On this article we’ll focus on how one can construct simply a easy Fb Sentiment Evaluation device able to classifying public posts (each from customers and from pages) as optimistic, destructive and impartial. We’re going to use Fb’s Graph API Search and the Datumbox API 1.0v. Just like the Twitter Sentiment Evaluation device that we constructed few months again, this implementation is written in PHP however you may construct very simply your individual device within the pc language of your selection.

Replace: The Datumbox Machine Studying Framework is now open-source and free to obtain. If you wish to construct a Sentiment Evaluation classifier with out hitting the API limitations, use the com.datumbox.purposes.nlp.TextClassifier class.

The entire PHP code of the device will be discovered on Github.

How Fb Sentiment Evaluation works?

As we mentioned in earlier articles, performing Sentiment Evaluation requires utilizing superior Machine Studying and Pure Language Processing methods. Within the earlier posts we noticed intimately a number of  Textual content Classifiers such because the Naive Bayes, the Softmax Regression and the Max Entropy, we mentioned the significance of utilizing Characteristic Choice in textual content classification issues and eventually we noticed how one can develop an implementation of the Multinomial Naive Bayes classifier in JAVA.

Performing Sentiment Evaluation on Fb doesn’t differ considerably to what we mentioned prior to now. In a nutshell, we have to fetch the fb posts and extract their content material after which we tokenize them with a purpose to extract their key phrase mixtures. Afterwards we carry out function choice to maintain solely the n-grams which might be vital for the classification drawback and we prepare our classifier to establish the optimistic, destructive and impartial posts.

The above course of is considerably simplified by utilizing the Datumbox’s Machine Studying API. All that one must do to carry out sentiment evaluation on Fb is name the Graph API search to extract the posts of curiosity, extract their textual content and name the Datumbox Sentiment Evaluation API to get their classification.

Constructing the Fb Sentiment Evaluation device

With the intention to construct the Fb Sentiment Evaluation device you require two issues: To make use of Fb API with a purpose to fetch the general public posts and to guage the polarity of the posts based mostly on their key phrases. For the primary process we’ll use the Fb’s Graph API search and for the second the Datumbox API 1.0v.

We’ll velocity the event of the device by utilizing 2 courses: The Fb PHP SDK which is able to simply permit us to entry the Graph search and the Datumbox PHP-API-Consumer. As soon as once more probably the most difficult process within the course of is making a Fb Utility which is able to permit us to fetch the posts from Fb; the Datumbox integration is a chunk of cake.

Creating your individual Fb Utility

facebook-sentiment-analysisSadly Fb made it obligatory to authenticate earlier than accessing their Graph Search API. Fortunately they supply an easy to make use of SDK which takes care a lot of the technical particulars of the combination. Nonetheless earlier than utilizing it you have to create by utilizing your Fb Account a brand new Fb software.

The method is easy. Go to Fb Builders web page (you will have to register you probably have by no means written a Fb Utility prior to now). Click on on Apps on the menu and choose “Create New App”.

Within the popup window fill within the Show Identify of your software, the Namespace, choose a Class and click on Create App. As soon as the Utility is created go to the primary web page of your Utility and choose Dashboard. That is the place you’ll get your AppID and the App Secret values. Copy these values in a secure place since we’ll want them later.

Subsequent go to the Settings of your software and click on “+ App Platform” on the underside of the web page. On the popup up choose “Web site” after which on the Website URL handle put the URL of the placement the place you’ll add your device (Instance: https://localhost/). Click on “Save Adjustments” and you might be achieved!

Get your Datumbox API key

To entry the Datumbox API join for a free account and go to your API Credentials panel to get your API Key.

Creating the Fb Sentiment Evaluation class

Lastly all we have to do is write a easy class that integrates the 2 APIs. First calls the Fb Graph Search, authenticates, fetches the posts after which passes them to Datumbox API to retrieve their polarity.

Right here is the code of the category together with the required feedback.


datumbox_api_key=$datumbox_api_key;
        
        $this->app_id=$app_id;
        $this->app_secret=$app_secret;
    }
    
    /**
    * This operate fetches the fb posts listing and evaluates their sentiment
    * 
    * @param array $facebookSearchParams The Fb Search Parameters which might be handed to Fb API. Learn extra right here https://builders.fb.com/docs/reference/api/search/
    * 
    * @return array
    */
    public operate sentimentAnalysis($facebookSearchParams) {
        $posts=$this->getPosts($facebookSearchParams);
        
        return $this->findSentiment($posts);
    }
    
    /**
    * Calls the Open Graph Search methodology of the Fb API for explicit Graph API Search Parameters and returns the listing of posts that match the search standards.
    * 
    * @param blended $facebookSearchParams The Fb Search Parameters which might be handed to Fb API. Learn extra right here https://builders.fb.com/docs/reference/api/search/
    * 
    * @return array $posts
    */
    protected operate getPosts($facebookSearchParams) {
        //Use the Fb SDK Consumer
        $Consumer = new Fb(array(
          'appId'  => $this->app_id,
          'secret' => $this->app_secret,
        ));

        // Get Consumer ID
        $person = $Consumer->getUser();

        //if Use isn't set, redirect to login web page
        if(!$person) {
            header('Location: '.$Consumer->getLoginUrl());
            die();
        }
        
        $posts = $Consumer->api('/search', 'GET', $facebookSearchParams); //name the service and get the listing of posts
        
        unset($Consumer);
        
        return $posts;
    }
    
    /**
    * Finds the Sentiment for a listing of Fb posts.
    * 
    * @param array $posts Checklist of posts coming from Fb's API
    * 
    * @param array $posts
    */
    protected operate findSentiment($posts) {
        $DatumboxAPI = new DatumboxAPI($this->datumbox_api_key); //initialize the DatumboxAPI shopper
        
        $outcomes=array();
        if(!isset($posts['data'])) {
            return $outcomes;
        }
        
        foreach($posts['data'] as $submit) { //foreach of the posts that we acquired
            $message=isset($submit['message'])?$submit['message']:'';
            
            if(isset($submit['caption'])) {
                $message.=("nn".$submit['caption']);
            }
            if(isset($submit['description'])) {
                $message.=("nn".$submit['description']);
            }
            if(isset($submit['link'])) {
                $message.=("nn".$submit['link']);
            }
            
            $message=trim($message);
            if($message!='') {
                $sentiment=$DatumboxAPI->SentimentAnalysis(strip_tags($message)); //name Datumbox service to get the sentiment
                
                if($sentiment!=false) { //if the sentiment isn't false, the API name was profitable.
                    $tmp = explode('_',$submit['id']);
                    if(!isset($tmp[1])) {
                        $tmp[1]='';
                    }
                    $outcomes[]=array( //add the submit message within the outcomes
                        'id'=>$submit['id'],
                        'person'=>$submit['from']['name'],
                        'textual content'=>$message,
                        'url'=>'https://www.fb.com/'.$tmp[0].'/posts/'.$tmp[1],
                        'sentiment'=>$sentiment,
                    );
                }
            }
        }
        
        unset($posts);
        unset($DatumboxAPI);
        
        return $outcomes;
    }
}


As you may see above on the constructor we cross the keys that are required to entry the two APIs. On the general public methodology sentimentAnalysis() we initialize the Fb Consumer, we authenticate and we retrieve the listing of posts. Be aware that you probably have not but approved your software or if you’re not logged in to Fb along with your account, you can be redirected to Fb.com to login and authorize the app (it’s your app, no worries about privateness points). As soon as the listing of posts is retrieved they’re handed to Datumbox API to get their polarity.

You’re good to go! You’re prepared to make use of this class to carry out Sentiment Evaluation on Fb. You may obtain the entire PHP code of the Fb Sentiment Evaluation device from Github.

Utilizing and Increasing the Implementation

To make use of the offered device it’s essential to create the Fb Utility as described above after which configure it by modifying the config.php file. On this file you will have to place the Datumbox API key, the Fb App Id and Secret that you simply copied earlier.

Lastly within the earlier submit now we have constructed a standalone Twitter Sentiment Evaluation device. It won’t take you greater than 10 minutes to merge the two implementations and create a single device which is able to fetching posts each from Fb and Twitter and presenting the ends in a single report.

In case you loved the article please take a minute to share it on Fb or Twitter! 🙂

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles