-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitter.R
97 lines (82 loc) · 2.21 KB
/
Twitter.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
87
88
89
90
91
92
93
94
95
96
97
###################################################################
########### Get tweets by keywords + sentiment analysis ###########
###################################################################
rm(list = ls())
library("rtweet")
library("tm")
library("RColorBrewer")
library("wordcloud")
library("ROAuth")
library("twitteR")
library('RCurl')
library("tidytext")
# Twitter Info
key = ""
secret = ""
#Twitter API info and call the whole object authenticate
authenticate <- OAuthFactory$new(
consumerKey = key,
consumerSecret = secret,
requestURL = 'https://api.twitter.com/oauth/request_token',
accessURL = 'https://api.twitter.com/oauth/access_token',
authURL = 'https://api.twitter.com/oauth/authorize'
)
# this will get to a Twitter Site where we can obtain the PIN
authenticate$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
# insert the PIN from Twitter
library("httr")
access_token = ""
access_secret = ""
library("rtweet")
#Give the app name
twitter_token <-
create_token(
app = "",
consumer_key = key,
consumer_secret = secret,
access_token,
access_secret
)
use_oauth_token(twitter_token)
#Obtain the required tweets by key words
tweets <- search_tweets(
"disneyplus AND disney+",
include_rts = FALSE,
n = 2000,
lang = "en",
reply_to_status_id = NULL,
until = "2019-09-19"
)
corpus <- Corpus(VectorSource(tweets$text))
clearCorpus <-
tm_map(corpus, function(x)
iconv(enc2utf8(x), sub = "byte"))
tdm <- TermDocumentMatrix(
clearCorpus,
control =
list(
removePunctuation = TRUE,
stopwords = c("com", "https", stopwords("english")),
removeNumbers = TRUE,
tolower = TRUE
)
)
m <- as.matrix(tdm)
word_freqs <- sort(rowSums(m), decreasing = TRUE)
dm <- data.frame(word = names(word_freqs), freq = word_freqs)
#Wordcloud of most repeated words
wordcloud(
dm$word,
dm$freq,
scale = c(3, .5),
random.order = FALSE,
colors = brewer.pal(8, "Dark2")
)
#Sentiment analysis of tweets
sentiment_analysis <- get_sentiments("afinn")
#'arg' should be one of “bing”, “afinn”, “loughran”, “nrc”
hist(
sentiment_analysis$value,
main = c("Sentiment Analysis of Tweets"),
xlab = c("Sentiment Analysis Value")
)