Sentiment analysis (also known as opinion mining or emotion AI) refers to the use of natural language processing, text analysis, computational linguistics, and biometrics to systematically identify, extract, quantify, and study affective states and subjective information. Sentiment analysis is widely applied to voice of the customer materials such as reviews and survey responses, online and social media, and healthcare materials for applications that range from marketing to customer service to clinical medicine.
# remove.packages(c('tidytext','textdata','janeaustenr','tidyr'))
if("tidytext" %in% rownames(installed.packages()) == FALSE)
{install.packages("tidytext")}
library(tidytext)
sentiments
subset(sentiments, sentiment == 'positive')
subset(sentiments, sentiment == 'negative')
if("textdata" %in% rownames(installed.packages()) == FALSE)
{install.packages("textdata")}
library(textdata)
get_sentiments("afinn")
subset(get_sentiments("afinn"),value==0)
subset(get_sentiments("afinn"),value==2)
subset(get_sentiments("afinn"),value==-4)
get_sentiments("bing")
subset(get_sentiments("bing"), sentiment == 'positive')
subset(get_sentiments("bing"), sentiment == 'negative')
get_sentiments("loughran")
subset(get_sentiments("loughran"), sentiment == 'positive')
subset(get_sentiments("loughran"), sentiment == 'negative')
get_sentiments("nrc")
if("janeaustenr" %in% rownames(installed.packages()) == FALSE)
{install.packages("janeaustenr")}
library(janeaustenr)
if("stringr" %in% rownames(installed.packages()) == FALSE)
{install.packages("stringr")}
library(stringr)
if("dplyr" %in% rownames(installed.packages()) == FALSE)
{install.packages("dplyr")}
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
austen_books()
austen_books() %>% group_by(book) %>%
summarise(total_lines = n())
tidy_data <- austen_books() %>%
group_by(book) %>%
mutate(linenumber = row_number(),
chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
ignore_case = TRUE)))) %>%
ungroup() %>%
unnest_tokens(word, text)
tidy_data
positive_sentiment <- get_sentiments("bing") %>%
filter(sentiment == "positive")
tidy_data %>%
filter(book == "Sense & Sensibility") %>%
semi_join(positive_sentiment) %>%
count(word, sort = TRUE)
## Joining, by = "word"
if("tidyr" %in% rownames(installed.packages()) == FALSE)
{install.packages("tidyr")}
library(tidyr)
SandS_sentiment <- tidy_data %>%
inner_join(get_sentiments("bing")) %>%
count(book = "Sense & Sensibility",
index = linenumber %/% 80, sentiment) %>%
spread(sentiment, n, fill = 0) %>%
mutate(sentiment = positive - negative)
## Joining, by = "word"
SandS_sentiment
if("ggplot2" %in% rownames(installed.packages()) == FALSE)
{install.packages("ggplot2")}
library(ggplot2)
ggplot(SandS_sentiment, aes(index, sentiment, fill = book)) +
geom_bar(stat = "identity", show.legend = TRUE) +
facet_wrap(~book, ncol = 2, scales = "free_x")
word_count <- tidy_data %>%
filter(book == "Sense & Sensibility") %>%
inner_join(get_sentiments("bing")) %>%
count(word, sentiment, sort = TRUE)
## Joining, by = "word"
head(word_count)
word_count %>%
filter(n > 30) %>%
mutate(n = ifelse(sentiment == "negative", -n, n)) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = sentiment))+
geom_col() +
coord_flip() +
labs(y = "Sentiment Score")
if("reshape2" %in% rownames(installed.packages()) == FALSE)
{install.packages("reshape2")}
library(reshape2)
##
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
##
## smiths
if("wordcloud" %in% rownames(installed.packages()) == FALSE)
{install.packages("wordcloud")}
library(wordcloud)
## Loading required package: RColorBrewer
tidy_data %>%
filter(book == "Sense & Sensibility") %>%
inner_join(get_sentiments("bing")) %>%
count(word, sentiment, sort = TRUE) %>%
acast(word ~ sentiment, value.var = "n", fill = 0) %>%
comparison.cloud(colors = c("red", "dark green"),
max.words = 100)
## Joining, by = "word"