library(twitteR)
library(stringr)
library(ggmap)
## Loading required package: ggplot2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(ggplot2)
library(wordcloud)
## Loading required package: RColorBrewer
library(tm)
## Loading required package: NLP
## 
## Attaching package: 'NLP'
## The following object is masked from 'package:ggplot2':
## 
##     annotate
library(syuzhet)




CONSUMER_SECRET <- "A6jY0TtdJOyMr5nLY2ANit2ZR4jDFs9RS0ne9XyIMGBeXcLyaP"
CONSUMER_KEY <- "FQ0dkU6x1qoS8JtVLzmFLvzVm"
ACCESS_SECRET <- "6fTc0JjSMd00trD8SRBr48UxYwzYnAT35rjkQ14C6XpU9"
ACCESS_TOKEN <- "1392870771601510404-J4pEYtExkissJaUOo0CFkVk8MtEnCW"

setup_twitter_oauth(consumer_key = CONSUMER_KEY,
                    consumer_secret = CONSUMER_SECRET,
                    access_token = ACCESS_TOKEN,
                    access_secret = ACCESS_SECRET)
## [1] "Using direct authentication"
encodeSentiment <- function(x) {
  if(x <= -0.5){
    "1) very negative"
  }else if(x > -0.5 & x < 0){
    "2) negative"
  }else if(x > 0 & x < 0.5){
    "4) positive"
  }else if(x >= 0.5){
    "5) very positive"
  }else {
    "3) neutral"
  }
}

tweets= searchTwitter ("Israel", n=5000, since = "2021-05-08", until = "2021-05-10", lang = "en")

tweetsDF <- twListToDF(tweets)

tweetSentiments <- get_sentiment (tweetsDF$text,method = "syuzhet")
tweets <- cbind(tweetsDF, tweetSentiments)


tweets$sentiment <- sapply(tweets$tweetSentiments,encodeSentiment)
head(tweets, n = 6)
##                                                                                                                                               text
## 1     RT @RebelGeek1111: Those who couldn't find beds to lay their covid positive a$$ in yesterday are also ready to stand with Israel today lmao…
## 2                                      RT @nishant8573: We are proud of India -Israel Friendship .  #IndiaStandsWithIsrael https://t.co/Q1qYpEHirP
## 3     RT @isgoodrum: I wonder why Chinese rocket debris landing harmlessly in the ocean got a million times more media coverage than an actual po…
## 4 RT @Athavulla_cfi: Masjidul aqsa is the integrity, dignity, pride &amp; purpose of life for all Muslims.The attack on Al Aqsa by colonialist Is…
## 5                                          RT @pukhtoon7070: Israel is not a country, but a terrorist camp. #BoycottIsrael https://t.co/YdwQ9f3r76
## 6                            BREAKING Israel's opposition leader tapped to form coalition that could end Netanyahu's rule… https://t.co/qMp4zlSPpI
##   favorited favoriteCount replyToSN             created truncated replyToSID
## 1     FALSE             0      <NA> 2021-05-09 23:59:59     FALSE       <NA>
## 2     FALSE             0      <NA> 2021-05-09 23:59:59     FALSE       <NA>
## 3     FALSE             0      <NA> 2021-05-09 23:59:59     FALSE       <NA>
## 4     FALSE             0      <NA> 2021-05-09 23:59:59     FALSE       <NA>
## 5     FALSE             0      <NA> 2021-05-09 23:59:58     FALSE       <NA>
## 6     FALSE             1      <NA> 2021-05-09 23:59:57      TRUE       <NA>
##                    id replyToUID
## 1 1391543504497627142       <NA>
## 2 1391543504376012801       <NA>
## 3 1391543504052973570       <NA>
## 4 1391543502140448771       <NA>
## 5 1391543500672471044       <NA>
## 6 1391543495903612929       <NA>
##                                                                           statusSource
## 1 <a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>
## 2 <a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>
## 3              <a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>
## 4 <a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>
## 5 <a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>
## 6   <a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>
##        screenName retweetCount isRetweet retweeted longitude latitude
## 1     arifmasood_           43      TRUE     FALSE        NA       NA
## 2 DESHDEEPAKSRIV9          107      TRUE     FALSE        NA       NA
## 3      kategodart         4149      TRUE     FALSE        NA       NA
## 4    SalmaraAnsar          309      TRUE     FALSE        NA       NA
## 5 Shehwar58914236          372      TRUE     FALSE        NA       NA
## 6  SUPERCOVFEFE72            1     FALSE     FALSE        NA       NA
##   tweetSentiments        sentiment
## 1            1.55 5) very positive
## 2            1.25 5) very positive
## 3            0.15      4) positive
## 4            0.50 5) very positive
## 5           -1.00 1) very negative
## 6           -0.75 1) very negative
qplot(tweets$tweetSentiments) + theme(legend.position="none")+
  xlab("Sentiment Score") +
  ylab("Number of tweets") + 
  ggtitle("Tweets by Sentiment Score") 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(tweets, aes(sentiment, fill = sentiment)) +
  geom_bar() + 
  theme(legend.position="none", axis.title.x = element_blank()) +
  ylab("Number of tweets") + 
  ggtitle("Tweets by Sentiment")

tweetSentiments <- get_nrc_sentiment(tweetsDF$text)
head(tweetSentiments)
##   anger anticipation disgust fear joy sadness surprise trust negative positive
## 1     0            1       0    0   0       0        0     0        0        0
## 2     0            1       0    0   2       0        0     2        0        2
## 3     1            0       1    0   0       0        0     0        1        1
## 4     1            0       0    1   1       0        0     2        1        3
## 5     1            0       1    1   0       1        1     0        1        0
## 6     1            0       0    1   0       0        0     2        1        2
tweetsDF$text[50]
## [1] "RT @Najeebu69418876: Jews are living in Israel under the mercy of Palestinians!! \n\nIsrael should stop their oppressions over the innocents.…"
tweets <- cbind(tweetsDF, tweetSentiments)
tweets[c(1:50),c(1, 17:26)]
##                                                                                                                                                            text
## 1                  RT @RebelGeek1111: Those who couldn't find beds to lay their covid positive a$$ in yesterday are also ready to stand with Israel today lmao…
## 2                                                   RT @nishant8573: We are proud of India -Israel Friendship .  #IndiaStandsWithIsrael https://t.co/Q1qYpEHirP
## 3                  RT @isgoodrum: I wonder why Chinese rocket debris landing harmlessly in the ocean got a million times more media coverage than an actual po…
## 4              RT @Athavulla_cfi: Masjidul aqsa is the integrity, dignity, pride &amp; purpose of life for all Muslims.The attack on Al Aqsa by colonialist Is…
## 5                                                       RT @pukhtoon7070: Israel is not a country, but a terrorist camp. #BoycottIsrael https://t.co/YdwQ9f3r76
## 6                                         BREAKING Israel's opposition leader tapped to form coalition that could end Netanyahu's rule… https://t.co/qMp4zlSPpI
## 7                  RT @IanByrneMP: The images we are seeing of worshippers attacked at al-Aqsa Mosque as Israel pushes ahead with plans to illegally evict Pal…
## 8              RT @Athavulla_cfi: Masjidul aqsa is the integrity, dignity, pride &amp; purpose of life for all Muslims.The attack on Al Aqsa by colonialist Is…
## 9                    RT @AbdulMu74859591: Israel is the Nation of the Murderers!! The world must join hands together to stop the oppressions from Israelites!!…
## 10                 @JenniferJJacobs @JakeSullivan46 Huh. The US is SO concerned that they are plan to send Israel their $3,800,000,000… https://t.co/zn4hj8bdmS
## 11             RT @segal_eran: Israel presents: How vaccines beat COVID-19 (for now)\n\nSince mid-Jan peak:\n\n99% fewer cases (now ~5 daily cases per million…
## 12                 @Kredo0 Clear Democrats hate Israel. Everyone knows these Arabs hate all Jewish people like NAZIs want to extermina… https://t.co/3MeYdIoHMe
## 13           RT @pfi_india: Your weapons are nothing in-front of this small Palestinian kids courage. 💪🏻\n\nWe repeat, Palestine will rise &amp; Israel will f…
## 14                        RT @haymarketbooks: Stand with Palestine. 🇵🇸 Support Boycott, Divestment, and Sanctions against the Apartheid State of Israel. ✊🏿✊🏽✊🏾
## 15                 RT @duneprince: Joe Biden "If I were a Jew I'd be a Zionist, my father pointed out I didn't need to be a jew to be a zionist. For I am. Isr…
## 16                 RT @kishineff: @marwilliamson @Gambit121 @AOC I want a copy of the letter to Joe Biden from 330 House members, including 125 Democrats, tha…
## 17                                                                   @tayyabmehar27 Yes\nSir All Indians are with you..\nTeach a lesson to Terrorists Israel...
## 18                 RT @ShahdabAbbasi: Israel was created by UN after world war 2 and its the total responsibility of the @UN to stop the Israel oppressions ov…
## 19                 Throwback to proposing a resolution to abolish Israel at Felsted MUN as a joke, but it ended up passing and I got a… https://t.co/DKvbmLITYq
## 20                 RT @ZukhimH: In cultivating relations with Israel, India should not turn its back on Palestinians. The Arab lands have been grabbed over th…
## 21             RT @aka_dpu: Israel stood beside India in it’s high &amp; lows. Even in this tough pandemic phase, they’re up for us sending supplies. It would…
## 22                 @5SOS Jerusalem is Palestine's capital\nNow in the Sheikh Jarah neighborhood, residents are being driven from their… https://t.co/QGGQ74s44U
## 23                 RT @salauddin_naick: This is called 'Imaan'- Muslims are sleeping in, to protect Masjid al Aqsa from Israeli forces. The second built mosqu…
## 24                  RT @MalarMurshida: The real face of terrorism - Israel! Palestinians were praying peacefully when they were attacked by the Israeli forces…
## 25                Apparently theres some arab revolt going on in Israel right now or something.\nI mean, if you didn't treat your neig… https://t.co/h0KVlGIXRe
## 26               RT @InbarCohen13: Palestinians Attacking Israeli Police Officers In Al-Aqsa Mosque \n\nI honestly can't believe how people still blame Israel…
## 27                 RT @Mohamme04313719: In cultivating relations with Israel, India should not turn its back on Palestinians. The Arab lands have been grabbed…
## 28                 RT @rafiqsarve: Israel is committing the crime of apartheid by seeking to maintain Jewish domination over Palestinians and its own Arab pop…
## 29              RT @palinfoen: Never again?! \nIsrael has been following you know who to the letter. \n#SaveSheikhJarrah #SheikhJarrah #Jerusalem \n#NeverAgai…
## 30                         CJ Werleman @cjwerleman\n·\n“Israel doesn’t have a history. Only a criminal record.” https://t.co/mBDKJSqYfd https://t.co/oxs8NVLUVr
## 31                         RT @Amanullah_23: Palestine will be Palestine not occupied Terrorist Israel🇻🇺🙏🏻\n\n#IndiaStandsWithPalestine https://t.co/n5thEW8y4d
## 32           RT @MomentumCV: FAKE\n#Channel5 spin about Russia meddling in UK/US politics. \n\nREALITY\nJoan Ryan MP.\nCrook and friend of Israel. \nSo we kno…
## 33 RT @ElyasThumbeSDPI: The Cowards\n........................\nYou Brute &amp; Coward Israel\nYou killed &amp; tortured Israel\nYou arson &amp; violate Israel…
## 34               RT @Arman_IYC: No 1 Trend in India with 421K Tweets sofar.\n\nThank you India. Shame on Israel. Palestinians are not alone. We call on everyo…
## 35                 RT @Remroum: My father is older than state of Israel. My family was ethnically cleansed long before 1967. Israel is a racist state that was…
## 36                 @Syrians4J @janinedigi @AOC @BernieSanders @ewarren @TimesofIsrael @POTUS @BarackObama Absolutely absurd comparison… https://t.co/NOBNoVitzV
## 37                 RT @SanaSaeed: There is something you can do to actively support Palestinians and put pressure on Israel to end the occupation, ethnic clea…
## 38           RT @Habeebinamdar: Palestine is dying to live. \nYour silence is criminal.\nThe least we can do is pray for them &amp; raise our voice against Is…
## 39                 RT @rafiqsarve: Israel is committing the crime of apartheid by seeking to maintain Jewish domination over Palestinians and its own Arab pop…
## 40                 RT @Altaf_Hosapete: Israel thinks they can defeat Palestine and get the country which isn't even theirs all for themselves! But little do t…
## 41         RT @ashish_mishraaa: *29 Years of Bilateral Relationship between India &amp; Israel*\n\nCurrently:\n\n☞INDIA is the LARGEST BUYER of ISRAELI Milita…
## 42                  RT @smilectics: during the Ferguson uprising, Palestinians on twitter recognized the brand of tear gas being used on us was the same exact…
## 43                                                                                        RT @Bilalrants: This laylatul qadr we pray for the downfall of Israel
## 44                RT @BadawiKaloti: It’s time which all world must stand against all the Racist and oppressive behavior to our people in Palestenian \n#فلسطين…
## 45                     RT @ShaikAejaz4: Israel ki najayez aulad ruk gai 184 pe,Real heroes still going on...\n#IndiaStandsWithPalestine https://t.co/aO02Pk3phy
## 46                 RT @Barahmeh: 90,000 Palestinians pledge in unison  to defend Al Aqsa and Jerusalem - a message of defiance that is reverberating around th…
## 47              RT @Remroum: A lot of "both sides" arguments today:\n\n~Only Palestinians are under military occupation\n\n~Only Palestinians have land stolen…
## 48                RT @SalmaNasreen4: I used to think Israel controlled Palastine.\nNow,I know Palastine is the only Arab country on which Israel doesn't contr…
## 49               RT @AnisPFI: Israel was created by Zionists and Europe by transporting Jews from Europe to Palestine\n\nPalestine belonged to Arabs and it wa…
## 50               RT @Najeebu69418876: Jews are living in Israel under the mercy of Palestinians!! \n\nIsrael should stop their oppressions over the innocents.…
##    anger anticipation disgust fear joy sadness surprise trust negative positive
## 1      0            1       0    0   0       0        0     0        0        0
## 2      0            1       0    0   2       0        0     2        0        2
## 3      1            0       1    0   0       0        0     0        1        1
## 4      1            0       0    1   1       0        0     2        1        3
## 5      1            0       1    1   0       1        1     0        1        0
## 6      1            0       0    1   0       0        0     2        1        2
## 7      1            0       0    0   0       1        0     0        1        1
## 8      1            0       0    1   1       0        0     2        1        3
## 9      0            0       0    0   0       0        0     1        0        1
## 10     0            1       0    1   0       1        0     0        0        0
## 11     0            1       0    0   0       0        0     0        0        0
## 12     1            0       1    1   0       1        0     0        1        0
## 13     0            0       0    0   0       0        0     0        1        1
## 14     0            0       0    0   0       0        0     0        2        0
## 15     0            0       0    0   0       0        0     1        0        0
## 16     0            1       0    0   0       0        0     0        1        1
## 17     0            1       0    0   1       0        1     3        0        3
## 18     0            0       0    1   0       0        0     0        1        0
## 19     1            0       0    0   0       0        0     0        2        0
## 20     0            0       0    0   0       0        0     0        0        0
## 21     0            0       0    1   0       2        0     0        2        1
## 22     0            1       0    0   0       0        0     0        0        0
## 23     0            0       0    0   0       0        0     0        0        1
## 24     1            0       1    1   0       1        0     1        1        1
## 25     2            1       1    1   1       1        2     1        2        1
## 26     3            0       2    2   0       1        1     1        2        1
## 27     0            0       0    0   0       0        0     0        0        0
## 28     2            0       0    1   0       1        1     0        3        0
## 29     0            1       0    0   0       0        0     0        0        0
## 30     1            0       1    1   0       0        0     0        1        0
## 31     1            0       1    1   0       1        1     0        1        0
## 32     1            0       0    0   1       0        0     1        2        1
## 33     2            0       1    3   0       2        0     0        3        0
## 34     0            0       1    1   0       1        0     0        1        1
## 35     0            1       0    0   0       1        0     1        0        0
## 36     0            0       0    0   0       0        0     0        1        0
## 37     0            0       0    0   0       0        0     0        1        1
## 38     2            1       2    3   1       1        1     1        2        1
## 39     2            0       0    1   0       1        1     0        3        0
## 40     0            0       0    0   0       0        0     0        1        0
## 41     0            0       0    0   0       0        0     0        0        0
## 42     1            1       0    1   0       0        0     0        1        0
## 43     0            1       0    2   1       1        1     1        1        1
## 44     1            1       1    1   0       1        0     0        1        0
## 45     0            0       0    0   0       0        0     1        0        1
## 46     1            0       1    2   1       0        0     1        1        3
## 47     2            0       0    1   0       0        0     0        1        2
## 48     0            0       0    0   0       0        0     0        0        0
## 49     0            0       0    0   0       0        0     0        0        0
## 50     0            0       0    0   0       0        0     0        0        1
sentimentTotals <- data.frame(colSums(tweets[,c(17:26)]))

names(sentimentTotals) <- "count"
sentimentTotals <- cbind("sentiment" = rownames(sentimentTotals), sentimentTotals)
rownames(sentimentTotals) <- NULL
sentimentTotals
##       sentiment count
## 1         anger  3081
## 2  anticipation  1141
## 3       disgust  1493
## 4          fear  3252
## 5           joy  1037
## 6       sadness  1781
## 7      surprise   748
## 8         trust  2200
## 9      negative  3814
## 10     positive  4094
ggplot(data = sentimentTotals, aes(x = sentiment, y = count)) +
  geom_bar(aes(fill = sentiment), stat = "identity") +
  theme(legend.position = "none") +
  xlab("Sentiment") + ylab("Total Count") + ggtitle("Total Sentiment Score for All Tweets")

install.packages('tinytex')
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
tinytex::install_tinytex()
## tlmgr option sys_bin ~/bin