The latest sentiment from Ashraf Ghani, after admitting defeat to the Taliban

1. Library use

1.1 Connect to Twitter

Enter the authentication details below
Authenticate with Twitter
api_key <- "your_api_key"
api_secret <- "your_api_secret"
access_token <- "your_access_token"
access_token_secret <- "your_access_token_secret"

2.Tweet of Ashraf Ghani

Scraping twitter of AsHraf Ghani use twitter account "@ashrafghani"
In this case we only scrapt 500 tweet as for saving memory
Saving data in directory as file text

3.Perform sentiment analysis

Some text need cleaning before the next process, then saving file as tex.file Please note that the classifying the polarity and emotion of the tweets may take a few minutes

some_txt <- read.table("d:/data/asr_some_txt.text")
# classify emotion
class_emo = classify_emotion(some_txt, algorithm="bayes", prior=1.0)
## Warning in TermDocumentMatrix.SimpleCorpus(x, control): custom functions are
## ignored
# get emotion best fit
emotion = class_emo[,7]
# substitute NA's by "unknown"
emotion[is.na(emotion)] = "unknown"
# classify polarity
class_pol = classify_polarity(some_txt, algorithm="bayes")
## Warning in TermDocumentMatrix.SimpleCorpus(x, control): custom functions are
## ignored
# get polarity best fit
polarity = class_pol[,4]

4.Data frame the results

4.1 Data frame with results

sent_df = data.frame(text=some_txt, emotion=emotion,
polarity=polarity, stringsAsFactors=FALSE)

4.2 Sort data frame

sent_df = within(sent_df, emotion, polarity)

6. Plot

6.1 Emotions Distribution

6.2 Polarity Distribution

7. Analisa

Dari grafik 1 terlihat, emotion "joy" jauh lebih banyak dari pada emotion "anger" dan "disgust",
dari grafik 2 terlihat 70% sentimen positif, 20% sentimen negatif dan sisanya 10 % netral, menunjukkan 
bahwa sampai saat ini Presiden Afganistan Ashraf Ghani masih disegani.