Hi, everyone !

Hope everyone is having a great time.
In this post, we are going to see the TWITTER SENTIMENT ANALYSIS by using JAVA as a programming language.

We are using OPENNLP Maven dependencies for doing this sentiment analysis.
Following is that Maven Dependency.

As you can see from the above pom.xml file, we are using three dependencies here.
A couple of these are for twitter namely twitter4j-core and twitter4j-stream.
The remaining dependency is opennlp-tools which is responsible for depicting the nature of tweet.
OpenNLP will tell you whether a tweet is positive or negative.

For building this model, we are going to use following input file.

I got this input file from this link.

This file contains two columns separated by tab character.
First column contains either 0 or 1 where 0 indicates negative and 1 indicates positive.
Second column contains the actual tweet.

Following is the input text file.

By using above input file, we are going to use following code in order to decide the tweet that we have fetched is positive or negative.

Following are the key points regarding above code

  • Line 44 is Consumer (API) Key
  • Line 45 is Consumer (API) Secret
  • Line 46 is Access Token
  • Line 47 is Access Token Secret
  • Line 50 indicates the keyword which is used for filtering out the tweets i.e. Udta Punjab
  • Line 69 depicts the trainModel() method which is used for creating the model using the tweets.txt file
  • Line 93 has classifyNewTweet() method which decides whether a tweet is positive or negative by using the already created model by trainModel() method

Once you execute this code, model will be created and then by using twitter API, tweets will be fetched which are then going to be classified either as POSITIVE or NEGATIVE.
The final output will be stored in an output file with name results.csv.

Twitter Sentiment Analysis Output Part 1
Twitter Sentiment Analysis Output Part 1
Twitter Sentiment Analysis Output Part 2
Twitter Sentiment Analysis Output Part 2
Twitter Sentiment Analysis Output Part 3
Twitter Sentiment Analysis Output Part 3

The final output looks something like this.

results file
results file


If you want to graphically represent the output of positive and negative tweets, you can use Microsoft Office Excel tool to do that.
You can open results.csv file in Excel and after selecting the output, go to Insert and click on Recommended Charts.
You will get the list of chart types and you can choose the suitable chart type for you.
I did use Bar Chart and the output looks something like this.

Sentiment Analysis Output
Sentiment Analysis Output

That is it people.
Thanks for having a read.
Kindly let me know if you have any concerns.
Have a great time. Cheers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s