June 30, 2016 1:38 am

An introduction to Twig templating – 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

In this last piece, we are going to show the sentiments of the collected using Facebook’s PHP SDK posts of an arbitrary page using Twig.

Our file which is served to users loads the Composer dependencies (Composer is a dependanc manager for PHP from which you can import third-party libraries – https://getcomposer.org/). Then, it checks whether a page-name GET parameter is set. If it is set, then the user has submitted the form and we would have to analyze the sentiments of its posts.

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

Following that, we instantiate the Twig loader, indicating that the templates are going to be in the file system and passing an argument to the folder where our templates would be.

Thus, our folder where Twig should start searching for templates is called templates.

Next, we actually set up Twig for displaying the templates by calling Twig_Environment, passing it the loader variable and any options. In terms of options, we set up the path to the view caches to be the subfolder cache in our templates directory. The cached view will be shown whenever a user accesses the same page with the same parameters twice.

Then, we build up the arguments for the view. If a sentiment analysis has been carried out – there should be arguments passed to the view. If there is no error, the arguments are the resulting pageAnalysis object and the name of the page. If there is an error, then we only set up the error as an argument. You could use ternary operators to manage the checks instead.

Finally, we display the index.html view (which should be located in the templates folder that we pointed Twig to) with Twig and pass it the necessary arguments.

echo $twig->render(‘index.html’, $args);
Using Twig’s templating capabilities, we check if there was an argument called error passed to the template and display its value – if there was an error.  Using {% %} we can run conditionals, loops and other logic while using {{ }} we display a variable, an argument, etc.. The |e filter in the context of error|edisplays the error argument while escaping it.

Then, no matter the arguments, we display a form where the users can write a page id, submit it and see new sentiment analysis results.

Following that, we start displaying the results of the sentiment analysis but only if there is one to display. We display the escaped name of the page that the user wrote  and show its dominant sentiment. We also display the sentiment in different color by providing different classes for positive, negative and neutral sentiments.

We display the number of positive, neutral, and negative posts by adding a HTML5 progress tag for each sentiment, we set the maximum value of the progress bar to be the total number of posts and the current value to be the positive posts for the positive progress bar, the negative for the second negative progress bar and so on. We also display a label which shows the actual numbers behind the progress bar.

After we repeat the above process for the negative and neutral sentiments, we employ a similar technique to display the total distribution of positivity, negativity and neutrality on the Facebook page.

So, besides learning a thing or two about templating with Twig, we have created a simple mini-sentiment analysis app that can inform users on the mood surrounding different Facebook pages.

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: