【发布时间】:2021-08-23 00:03:13
【问题描述】:
我正在学习文本挖掘和 rTweet,目前我正在集思广益,寻找清理从推文中获得的文本的最简单方法。我一直在使用这个link推荐的方法 删除 URL,删除除英文字母或空格以外的任何内容,删除停用词,删除多余的空格,删除数字,删除标点符号。
此方法同时使用 gsub 和 tm_map(),我想知道是否可以使用 stringr 简化清洁过程,以便将它们简单地添加到清洁管道中。 I saw an answer in the site 推荐了以下函数,但由于某种原因我无法运行它。
clean_tweets <- function(x) {
x %>%
str_remove_all(" ?(f|ht)(tp)(s?)(://)(.*)[.|/](.*)") %>%
str_replace_all("&", "and") %>%
str_remove_all("[[:punct:]]") %>%
str_remove_all("^RT:? ") %>%
str_remove_all("@[[:alnum:]]+") %>%
str_remove_all("#[[:alnum:]]+") %>%
str_replace_all("\\\n", " ") %>%
str_to_lower() %>%
str_trim("both")
}
清洁解决方案:
tweetsClean <- df %>%
mutate(clean = clean_tweets(text))
最后,是否可以保留 emoji 以计算使用 emoji 的频率并可能为每个 emoji 创建自定义情绪?
表情符号解决方案:
library(emo)
TopEmoji <- tweetsClean %>%
mutate(emoji = ji_extract_all(text)) %>%
unnest(cols = c(emoji)) %>%
count(emoji, sort = TRUE) %>%
top_n(5)
一旦文本值是干净的,我的过程是选择相关列,添加行号以保留每个单词所属的推文,并取消嵌套标记
tweetsClean <- tweets %>%
select(created_at,text) %>%
mutate(linenumber = row_number()) %>%
select(linenumber,everything()) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words)
之后我分配了所需的情绪,并根据使用 AFINN 获得的情绪的总和为每一行分配一个值:
sentiment_bing <- get_sentiments("bing")
sentiment_AFINN <- get_sentiments("afinn")
tweetsValue <- tweetsClean %>%
inner_join(sentiment_bing) %>%
inner_join(sentiment_AFINN) %>%
group_by(linenumber,created_at) %>%
mutate(TweetValue = sum(value))
感谢指点!
测试数据:
df <- structure(list(created_at = structure(c(1622854597, 1622853904,
1622853716, 1622778852, 1622448379, 1622450951, 1622777623, 1622853561,
1622466544, 1622853192), tzone = "UTC", class = c("POSIXct",
"POSIXt")), text = c("@elonmusk can the dogefather ride @CumRocketCrypto into the night. #SpaceX @dogecoin https://twitter.com/",
"@CryptoCrunchApp @CumRocketCrypto @vergecurrency @InuSanshu @Mettalex @UniLend_Finance @NuCypher @Chiliz @JulSwap @CurveFinance @PolyDoge Wrong this twitt shansu",
"9am AEST Sunday morning!!!\nI will be hosting on the @CumRocketCrypto twitch channel!\n\nSo cum say Hi! https://twitter.com/",
"@SamInCrypt1 @IamMars34147875 @DylanMcKitten @elonmusk @CumRocketCrypto Cumrocket <U+0001F4A6> https://twitter.com/",
"@DK19663019 @CumRocketCrypto Oh hey, that's me! Did you grab one?",
"@DK19663019 @CumRocketCrypto Thank you! <U+2764><U+FE0F>", "@CumRocketInfo @elonmusk @CumRocketCrypto Maybe he'd like to meet the CUMrocket models? https://twitter.com/",
"@AerotyneToken @CumRocketCrypto Is there a way to make sure ones wallet ID is on the list?",
"@AerotyneToken @CumRocketCrypto Does one have to attend the giveaway stream, or just hold 0.2 BNB of #CUMMIES and #ATYNE?\nAnd what happens if I bought about 0.2BNB each and the BNB price rises? Do I have to check every day if they're still worth at least 0.2?",
"@Don_Santino1 @brandank_cr @PAWGcoinbsc @Tyga @CumRocketCrypto Massive bull flag. 10x is imminent!"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
【问题讨论】: