Sentiment analysis using R

43,341

Solution 1

And there is this package:

sentiment: Tools for Sentiment Analysis

sentiment is an R package with tools for sentiment analysis including bayesian classifiers for positivity/negativity and emotion classification.

Update 14 Dec 2012: it has been removed to the archive...

Update 15 Mar 2013: the qdap package has a polarity function, based on Jeffery Breen's work

Solution 2

Here's the work I've done on sentiment analysis in R.

The code is, by no means, polished or well-packaged, but I posted it on Github with basic documentation. I used the ViralHeat sentiment API, which just returns JSON, so the actual function to do the sentiment analysis is pretty trivial (see code here).

Feel free to contact me if you're having trouble using it. And note that you'll need to register for an API key with ViralHeat before you'll be able to use it. If you're finding the quotas too restrictive, I had contacted them and they were happy to give me a ton more queries for a few months while I played around with the API.

Solution 3

For step by step guide to use 1) Viral Heat API 2) Jeffrey Breen's approach 3) Using Sentiment Package, check out this link: https://sites.google.com/site/miningtwitter/questions/sentiment

Solution 4

I've tried to reorganize and provide a cohesive sentiment analysis package here. SentR includes word stemming and preprocessing and provides access to the ViralHeat API, a default aggregating function as well as a more advanced Naive Bayes method.

Installing is relatively simple:

install.packages('devtools')
require('devtools')
install_github('mananshah99/sentR')
require('sentR')

And a simple classification example:

# Create small vectors for happy and sad words (useful in aggregate(...) function)
positive <- c('happy', 'well-off', 'good', 'happiness')
negative <- c('sad', 'bad', 'miserable', 'terrible')

# Words to test sentiment
test <- c('I am a very happy person.', 'I am a very sad person', 
'I’ve always understood happiness to be appreciation. There is no greater happiness than appreciation for what one has- both physically and in the way of relationships and ideologies. The unhappy seek that which they do not have and can not fully appreciate the things around them. I don’t expect much from life. I don’t need a high paying job, a big house or fancy cars. I simply wish to be able to live my life appreciating everything around me. 
')

# 1. Simple Summation
out <- classify.aggregate(test, positive, negative)
out

# 2. Naive Bayes
out <- classify.naivebayes(test)
out

Which provides the following output:

  score
1     1
2    -1
3     2

     POS                NEG                 POS/NEG             SENT      
[1,] "9.47547003995745" "0.445453222112551" "21.2715265477714"  "positive"
[2,] "1.03127774142571" "9.47547003995745"  "0.108836578774127" "negative"
[3,] "67.1985217685598" "35.1792261323723"  "1.9101762362738"   "positive"

Please feel free to contribute :) Hope that helps!

Share:
43,341
djq
Author by

djq

Currently working for a large tech company in Dublin. Previously, a co-founder of a technology startup; there I worked with a small team using Python, Django, Django-Rest-Framework, Pandas (and more!). I've a background in urban analysis (geospatial data), stats (R) and data visualization (ggplot2, D3) Excited about how technology can be used for social good.

Updated on October 19, 2020

Comments

  • djq
    djq over 3 years

    Are there any R packages that focus on sentiment analysis? I have a small survey where users can write a comment about their experience of using a web-tool. I ask for a numerical ranking, and there is the option of including a comment.

    I am wondering what the best way of assessing the positiveness or negativeness of the comment is. I would like to be able to compare it to the numerical ranking that the user provides, using R.