-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sentiment Analysis.R
37 lines (36 loc) · 1.18 KB
/
Sentiment Analysis.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
library(janeaustenr)
library(stringr)
library(tidytext)
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)
positive_senti <- get_sentiments("bing") %>%
filter(sentiment == "positive")
tidy_data %>%
filter(book == "prideprejudice") %>%
semi_join(positive_senti) %>%
count(word, sort = TRUE)
library(tidyverse)
bing <- get_sentiments("bing")
prideprejudice_sentiment <- tidy_data %>%
inner_join(bing) %>%
count(book = "prideprejudice" , index = linenumber %/% 80, sentiment) %>%
spread(sentiment, n, fill = 0) %>%
mutate(sentiment = positive - negative)
counting_words <- tidy_data %>%
inner_join(bing) %>%
count(word, sentiment, sort = TRUE)
head(counting_words)
counting_words %>%
filter(n > 125) %>%
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")