-
Notifications
You must be signed in to change notification settings - Fork 1
/
negation_sentimentr.rmd
188 lines (106 loc) · 4.36 KB
/
negation_sentimentr.rmd
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
---
title: "part_4"
author: "RN7"
date: "November 25, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r}
library(sentimentr)
library(magrittr)
library(dplyr)
cr90 <- c("all power to the engines, we can't take another hit like that!",
"we're running out of time", "we won't be able to to get out of this sector without this",
"don't you love how the empire always know how to find us?",
"don't like to be that guy but...", "I don't hate Rodians!")
cr90 %>% sentiment()
cr90 %>% extract_sentiment_terms()
'I dont hate Rodians' %>% extract_sentiment_terms()
"i won't run away, although the storm is gettign worse and i see no end" %>% extract_sentiment_terms()
"i won't run away, although the storm is gettign worse and i see no end" %>% sentiment_attributes()
```
```{r}
presidential_debates_2012 <- presidential_debates_2012
presidential_debates_2012 <- presidential_debates_2012 %>%
dplyr::mutate(dialogue_split = get_sentences(dialogue))
presidential_debates_2012 %>%
dplyr::mutate(dialogue_split = get_sentences(dialogue)) %$%
sentiment_by(dialogue_split, list(person, time))
presidential_debates_2012 <- presidential_debates_2012 %>%
sentiment_by(dialogue_split, list(person, time))
library(termco) # not available for current R version (3.4.0)
library(gofastr)
sentiment_attributes(presidential_debates_2012$dialogue)
```
```{r}
library(tidytext)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)
df <- read.csv("thrice.csv", stringsAsFactors = FALSE)
glimpse(df)
thrice <- df %>%
unnest_tokens(lines, lyrics, token = str_split, pattern = " <br>") %>%
unnest_tokens(word, lines, token = "words")
```
```{r}
thrice_split_df <- df %>%
unnest_tokens(lines, lyrics, token = str_split, pattern = " <br>")
thrice_split_df <- thrice_split_df %>%
mutate(lines_split = get_sentences(lines))
thrice_split_df <- thrice_split_df %$%
sentiment_by(lines_split, list(title))
thrice_split_df %>% arrange(desc(ave_sentiment))
plot(thrice_split_df)
plot(uncombine(thrice_split_df))
thrice_split_df %>% highlight()
thrice %>% filter(album == "Identity Crisis") %>% sentiment_attributes(word)
thrice_split_df %>%
ggplot(aes(title, ave_sentiment)) +
geom_col()
```
```{r}
thrice %>%
mutate(word = get_sentences(word)) %>%
sentiment_by(word, list(title))
```
## Preview for Part 4
Here's a little preview of what examining negation words in bi-grams looks like. In the plot below we created a new variable called `contribution` where we multiplied the **AFINN** score of `word2` (the word coming after a *negation* word `word1`) with their frequency to see the full effect of each negation bi-gram on the overall sentiment.
```{r negation bi-gram sample, fig.height=7, fig.width=10, fig.align='center', echo=FALSE}
library(tidyr)
biGrams <- df %>%
select(album, title, year, lyrics) %>%
mutate(lyrics = iconv(lyrics, to = 'latin1')) %>%
# convert to ASCII for better separation into ngrams (words with apostrophes)
unnest_tokens(line, lyrics, token = stringr::str_split, pattern = ' <br>') %>%
# split lines on the <br> tags
unnest_tokens(ngram, line, token = "ngrams", n = 2)
biGrams_sep <- biGrams %>%
separate(ngram, c("word1", "word2"), sep = "[^-'\\w]")
negation_words <- c("not", "no", "never", "without", "won't", "don't", "wouldn't", "couldn't")
negated_bigrams <- biGrams_sep %>%
filter(word1 %in% negation_words) %>%
inner_join(get_sentiments("afinn"), by = c(word2 = "word")) %>%
count(word1, word2, score, sort = TRUE) %>%
ungroup()
negated_bigrams %>%
mutate(contribution = n * score,
score = reorder(paste(word2, word1, sep = "__"), contribution),
sentiment = if_else(contribution > 0, "positive", "negative")) %>%
group_by(word1) %>%
# top_n(10, abs(contribution)) %>%
ggplot(aes(word2, contribution, fill = as.factor(sentiment))) +
geom_col(show.legend = FALSE, alpha = 0.8, width = 0.9) +
scale_fill_manual(guide = FALSE, values = c("black", "darkgreen")) +
facet_wrap(~ word1, scales = "free") +
coord_flip() +
theme_bw() +
theme(axis.text = element_text(size = 12),
panel.grid.minor.x = element_line(size = 1.1),
panel.grid.major.x = element_line(size = 1.1)) +
labs(x = "Word Preceded by Negation Term", y = "Sentiment Score * Number of Occurences")
```