Sentiment Analysis in R with {sentimentr} that handles Negation (Valence Shifters)

[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 the most wanted and used NLP techniques. Companies like to see what their customers are talking about – like if there’s a new product launch then what’s the feedback about it. Whereever you’ve got Natural Language – like Social Media, Community Pages, Customer Support – Sentiment Analysis as a technique has found its home there.

While the technique itself is highly wanted, Sentiment Analysis is one of the NLP fields that’s far from super-accurate and the reason being is a lot of ways Humans talk. One of the aspects of it is called Valence Shifters like Negation that can flip the polarity of a sentence with one word.

“I’m happy” -> Positive
“I’m not happy” -> Negative

Because of this, a lot of out-of-box Sentiment analysis packages and libraries fail at tasks like this. Kudos to Tyler Rinker’s sentimentr R package that handles this scenario very well. sentimentr is a lexicon-based Sentiment Analysis Package that’s one of the best out-of-box sentiment analysis solution (given you don’t want to build a Sentiment Classification or you don’t want to use a Paid API like Google Cloud API).

Youtube –

Video Tutorial

Code to get started:


text <- "This tutorial is awesome. The creator is not boring"




sentiment_by(text, by = NULL)


debates <- presidential_debates_2012  

debates_with_pol <- debates %>% 
  get_sentences() %>% 
  sentiment() %>% 
  mutate(polarity_level = ifelse(sentiment < 0.2, "Negative",
                                 ifelse(sentiment > 0.2, "Positive","Neutral")))

debates_with_pol %>% filter(polarity_level == "Negative") %>% View()

debates_with_senti %>% 
  ggplot() + geom_boxplot(aes(y = person, x = sentiment))

debates$dialogue %>% 
  get_sentences() %>% 
  sentiment_by() %>% #View()

debates %>% 
  get_sentences() %>% 
  sentiment_by(by = NULL) %>% #View()
  ggplot() + geom_density(aes(ave_sentiment))


To leave a comment for the author, please follow the link and comment on their blog: r-bloggers on Programming with R. 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)