如何清理R中的twitter数据?

如何清理R中的twitter数据?,r,twitter,text-mining,data-cleaning,R,Twitter,Text Mining,Data Cleaning,我使用twitter包从twitter中提取tweet,并将其保存到文本文件中 我已经在语料库上完成了以下工作 xx<-tm_map(xx,removeNumbers, lazy=TRUE, 'mc.cores=1') xx<-tm_map(xx,stripWhitespace, lazy=TRUE, 'mc.cores=1') xx<-tm_map(xx,removePunctuation, lazy=TRUE, 'mc.cores=1') xx<-tm_map(xx,

我使用twitter包从twitter中提取tweet,并将其保存到文本文件中

我已经在语料库上完成了以下工作

xx<-tm_map(xx,removeNumbers, lazy=TRUE, 'mc.cores=1')
xx<-tm_map(xx,stripWhitespace, lazy=TRUE, 'mc.cores=1')
xx<-tm_map(xx,removePunctuation, lazy=TRUE, 'mc.cores=1')
xx<-tm_map(xx,strip_retweets, lazy=TRUE, 'mc.cores=1')
xx<-tm_map(xx,removeWords,stopwords(english), lazy=TRUE, 'mc.cores=1')
清理tweet后,我只希望留下正确完整的英语单词,即一个没有其他内容(用户名、缩写词、URL)的句子/短语

例如:

One man stands between us and annihilation oh hell no on 

(注意:tm软件包中的转换命令只能删除停止字、标点符号空格以及转换为小写)

要删除URL,可以尝试以下操作:

removeURL <- function(x) gsub("http[[:alnum:]]*", "", x)
xx <- tm_map(xx, removeURL)
使用gsub和

stringr包

我已经找到了部分解决方案来删除转发、对屏幕名称的引用、哈希标记、空格、数字、标点符号和URL

  clean_tweet = gsub("&amp", "", unclean_tweet)
  clean_tweet = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "", clean_tweet)
  clean_tweet = gsub("@\\w+", "", clean_tweet)
  clean_tweet = gsub("[[:punct:]]", "", clean_tweet)
  clean_tweet = gsub("[[:digit:]]", "", clean_tweet)
  clean_tweet = gsub("http\\w+", "", clean_tweet)
  clean_tweet = gsub("[ \t]{2,}", "", clean_tweet)
  clean_tweet = gsub("^\\s+|\\s+$", "", clean_tweet) 
参考:(希克斯,2014) 在上述之后 我做了下面的工作

 #get rid of unnecessary spaces
clean_tweet <- str_replace_all(clean_tweet," "," ")
# Get rid of URLs
clean_tweet <- str_replace_all(clean_tweet, "http://t.co/[a-z,A-Z,0-9]*{8}","")
# Take out retweet header, there is only one
clean_tweet <- str_replace(clean_tweet,"RT @[a-z,A-Z]*: ","")
# Get rid of hashtags
clean_tweet <- str_replace_all(clean_tweet,"#[a-z,A-Z]*","")
# Get rid of references to other screennames
clean_tweet <- str_replace_all(clean_tweet,"@[a-z,A-Z]*","")   
#去掉不必要的空格

clean_tweet对我来说,出于某种原因,这段代码不起作用-

# Get rid of URLs
clean_tweet <- str_replace_all(clean_tweet, "http://t.co/[a-z,A-Z,0-9]*{8}","")
所以,我用了

clean_tweet4 <- str_replace_all(clean_tweet3, "https://t.co/[a-z,A-Z,0-9]*","")
clean_tweet5 <- str_replace_all(clean_tweet4, "http://t.co/[a-z,A-Z,0-9]*","")

clean_tweet4代码执行一些基本的清理

转换成小写
df
library(tidyverse)
清除推文%
#删除URL
str|u remove|u all(“?(f|ht)(tp)(s?)(:/)(.*)[.|/](.*))%>%
#将“&”字符引用替换为“和”
str_替换所有(“&;”、“和”)%>%
#使用标准字符类删除puntucation
str_remove_all(“[[:punct:][]”)%>%
#从转发开始删除“RT:”
str_删除所有(“^RT:?”)%>%
#删除提及的内容,例如“@my_account”
str_remove_all(“@[[:alnum:]+”)%>%
#删除hashtag
str_remove_all(“#[:alnum:][]+”)%>%
#用空格替换任何换行符
str\u replace\u all(“\\\n”和“)%>%
#把每件事都写得小写
str_至_下部()%>%
#删除文本周围的任何尾随空格
str_trim(“两者”)
}
推文%>%clean_推文

那么,
sharkanado
foxtel
就可以了,因为它们不是“合适的”英语单词……如果你使用
xx,你会看到任何改进吗?精确的语法可能取决于你安装的
tm
软件包的版本号。这非常有效,但是,如果不想覆盖变量,请确保不要在参数中使用
clean\u tweet
!还要确保顺序正确。如果您先删除提及的内容,然后进行RT检查(
clean_tweet)是否有可能获得关于每个步骤中删除的内容的评论?我目前正在学习Regex,但在识别某些表达式时仍然存在问题。Thanks@k3r0-我在每个步骤中都添加了注释,以更清楚地说明它在做什么我读了一些关于它的内容,并找出了其中的一些,但不是全部ar执行函数,这也是一个很好的学习。谢谢!
# Get rid of URLs
clean_tweet <- str_replace_all(clean_tweet, "http://t.co/[a-z,A-Z,0-9]*{8}","")
Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement),  : 
 Syntax error in regexp pattern. (U_REGEX_RULE_SYNTAX)
clean_tweet4 <- str_replace_all(clean_tweet3, "https://t.co/[a-z,A-Z,0-9]*","")
clean_tweet5 <- str_replace_all(clean_tweet4, "http://t.co/[a-z,A-Z,0-9]*","")
df <- tm_map(df, tolower)  
df <- tm_map(df, removePunctuation)
df <- tm_map(df, removeNumbers)
df <- tm_map(df, removeWords, stopwords('english'))
removeURL <- function(x) gsub('http[[:alnum;]]*', '', x)
    library(tidyverse)    
    
    clean_tweets <- function(x) {
                x %>%
                        # Remove URLs
                        str_remove_all(" ?(f|ht)(tp)(s?)(://)(.*)[.|/](.*)") %>%
                        # Replace "&" character reference with "and"
                        str_replace_all("&amp;", "and") %>%
                        # Remove puntucation, using a standard character class
                        str_remove_all("[[:punct:]]") %>%
                        # Remove "RT: " from beginning of retweets
                        str_remove_all("^RT:? ") %>%
                        # Remove mentions e.g. "@my_account"
                        str_remove_all("@[[:alnum:]]+") %>%
                        # Remove hashtags
                        str_remove_all("#[[:alnum:]]+") %>%
                        # Replace any newline characters with a space
                        str_replace_all("\\\n", " ") %>%
                        # Make everything lowercase
                        str_to_lower() %>%
                        # Remove any trailing whitespace around the text
                        str_trim("both")
        }

    tweets %>% clean_tweets