我正在使用一个演讲时间跨度为数年的语料库(汇总到人年级别)。我想删除一年中出现次数少于 4 次的单词(不是为整个语料库删除它,而是只删除它不符合阈值的那一年)。
我尝试了以下方法:
DT$text <- ifelse(grepl("1998", DT$session), mgsub(DT$text, words_remove_1998, ""), DT$text)
and
DT$text <- ifelse(grepl("1998", DT$session), str_remove_all(DT$text, words_remove_1998), DT$text)
and
DT$text <- ifelse(grepl("1998", DT$session), removeWords(DT$text, words_remove_1998), DT$text)
and
DT$text <- ifelse(grepl("1998", DT$session), drop_element(DT$text, words_remove_1998), DT$text)
然而,似乎没有一个工作。Mgsub 只是用 "" 代替 1998 的整个语音,而其他选项则给出错误消息。removeWords 不起作用的原因是我的 words_remove_1998 向量太大。我试图拆分单词向量并遍历单词(参见下面的代码),但 R 似乎不喜欢这样(永远运行)。
group <- 100
n <- length(words_remove_1998)
r <- rep(1:ceiling(n/group),each=group)[1:n]
d <- split(words_remove_1998,r)
for (i in 1:length(d)) {
DT$text <- ifelse(grepl("1998", DT$session), removeWords(DT$text, c(paste(d[[i]]))), DT$text)
}
关于如何解决这个问题的任何建议?
谢谢您的帮助!
可重现的例子:
text <- rbind(c("i like ice cream"), c("banana ice cream is my favourite"), c("ice cream is not my thing"))
name <- rbind(c("Arnold Ford"), c("Arnold Ford"), c("Leslie King"))
session <- rbind("1998", "1999", "1998")
DT <- cbind(name, session, text)
words_remove_1998 <- c("like", "ice", "cream")
newtext <- rbind(c("i"), c("banana ice cream is my favourite"), c("is not my thing"))
DT <- cbind(DT, newtext)
我想要删除的真实词向量包含 30k 个元素。