我之前问过如何通过保留原始格式从字符向量中的停止列表中删除单词的问题 . 任务是在矢量“单词”中删除“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"
这可能吗?
2 回答
将模式更改为
包括字符串标记的开始和结束,而不仅仅是单词边界 . 其余代码应该可以正常使用这一个更改 .
您可以尝试使用
gsub
,即