-
Notifications
You must be signed in to change notification settings - Fork 24
/
4e1-twitter1.R
122 lines (102 loc) · 3.7 KB
/
4e1-twitter1.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
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
#Twitter 1 - Configure Tweets and Download them
#@dupadhyaya #Working using my Keys
#Load libraries
library("curl")
library("twitteR")
library("ROAuth")
library("syuzhet") #library for sentiment analysis - comparison
download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
#https://apps.twitter.com/
#different for each account
# consumerKey="uRDuync3BziwQnor1MZFBKp0x"
# consumerSecret="t8QPLr7RKpAg4qa7vth1SBsDvoPKawwwdEhNRjdpY0mfMMdRnV"
# AccessToken="14366551-Fga25zWM1YefkTb2TZYxsrx2LVVSsK0uSpF08sugW"
# AccessTokenSecret="3ap8BZNVoBhE2GaMGLfuvuPF2OrHzM3MhGuPm96p3k6Cz"
consumerKey='uRDuync3BziwQnor1MZFBKp0x'
consumerSecret='t8QPLr7RKpAg4qa7vth1SBsDvoPKawwwdEhNRjdpY0mfMMdRnV'
AccessToken='14366551-Fga25zWM1YefkTb2TZYxsrx2LVVSsK0uSpF08sugW'
AccessTokenSecret='3ap8BZNVoBhE2GaMGLfuvuPF2OrHzM3MhGuPm96p3k6Cz'
#Common for all accounts except the keys
cred <- OAuthFactory$new(consumerKey=consumerKey, consumerSecret=consumerSecret, requestURL='https://api.twitter.com/oauth/request_token', accessURL='https://api.twitter.com/oauth/access_token', authURL='https://api.twitter.com/oauth/authorize')
cred$handshake(cainfo="cacert.pem") # it will take you to browser: authorise, copy key and paste in R Studio at Console. Once it stores, it move to R prompt
save(cred, file="twitter authentication.Rdata") # store this to avoid asking again
#Load saved authentication cert
load("twitter authentication.Rdata")
#registerTwitterOAuth(cred)
setup_twitter_oauth(consumerKey, consumerSecret, AccessToken, AccessTokenSecret)
#type 1 : Yes
search.string <- "#businessanalytics"
search.string <- "#marketinganalytics"
no.of.tweets <- 100
tweets <- searchTwitter(search.string, n=no.of.tweets,lang="en")
tweets
tweets[1:10]
#Another Topics
search.string <- "#indvsaus"
#search.string <- "#asiacup"
no.of.tweets <- 100
tweets <- searchTwitter(search.string, n=no.of.tweets,lang="en")
tweets[1:5]
#My Tweets : will change if you use your own account
homeTimeline(n=15) #my tweets
mentions(n=15) # my tags
mentions(n=5)
#for user -
(tweets = userTimeline("rajatkadam1998", n=10))
userTimeline("rahulg_IITK", n=5)
#------------------------------------
?userTimeline
tweets = userTimeline("realDonaldTrump", n=100)
tweets = userTimeline("imVkohli", n=100)
#where he has been tagged also.#mentions(n=15)
#english
tweets[1:5]
n.tweet <- length(tweets)
n.tweet
tweets.df = twListToDF(tweets)
head(tweets.df)
summary(tweets.df)
#Remove hashtags & unnecessary characters
tweets.df2 <- gsub("http.*","",tweets.df$text)
tweets.df2 <- gsub("https.*","",tweets.df2)
tweets.df2 <- gsub("#.*","",tweets.df2)
tweets.df2 <- gsub("@.*","",tweets.df2)
head(tweets.df2)
#-----
library("syuzhet") #library for sentiment analysis - comparison
word.df <- as.vector(tweets.df2)
emotion.df <- get_nrc_sentiment(word.df)
emotion.df2 <- cbind(tweets.df2, emotion.df)
head(emotion.df2)
emotion.df2
word.df
#-----
sent.value <- get_sentiment(word.df)
sent.value
tweets[c(16,62)]
most.positive <- word.df[sent.value == max(sent.value)]
most.positive
most.negative<- word.df[sent.value <= min(sent.value)]
most.negative
sent.value
#-----
positive.tweets <- word.df[sent.value > 0]
head(positive.tweets)
negative.tweets <- word.df[sent.value < 0]
head(negative.tweets)
neutral.tweets <- word.df[sent.value == 0]
head(neutral.tweets)
#----
# Alternate way to classify as Positive, Negative or Neutral tweets
category_senti <- ifelse(sent.value < 0, "Negative", ifelse(sent.value > 0, "Positive", "Neutral"))
head(category_senti)
category_senti2 <- cbind(tweets,category_senti,sent.value)
head(category_senti2)
#----
table(category_senti)
tweets[13]
#Text Mining / Twitter Analysis
#Finding out sentiments
#looking for keywords used in the tweets
#wordcloud for these words
#