Sentiment Analysis using R
R
Analysis
Natural Language Processing
Regex
Syuzhet package
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
- First load the libraries and the dataset required to perform sentiment analysis
#Load libraries
library(syuzhet)
library(tm)
library(twitteR)
#Load dataset
<- read.csv("https://raw.githubusercontent.com/textmining-infopros/chapter7/master/7b_dataset.csv")
data
#Avoid error related to tolower() invalid multibyte string
sapply(data,is.character)] <- sapply(
data[,
sapply(data,is.character)],
data[,
"WINDOWS-1252","UTF-8") iconv,
- 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
<- as.vector(t(data)) vector
- 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
<- get_nrc_sentiment(vector) emotion.data
#emotions
<- cbind(data, emotion.data) emotion.data2
#sentiment score
<- get_sentiment(vector) sentiment.score