-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextMining_SentimentAnalysis.R
107 lines (76 loc) · 3.24 KB
/
TextMining_SentimentAnalysis.R
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#Text Mining: "Sentiment Analysis"
#install.packages("tidytext")# provides additional text mining functions
#install.packages("tidyr")
#install.packages("ggplot2")
#install.packages("wordcloud")
#Dictionaries to segreggate words:
#There are a variety of dictionaries that exist for evaluating the opinion or emotion in text.
#The tidytext package contains three sentiment lexicons in the sentiments dataset.
library(tidytext)
sentiments
#selecting the lexicon:
get_sentiments("afinn")
get_sentiments("nrc")
get_sentiments("bing")
#Tidytext will allow us to perform efficient text analysis on our data.
#We will convert the text of our books into a tidy format using unnest_tokens() function.
library(janeaustenr)
library(stringr)
library(dplyr)
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
#We will now make use of the "bing" lexicon to implement filter() over the words that correspond to joy.
positive_senti <- get_sentiments("bing") %>%
filter(sentiment == "positive")
tidy_data %>%
filter(book == "Emma") %>%
semi_join(positive_senti) %>%
count(word, sort = TRUE)
#From our above result, we observe many positive words like "good", "happy", "love" etc.
#Now, we use spread() function to segregate our data into separate columns of positive and negative sentiments.
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)
#We use the mutate() function to calculate the difference between positive and negative sentiment.
Emma_sentiment
#Visualizing the words present in book "Emma" based on their corrosponding positive and negative scores.
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")
#Let's count the most common positive and negative words that are present in the novel.
counting_words <- tidy_data %>%
inner_join(bing) %>%
count(word, sentiment, sort = TRUE)
head(counting_words)
#visualizing 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")
#visualizing wordcloud to show the most recurring positive and negative words.
#using the comparision.cloud() function to plot both negative and positive words in a single wordcloud.
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)
#This word cloud will enable us to efficiently visualize the negative as well as positive groups of data.
#We are able to see different groups of data based on their corresponding sentiments.