Sentiment Analysis using R

R
Analysis
Natural Language Processing
Regex
Syuzhet package
Author

Simisani Ndaba

Published

October 8, 2022

Meetup Description

After months of postponements and re-appointments, the meetup came to fruition a talk on Sentiment Analysis using different R packages. The meetup aimed at looking at positive and negative sentiments in texts, but also more detailed emotions such as surprise or fear. We learned how to visualise commonly used positive and negative words. The session was suitable for all levels and didn’t need any previous experience with this topic.

Sentiment Analysis using R

Sentiment analysis (also referred to as subjectivity analysis or opinion mining or emotion artificial intelligence) is a NLP technique that identifies important patterns of information and features from a large text corpus.

The session had the following demonstration.

PART I

  1. First load the libraries and the dataset required to perform sentiment analysis
#Load libraries

library(syuzhet)

library(tm)

library(twitteR)

#Load dataset

data<- read.csv("https://raw.githubusercontent.com/textmining-infopros/chapter7/master/7b_dataset.csv")

#Avoid error related to tolower() invalid multibyte string

data[,sapply(data,is.character)] <- sapply(

data[,sapply(data,is.character)],

iconv,"WINDOWS-1252","UTF-8")
  1. The syuzhet package works only on vectors. So, the data was converted to a vector.
#syuzhet package works only on vectors. So, the data was converted to a vector

vector <- as.vector(t(data))
  1. For each book review, scores were determined for different emotions, where a score with value 0 implies the emotion is not associated with the review, and the score of value 1 means there is an association between the emotion and the review. Subsequently, a higher score indicates stronger emotion.
#Sentiment analysis

emotion.data <- get_nrc_sentiment(vector)
#emotions

emotion.data2 <- cbind(data, emotion.data)
#sentiment score

sentiment.score <- get_sentiment(vector)
Back to top