How to do negation-proof sentiment analysis in R

[This article was first published on r-bloggers on Programming with R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Sentiment Analysis is one of those things in Machine learning which is still getting improvement with the rise of Deep Learning based NLP solutions. There are many things like Sarcasm, Negations and similar items make Sentiment Analysis a rather tough nut to crack.

Deep learning as much as it’s effective, it’s also computationally expensive and if you are ready to trade off between Cost (expense) and Accuracy, then you this is the solution for building a negation-proof Sentiment Analysis solution (in R).

What’s Negation (Proof)?

Typical lexicon based Sentiment Analysis solutions can’t handle Negations easily – which is that good is positive and not good is negative. Negation Proof solution is something that can handle such negations and classify their polarity (sentiment) correctly.

sentimentr

For building a negation-proof sentiment analysis solution, we’re going to use the R package sentimentr by Tyler Rinker.

Why sentimentr?

According to the documentation of sentimentr,

So what does sentimentr do that other packages don’t?

sentimentr attempts to take into account valence shifters (i.e., negators, amplifiers (intensifiers), de-amplifiers (downtoners), and adversative conjunctions) while maintaining speed. Simply put, sentimentr is an augmented dictionary lookup.

For more information on Valence Shifters, Check out sentimentr’s documentation.

Installation

You can install the stable version of sentimentr from CRAN:

install.packages("sentimentr")

or

install the development version from Github:

# install.packages("devtools")

devtools::install_github("trinker/lexicon")
devtools::install_github("trinker/sentimentr")

Loading the package

Let’s import sentimentr and also magrittr for pipe operator (%>%) and dplyr for data manipulation.

library(sentimentr) 
library(magrittr)
library(dplyr)

Sample Text (with Negations)

Let’s define two sentences for us to check if sentimentr is negation-proof.

text1 <- "I am a good girl. Today I am happy"

text2 <- "I am not a good girl. Today I'm not happy"

Sentiment Analysis in Action

We’ll use the function sentiment() to identify the approximate the sentiment (polarity) of text by sentence.

sentimentr::sentiment(text1)
##    element_id sentence_id word_count sentiment
## 1:          1           1          5 0.3354102
## 2:          1           2          4 0.3750000
sentimentr::sentiment(text2)
##    element_id sentence_id word_count  sentiment
## 1:          1           1          6 -0.3061862
## 2:          1           2          4 -0.3750000

As we can see from the above outputs, using sentimentr is doing a good job in rightly scoring the sentiment score for sentence with and without negations (valence shifters).

The End

This is just a very simple (perhaps, Naive too) walkthrough of the sentimentr package but it has got a lot more like sentiment_by(), highlight(), profanity() and much more. Tyler Rinker has got a very nice, comprehensive and super-helpful documentation and also benchmarks comparing sentimentr with other similar packages.

To leave a comment for the author, please follow the link and comment on their blog: r-bloggers on Programming with R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)