jessicacardoso

basic_preprocess

Nov 21st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.89 KB | None | 0 0
  1. library(dplyr)
  2. library(purrr)
  3. library(stringr)
  4. library(stringi)
  5. library(magrittr)
  6.  
  7. clean_tweets <- function(df){
  8.   #Preprocessing
  9.   url_pattern <- "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
  10.   # Get rid of URLs
  11.   df$clean_tweet <- str_replace_all(df$text,url_pattern, " ")
  12.   # Take out retweet header, there is only one
  13.   df$clean_tweet <- str_replace(df$clean_tweet,"(RT|via)((?:\\b\\W*@\\w+)+)"," ")
  14.   # Get rid of hashtags
  15.   df$clean_tweet <- str_replace_all(df$clean_tweet,"#\\S+"," ")
  16.   # Get rid of references to other screennames
  17.   df$clean_tweet <- str_replace_all(df$clean_tweet,"@\\S+"," ")
  18.   #remover emojis
  19.   df$clean_tweet <- str_replace_all(df$clean_tweet,"\\p{So}|\\p{Cn}", " ")
  20.   #remover simbolos de comparação > <
  21.   df$clean_tweet <- str_replace_all(df$clean_tweet,"&\\S+", " ")
  22.   #remover quebra de linhas
  23.   df$clean_tweet = str_replace_all(df$clean_tweet, "[\r\n]" , " ")
  24.   #remover acentuação e caracteres estranhos
  25.   df$clean_tweet <- stri_trans_general(df$clean_tweet, "Latin-ASCII")
  26.   df$clean_tweet <- iconv(df$clean_tweet, "latin1", "ASCII", sub="")
  27.   #get rid of unnecessary spaces
  28.   df$clean_tweet <- str_replace_all(df$clean_tweet,"\\s+"," ")
  29.   df$clean_tweet <- str_replace_all(df$clean_tweet, "^\\s+|\\s+$", "")
  30.   #minusculas
  31.   df$clean_tweet <- tolower(df$clean_tweet)
  32.   df
  33. }
  34.  
  35.  
  36. ###---- Pré-processamento básico------
  37.  
  38. tweets_1o_turno <- readRDS("data/tweets_1o_turno.rds")
  39. tweets_2o_turno <- readRDS("data/tweets_2o_turno.rds")
  40.  
  41. tweets_1o_turno <- tweets_1o_turno[!duplicated(tweets_1o_turno),]
  42. tweets_2o_turno <- tweets_2o_turno[!duplicated(tweets_2o_turno),]
  43.  
  44. tweets_1o_turno <- clean_tweets(tweets_1o_turno)
  45. tweets_2o_turno <- clean_tweets(tweets_2o_turno)
  46.  
  47. saveRDS(tweets_1o_turno, 'data/tweets_1o_turno_without_duplicates.rds')
  48. saveRDS(tweets_2o_turno, 'data/tweets_2o_turno_without_duplicates.rds')
Add Comment
Please, Sign In to add comment