我使用 twitteR 包从 twitter 中提取推文并将它们保存到文本文件中。
我已经对语料库进行了以下操作
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')
(使用 mc.cores=1 和 lazy=True 否则 mac 上的 R 会遇到错误)
tdm<-TermDocumentMatrix(xx)
但是这个词条文档矩阵有很多奇怪的符号,无意义的词等等。
如果一条推文是
RT @Foxtel: One man stands between us and annihilation: @IanZiering.
Sharknado‚Äã 3: OH HELL NO! - July 23 on Foxtel @SyfyAU
清理推文后,我只希望留下正确的完整英语单词,即一个没有其他所有内容的句子/短语(用户名、缩写词、网址)
例子:
One man stands between us and annihilation oh hell no on
(注意:tm 包中的转换命令只能去除停用词、标点空格和小写转换)
最佳答案
使用 gsub 和
stringr package
我已经找到了删除转推、对屏幕名称、主题标签、空格、数字、标点符号、网址的引用的部分解决方案。
clean_tweet = gsub("&", "", 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]*","")
引用:(斯坦顿 2013)
在执行上述任何操作之前,我使用以下命令将整个字符串折叠成一个长字符。
paste(mytweets, collapse=" ")
与 tm_map 转换相比,这个清理过程对我来说非常有效。
我现在只剩下一套合适的词和一些不合适的词。
现在,我只需要弄清楚如何删除不正确的英语单词。
可能我将不得不从单词词典中减去我的一组单词。
关于r - 如何清理 R 中的 Twitter 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31348453/