【发布时间】:2018-09-28 16:53:02
【问题描述】:
我之前问过一个问题,如何通过保持原始格式从字符向量中的停止列表中删除单词。任务是删除向量“words”中的“words_to_remove”单词。 我接受了这个解决方案:
words_to_remove = c("the", "This")
pattern <- paste0("\\b", words_to_remove, "\\b", collapse="|")
words = c("the", "The", "Intelligent", "this", "This")
res <- grepl(pattern, words, ignore.case=TRUE)
words[!res]
现在我遇到的问题是“单词”条目中有多个单词。如果包含停用词,则删除整个条目。
words = c("the", "The Book", "Intelligent", "this", "This")
我收到输出
[1] "Intelligent"
但我希望它是
[1] "Book" "Intelligent"
这可能吗?
【问题讨论】:
-
使用
gsub而不是grepl。用空格替换单词。 -
试试
gsub(paste(words_to_remove, collapse = '|'), '', words, ignore.case = TRUE) -
@WinterMensch,请参阅我在您上一个问题中对 Tim 的回答的第一条评论。
标签: r text text-mining stop-words