【问题标题】:Remove words from stopword list从停用词列表中删除单词
【发布时间】: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


【解决方案1】:

您可以尝试使用gsub,即

v1 <- gsub(paste(words_to_remove, collapse = '|'), '', words, ignore.case = TRUE)

#Tidy up your output

trimws(v1)[v1 != '']
#[1] "Book"        "Intelligent"

【讨论】:

    【解决方案2】:

    把图案改成

    pattern <- paste0("^", words_to_remove, "$", collapse="|")
    

    包括字符串标记的开始和结束,而不仅仅是单词边界。您的其余代码应该可以正常使用这一更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-02
      • 2019-01-03
      • 2021-12-29
      • 2021-12-22
      • 1970-01-01
      • 2016-09-25
      • 2019-09-10
      • 1970-01-01
      相关资源
      最近更新 更多