Scraping twitter data to visualize trending tweets in Kuala Lumpur

(Disclaimer: I’ve no grudge against python programming language per se. I think its equally great. In the following post, I’m merely recounting my experience.)

It’s been quite a while since I last posted. The reasons are numerous, notable being, unable to decide which programming language to choose for web data scraping. The contenders were data analytic maestro, R and data scraping guru, python. So, I decided to give myself some time to figure out which language will be best for my use case. My use case was, Given some search keywords, scrape twitter for related posts and visualize the result. First, I needed the live data. Again, I was at the cross-roads, “R or Python”. Apparently python has some great packages for twitter data streaming like twython,python-twitter, tweepy. Equivalent R libraries are twitteR,rwteet. I chose the rtweet package for data collection over python for following reasons;

  • I do not have to create a credential file (unlike in python) to log in to my twitter account. However, you do need to authenticate the twitter account when using the rtweet package. This authentication is done just once if using the rtweet package. Your twitter credentials will be stored locally.
  • Coding and code readability is far more easier as compared to python.
  • The rtweet package allows for multiple hash tags to be searched for.
  • To localize the data, the package also allows for specifying geographic coordinates.

So, using the following code snippet, I was able to scrape data. The code has following parts;

  1. A custom search for tweets function which will accept the search string. If search string is NULL, it will throw a message and stop, else it will search for hash tags specified in search string and return a data frame as output.

     > library(rtweet)
     > library(tidytext)
     > library(tidyverse)
     > library(stringr)
     > library(stopwords)
     > library(rtweet) # for search_tweets()
     # Create a function that will accept multiple hashtags and will search the twitter api for related tweets
    
     > search_tweets_queries 
  2. A data frame containing the search terms. Note, here my search hash-tags are KTM, MRT and monorail.

     # create data frame with query column
     > df_query 
  3. Once the data is collected, I’ll keep some selected columns only.

     # Select and keep only relevant columns
     > df_select_tweets%
       select(c(user_id,created_at,screen_name, !is.na(hashtags),text,
        source,display_text_width>0,lang,!is.na(place_name),
        !is.na(place_full_name),
        !is.na(geo_coords), !is.na(country), !is.na(location),
        retweet_count,account_created_at, account_lang, query)
      )
    
  4. Text mining: The collected data need to be cleaned. Therefore, I’ve used the basic gsub() function and str_replace_all() from the stringr library.

     # Saving the selected columns data
     > df_select_tweets_1 = data.frame(lapply(df_select_tweets, as.character), stringsAsFactors=FALSE)
     ### Text preprocessing
        
     # 1. Remove URL from text
     # collapse to long format
     > clean_tweet clean_tweet$text = gsub("&amp", "", clean_tweet$text)
     > clean_tweet$text = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "", clean_tweet$text)
     > clean_tweet$text = gsub("@\\w+", "", clean_tweet$text)
     > clean_tweet$text = gsub("[[:punct:]]", "", clean_tweet$text)
     > clean_tweet$text = gsub("[[:digit:]]", "", clean_tweet$text)
     > clean_tweet$text = gsub("http\\w+", "", clean_tweet$text)
     > clean_tweet$text = gsub("[ \t]{2,}", "", clean_tweet$text)
     > clean_tweet$text = gsub("^\\s+|\\s+$", "", clean_tweet$text)
    
     #get rid of unnecessary spaces
     > clean_tweet$text  clean_tweet$text clean_tweet$text clean_tweet$text  clean_tweet$text  clean_tweet$text 

    a. Next, I’ll use the tidytext library for token extraction

     # Unnest the tokens
     > df.clean%
       unnest_tokens(word, text)
        
     > clean_tweets clean_tweets word_freq %
       count(word, sort=TRUE)
     > word_freq 
    
     # A tibble: 5,291 x 2
        wordn
           
      1 mrt   596
      2 ktm   582
      3 ke455
      4 kl259
      5 ni251
      6 naik  221
      7 the   214
      8 at208
      9 sentral   195
     10 nak   193
     # ... with 5,281 more rows
    

    b. It should be noted, the national language of Malaysia is Bahasa Melayu (BM). To remove the stop words in BM, I’ve used the stopwords library. lots of stop words like the, and, to, a etc. Let’s remove the stop words. We can remove the stop words from our tibble with anti_join and the built-in stop_words data set provided by tidytext.

     > clean_tweets %>%
       # remove the stopwords in Bahasa Melayu (BM). Use `ms` for BM. See this reference for other language codes: https://en.wikipedia.org/wiki/ISO_639-1
       anti_join(get_stopwords(language="ms", source="stopwords-iso")) %>%
       # remove the stopwords in english
       anti_join(get_stopwords(language="en", source="stopwords-iso")) %>%
       count(word, sort=TRUE) %>%
       top_n(10) %>%
       ggplot(aes(word,n, fill=word))+
       geom_bar(stat = "identity")+
       xlab(NULL)+
       ylab(paste('Word count'))+
       ggtitle(paste('Most common words in tweets')) +
       theme(legend.position="none") +
       theme_minimal()+
       coord_flip()
    
  5. Finally, I present a basic bar plot to show the trending words.

    Barplot: Trending twitter words in kuala lumpur, malaysia

Area’s of further improvement

  • How to extract tweets within a given time range?

To leave a comment for the author, please follow the link and comment on their blog: Stories Data Speak.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (, Boxplots, maps, ), programming (, Sweave, , , Eclipse, , , Web Scraping) statistics (, , time series, ) and more…

If you got this far, why not subscribe for updatesfrom the site? Choose your flavor: e-mail, , RSS, or facebook

Advertisements