-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSentiment analysis_R.R
More file actions
61 lines (60 loc) · 2.2 KB
/
Sentiment analysis_R.R
File metadata and controls
61 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Developing Sentiment Analysis Model in R
library(tidytext)
sentiments
# Retrieving bing lexicon
get_sentiments("bing")
# Performing Sentiment Analysis with the Inner Join
# Performing tidy operation on our text such that each row contains a single word
library(janeaustenr)
library(stringr)
library(tidytext)
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)
# Using the book Emma and derive its words to implement our sentiment analysis model.
positive_senti <- get_sentiments("bing") %>%
filter(sentiment == "positive")
tidy_data %>%
filter(book == "Emma") %>%
semi_join(positive_senti) %>%
count(word, sort = TRUE)
# Segregate data into separate columns of positive and negative sentiment, Calculate the total sentiment.
library(tidyr)
bing <- get_sentiments("bing")
Emma_sentiment <- tidy_data %>%
inner_join(bing) %>%
count(book = "Emma" , index = linenumber %/% 80, sentiment) %>%
spread(sentiment, n, fill = 0) %>%
mutate(sentiment = positive - negative)
# Visualize the words present in the book “Emma”.
library(ggplot2)
ggplot(Emma_sentiment, aes(index, sentiment, fill = book)) +
geom_bar(stat = "identity", show.legend = TRUE) +
facet_wrap(~book, ncol = 2, scales = "free_x")
# Counting the most common positive and negative words
counting_words <- tidy_data %>%
inner_join(bing) %>%
count(word, sentiment, sort = TRUE)
head(counting_words)
# Visualization of our sentiment score.
counting_words %>%
filter(n > 150) %>%
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")
# Creating a wordcloud that will delineate the most recurring positive and negative words.
library(reshape2)
library(wordcloud)
tidy_data %>%
inner_join(bing) %>%
count(word, sentiment, sort = TRUE) %>%
acast(word ~ sentiment, value.var = "n", fill = 0) %>%
comparison.cloud(colors = c("red", "dark green"),
max.words = 100)