Processing twitter text

Análisis de datos de redes sociales en R

Vivek Vijayaraghavan

Data Science Coach

Lesson overview

  • Why process tweet text?
  • Steps in processing tweet text
    • removing redundant information
    • Converting text into a corpus
    • Removing stop words
Análisis de datos de redes sociales en R

Why process tweet text?

  • Tweet text is unstructured, noisy, and raw
  • Contains emoticons, URLs, numbers
  • Clean text required for analysis and reliable results
Análisis de datos de redes sociales en R

Steps in text processing

Step1: Remove redundant information

Análisis de datos de redes sociales en R

Steps in text processing

Step2: Convert text to a corpus

Análisis de datos de redes sociales en R

Steps in text processing

Step3: Convert to lowercase

Análisis de datos de redes sociales en R

Steps in text processing

Step4: Remove stop words

Análisis de datos de redes sociales en R

Extract tweet text

# Extract 1000 tweets on "Obesity" in English and exclude retweets
tweets_df <- search_tweets("Obesity", n = 1000, include_rts = F, lang = 'en')
# Extract the tweet texts and save it in a data frame
twt_txt <- tweets_df$text
Análisis de datos de redes sociales en R

Extract tweet text

head(twt_txt, 3)
[1] "@WeeaUwU for real, obesity should not be praised like it is in today's society" 

[2] "Great work by @DosingMatters in @AJHPOfficial on \"Vancomycin Vd estimation in 
adults with class III obesity\". As we continue to study/learn more about dosing in 
large body weight pts, we see that it's not a simple, one size, one level estimate 
that works https://t.co/KkYPqS6JzG" 

[3] "The Scottish Government have an ambition to halve childhood obesity by 2030. 
This means reducing obesity prevalence in 2-15yo children in Scotland to 7%. 
\n\n\U0001f449 In 2018, this figure was 16%\n\nFind out more in our latest blog: 
https://t.co/FWp56QWjQc https://t.co/XBK8Je7F1A"
Análisis de datos de redes sociales en R

Removing URLs

# Remove URLs from the tweet text
library(qdapRegex)
twt_txt_url <- rm_twitter_url(twt_txt)
Análisis de datos de redes sociales en R

Removing URLs

twt_txt_url[1:3]
[1] "@WeeaUwU for real, obesity should not be praised like it is in today's society"

[2] "Great work by @DosingMatters in @AJHPOfficial on \"Vancomycin Vd estimation in  adults 
with class III obesity\". As we continue to study/learn more about dosing in large body 
weight pts, we see that it's not a simple, one size, one level estimate that works"

[3] "The Scottish Government have an ambition to halve childhood obesity by 2030. 
This means reducing obesity prevalence in 2-15yo children in Scotland to 7%. 
\U0001f449In 2018, this figure was 16% Find out more in our latest blog:"  
Análisis de datos de redes sociales en R

Special characters, punctuation & numbers

# Remove special characters, punctuation & numbers
twt_txt_chrs  <- gsub("[^A-Za-z]", " ", twt_txt_url)
Análisis de datos de redes sociales en R

Special characters, punctuation & numbers

twt_txt_chrs[1:3]
[1] " WeeaUwU for real  obesity should not be praised like it is in today s society"

[2] "Great work by  DosingMatters in  AJHPOfficial on  Vancomycin Vd estimation in 
adults with class III obesity   As we continue to study learn more about dosing in 
large body weight pts  we see that it s not a simple  one size  one level estimate 
that works"

[3] "The Scottish Government have an ambition to halve childhood obesity by       This 
means reducing obesity prevalence in     yo children in Scotland to       In       this 
figure was     Find out more in our latest blog "   
Análisis de datos de redes sociales en R

Convert to text corpus

# Convert to text corpus
library(tm)
twt_corpus <- twt_txt_chrs %>% 
                VectorSource() %>% 
                Corpus()
twt_corpus[[3]]$content
[1] "The Scottish Government have an ambition to halve childhood obesity by       
This means reducing obesity prevalence in     yo children in Scotland to       In  
     this figure was     Find out more in our latest blog "
Análisis de datos de redes sociales en R

Convert to lowercase

  • A word should not be counted as two different words if the case is different
# Convert text corpus to lowercase
twt_corpus_lwr <- tm_map(twt_corpus, tolower) 
twt_corpus_lwr[[3]]$content
[1] "the scottish government have an ambition to halve childhood obesity by     this 
means reducing obesity prevalence in     yo children in scotland to       in    this 
figure was     find out more in our latest blog "
Análisis de datos de redes sociales en R

What are stop words?

  • Stop words are commonly used words like a, an, and but
# Common stop words in English
stopwords("english")

Default stop words in English

Análisis de datos de redes sociales en R

Remove stop words

  • Stop words need to be removed to focus on the important words
# Remove stop words from corpus
twt_corpus_stpwd <- tm_map(twt_corpus_lwr, removeWords, stopwords("english"))
twt_corpus_stpwd[[3]]$content

[1] " scottish government   ambition  halve childhood obesity         means 
reducing obesity prevalence      yo children  scotland                figure      
find     latest blog "
Análisis de datos de redes sociales en R

Remove additional spaces

  • Remove additional spaces to create a clean corpus
# Remove additional spaces
twt_corpus_final <- tm_map(twt_corpus_stpwd, stripWhitespace)
twt_corpus_final[[3]]$content
[1] " scottish government ambition halve childhood obesity means reducing obesity 
prevalence yo children scotland figure find latest blog "
Análisis de datos de redes sociales en R

Let's practice!

Análisis de datos de redes sociales en R

Preparing Video For Download...