Elaborazione del testo di Twitter

Analisi dei dati dei social media in R

Vivek Vijayaraghavan

Data Science Coach

Panoramica della lezione

  • Perché elaborare i tweet?
  • Fasi di elaborazione del testo
    • rimuovere informazioni ridondanti
    • convertire il testo in un corpus
    • rimuovere le stop word
Analisi dei dati dei social media in R

Perché elaborare i tweet?

  • Il testo dei tweet è non strutturato, rumoroso e grezzo
  • Contiene emoticon, URL, numeri
  • Serve testo pulito per analisi e risultati affidabili
Analisi dei dati dei social media in R

Fasi del text processing

Passo 1: Rimuovi informazioni ridondanti

Analisi dei dati dei social media in R

Fasi del text processing

Passo 2: Converti il testo in un corpus

Analisi dei dati dei social media in R

Fasi del text processing

Passo 3: Converti in minuscolo

Analisi dei dati dei social media in R

Fasi del text processing

Passo 4: Rimuovi le stop word

Analisi dei dati dei social media in R

Estrai il testo dei tweet

# 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
Analisi dei dati dei social media in R

Estrai il testo dei tweet

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"
Analisi dei dati dei social media in R

Rimozione degli URL

# Remove URLs from the tweet text
library(qdapRegex)
twt_txt_url <- rm_twitter_url(twt_txt)
Analisi dei dati dei social media in R

Rimozione degli URL

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:"  
Analisi dei dati dei social media in R

Caratteri speciali, punteggiatura e numeri

# Remove special characters, punctuation & numbers
twt_txt_chrs  <- gsub("[^A-Za-z]", " ", twt_txt_url)
Analisi dei dati dei social media in R

Caratteri speciali, punteggiatura e numeri

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 "   
Analisi dei dati dei social media in R

Converti in un corpus testuale

# 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 "
Analisi dei dati dei social media in R

Converti in minuscolo

  • Una parola non va conteggiata come due solo perché ha maiuscole diverse
# 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 "
Analisi dei dati dei social media in R

Cosa sono le stop word?

  • Le stop word sono parole comuni come a, an, and, but
# Common stop words in English
stopwords("english")

Stop word predefinite in inglese

Analisi dei dati dei social media in R

Rimuovi le stop word

  • Le stop word vanno rimosse per concentrarsi su quelle importanti
# 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 "
Analisi dei dati dei social media in R

Rimuovi spazi aggiuntivi

  • Rimuovi gli spazi extra per ottenere un corpus pulito
# 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 "
Analisi dei dati dei social media in R

Ayo berlatih!

Analisi dei dati dei social media in R

Preparing Video For Download...