In this blog we will learn how to analyze the comments of Public Facebook post using Facebook Graph API Explorer & R!
Facebook developer account required to get started with this Facebook Graph API .
If you don’t have Facebook developer account, you can upgrade your personal Facebook account to a Facebook Developer account from here this link.
After registering as Facebook Developer, go to “Tools & Support”->”Graph API Explorer”
To explore Graph API – Token & Permissions are required, so just click on the “Get Token”.
~ Courtesy Facebook Developer
As public profiles included by default in permissions, just click on “Get Access Token”.
Below is my access token, which will expire after some time. As shown in token info.
Now we have token, let’s explore now.
Extracting Comments from the Public Facebook Post.
First thing you required is the Post Id of the post. See below steps to get the post Id.
Suppose below is the post, we want to analyze. Click on the Post Date Time. See below highlighted box. ~ Post Courtesy Facebook
Copy below Id. This is the post Id.
Go to the Graph Explorer.
Type “Post_id/comments” in below box & click on Submit.
You can also give limit for number of comments to return, like this
Output: Below are post comments in the JSON format.
If you want more comments click on “next” for next page of comments.
Click on “Get Code” to get the cURL code. Copy this URL, we will use this URL in R.
Text Analysis in R
R Packages required:
install.packages(“RCurl “): It allows us to compose general HTTP requests and provides convenient functions to fetch data.
install.packages(“rjson”): It allows us to converts JSON object into R objects and vice-versa.
install.packages(“tm”): A Mining Package for text mining applications within R. It offers a number of transformations that ease the tedium of cleaning data.
url used in above image is copied from cURL code from Graph Explorer.
Output: So the first page give me 25 comments only. We will analyze here only 1st page i.e. 25 comments only.
Cleaning & Analyzing Data:
Creating corpus & removing extra spaces, special characters & other unwanted things.
Creating Term Document Matrix:
Here is the 760 extracted words with frequency.
install.packages(“wordcloud”): For plotting a word cloud
In this Word Cloud we are taking only 100 words with minimum frequency of 2.
Feedback and suggestions are most welcome. If you have any feedback, suggestions or questions please comment.
Happy Learning!Follow @shobhitsinghIN