First of all, let’s attach our dataset via url. Dataset for this tutorial is in the Github. You can download it and read from local as csv file.
urlfile<-'https://raw.githubusercontent.com/qarabala/DataSets/main/Sentiment_Text_data.csv'
text_df<-read.csv(urlfile)
#text_df<-read.csv(file.choose(), stringsAsFactors = FALSE)
Before start our tutorial, let’s understand what does the sentimental analysis mean? We are going to use ‘sentimentr’ package.
What is Sentiment Analysis?
Sentiment Analysis is a process of extracting opinions that have different scores like positive, negative or neutral. Based on sentiment analysis, you can find out the nature of opinion or sentences in text.
Sentiment Analysis is a type of classification where the data is classified into different classes like positive or negative or happy, sad, angry, etc.
library(sentimentr)
#some_text<-c('This is a very beautiful day', 'I am not feeling good. I hate it when it happens',
#'The product is good but expensive at this price', 'I love you')
some_text<-text_df
result<-sentiment(some_text$sentence)
#plot(result)
head(result)
## element_id sentence_id word_count sentiment
## 1: 1 1 33 0.2263010
## 2: 2 1 35 0.2112886
## 3: 2 2 4 0.0000000
## 4: 3 1 5 -0.2511999
## 5: 4 1 19 0.4588315
## 6: 5 1 21 0.2291288
result<-sentiment_by(some_text$sentence)
head(result)
## element_id word_count sd ave_sentiment
## 1: 1 33 NA 0.2263010
## 2: 2 39 0.1494036 0.1152973
## 3: 3 5 NA -0.2511999
## 4: 4 19 NA 0.4588315
## 5: 5 21 NA 0.2291288
## 6: 6 24 NA 0.1530931
result<-extract_sentiment_terms(some_text$sentence)
head(result)
## element_id sentence_id negative positive
## 1: 1 1 new,greater
## 2: 2 1 gorgeously,director,vision
## 3: 2 2
## 4: 3 1 tepid effective
## 5: 4 1 like,fun,good
## 6: 5 1 issue honest,keenly,like
In this article we are going to make a Sentimental analysis with syuzhet library. syuzhet is an R package for the extraction of sentiment and sentiment-based plot arcs from text.
library(syuzhet, warn.conflicts=F, quietly=T)
#Convert it into character vector
review<-as.character(text_df$sentence)
#Obtain sentiment scores
get_nrc_sentiment('happy')
## anger anticipation disgust fear joy sadness surprise trust negative positive
## 1 0 1 0 0 1 0 0 1 0 1
get_nrc_sentiment('excitement')
## anger anticipation disgust fear joy sadness surprise trust negative positive
## 1 0 1 0 0 1 0 1 0 0 1
s<-get_nrc_sentiment(review)
#Combine text and sentiment columns
review_sentiment<-cbind(text_df$sentence,s)
#head(review_sentiment)
barplot(colSums(s), col = rainbow(10), ylab = 'Count', main = 'Sentimental scores of movie')
Classwork
Investigate sentimentr and syuzhet packages (and their functions)
Workshop: https://www.r-bloggers.com/2021/05/sentiment-analysis-in-r-3/