-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFP_sentiments.R
66 lines (53 loc) · 1.47 KB
/
FP_sentiments.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
library(tidyverse)
library(lubridate)
library(stringr)
library(tidytext)
library(broom)
library(scales)
library(dplyr)
library(ggplot2)
library(rsconnect)
theme_set(theme_bw())
# get text
Livy <- read_csv("All_Livy.csv")%>%
mutate(Book = parse_number(Book))
# convert to tokens
Livy <- Livy %>%
group_by(Book, Chapter) %>%
unnest_tokens(word, Text) %>%
mutate(linenumber = row_number())
# remove stop words
cleaned_livy <- Livy %>%
anti_join(stop_words)
cleaned_livy %>%
count(word, sort = TRUE)
# add sentiment from NRC dictionary
LivySentiment <- cleaned_livy %>%
inner_join(get_sentiments("nrc"))
# summarize sentiment by chapter
LivySentimentCount <- LivySentiment %>%
count(Book, Chapter, sentiment) %>%
left_join(cleaned_livy %>%
count(Book, Chapter) %>%
rename(n_total = n)) %>%
mutate(pct = n / n_total)
chapter_id <- Livy %>%
select(Book, Chapter) %>%
distinct() %>%
ungroup %>%
mutate(livy_id = row_number())
LivySentimentCount <- LivySentimentCount %>%
left_join(chapter_id) %>%
arrange(livy_id, sentiment)
break_points <- chapter_id %>%
group_by(Book) %>%
slice(1) %>%
na.omit()
ggplot(LivySentimentCount, aes(livy_id, pct, group = sentiment,
color = sentiment)) +
geom_smooth(se = FALSE)+
ggtitle("Sentiments In Livy")+
xlab("Book")+
ylab("Percent of Text per Chapter")+
scale_x_continuous(breaks = break_points$livy_id,
labels = break_points$Book)