-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsentiment-manning.Rmd
181 lines (142 loc) · 3.73 KB
/
sentiment-manning.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
---
title: "Sentiment Analysis Case Study: Peyton Manning vs. Tom Brady"
author: "Dr. Stephen W. Thomas, Queen's University"
date: "July 14, 2017"
output:
pdf_document:
highlight: pygments
number_sections: yes
toc: no
toc_depth: '2'
---
```{r}
library(tidytext)
library(RSentiment)
library(cleanNLP)
library(tidyr)
library(dplyr)
library(ggplot2)
library(readr)
library(lubridate)
library(stringr)
library(scales)
```
```{r}
# Example input: "Thu Oct 02 15:38:29 +0000 2014"
configDate = function(x) {
tmp = strsplit(x, "\\s+")[[1]]
as.POSIXct(paste(tmp[2], tmp[3], tmp[6]), format="%b %d %Y", tz="GMT")
}
```
```{r}
createDate = FALSE
if (createDate == TRUE){
tweets_manning <- read_csv("manning.csv")
tweets_brady <- read_csv("brady.csv")
tweets <- bind_rows(tweets_manning %>%
mutate(person = "Manning"),
tweets_brady %>%
mutate(person = "Brady"))
tweets$timestamp = 0
for (i in 1:nrow(tweets)){
tweets[i,]$timestamp = configDate(tweets[i,]$time)
}
tweets = tweets %>%
select(-time) %>%
select(id, timestamp, person, text)
write_csv(tweets, "alltweets.csv")
} else {
tweets = read_csv("alltweets.csv", col_types = list(id = col_number()))
}
tweets$id = c(1:nrow(tweets))
tweets[1:10,]
tweets <- tweets %>%
sample_frac(0.1)
```
```{r}
ggplot(tweets, aes(x = as.POSIXct(timestamp, origin="1970-01-01"), fill = person)) +
geom_histogram(position = "identity", bins = 11, show.legend = FALSE) +
facet_wrap(~person, ncol = 1, scales = "free_y") +
scale_x_datetime(labels = date_format("%b %d %Y", tz="GMT"), breaks=date_breaks("1 day")) +
labs(x="Date", y="Number of Tweets") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
```{r}
# TODO:
# Count exact duplicates
# Count retweets
# Count tweets with url
# Count length of tweets
# Show duplicate tweets
tweets %>%
group_by(text) %>%
summarize(count = n()) %>%
arrange(desc(count))
replace_reg <- "https://t.co/[A-Za-z\\d]+|http://[A-Za-z\\d]+|&|<|>|RT|https"
unnest_reg <- "([^A-Za-z_\\d#@']|'(?![A-Za-z_\\d#@]))"
custom_stop_words = data.frame(word=c("loan", "business"))
# Remove URLs
tweets = tweets %>%
mutate(text = gsub("(f|ht)tp(s?)://\\S+", "", text))
# Remove URLs
tweets = tweets %>%
mutate(text = gsub("(f|ht)tp(s?)://\\S+", "", text))
tweets
```
Create tidy format.
```{r}
text_df <- tweets %>%
#filter(!str_detect(text, "^RT")) %>%
#mutate(text = str_replace_all(text, replace_reg, "")) %>%
#unnest_tokens(word, text, token = "regex", pattern = unnest_reg) %>%
unnest_tokens(word, text)
text_df
```
```{r}
## Remove stopwords
custom_stop_words = data.frame(word=c("loan", "business"))
text_df <- text_df %>%
anti_join(stop_words, by=c("word"="word")) %>%
anti_join(custom_stop_words, by=c("word"="word")) %>%
arrange(id)
```
Do sentiment analysis for
```{r}
nrc <- get_sentiments("nrc")
head(text_df)
sents = text_df %>%
inner_join(nrc) %>%
count(person, id = id, timestamp=timestamp, sentiment) %>%
spread(sentiment, n, fill = 0) %>%
mutate(sentiment = positive - negative) %>%
arrange(sentiment)
sents
```
Look at examples
Quantify sentiment by rating
Look at average
Look at average over time
Look at different emotions
Try different lexicons
```{r}
sents
sents %>%
ggplot(aes(sentiment)) + geom_histogram() + facet_wrap(~person)
sents %>%
filter(person=="Manning") %>%
group_by(sentiment) %>%
summarise (n = n()) %>%
mutate(freq = n / sum(n), cum = cumsum(freq))
sents %>%
filter(person=="Brady") %>%
group_by(sentiment) %>%
summarise (n = n()) %>%
mutate(freq = n / sum(n), cum = cumsum(freq))
```
```{r}
tweets[tweets$id==6621,]$text
```
```{r}
text_df %>%
filter (id == 1739)
```