使用 tidytext 删除停用词

Removing stop words with tidytext

使用 tidytext,我有这个代码:

data(stop_words)
tidy_documents <- tidy_documents %>%
      anti_join(stop_words)

我希望它使用包中内置的停用词将名为 tidy_documents 的数据帧写入同名数据帧,但如果它们在 stop_words 中,则删除这些词。

我收到这个错误:

错误:没有公共变量。请指定 by 参数。 回溯:

1. tidy_documents %>% anti_join(stop_words)
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3. eval(quote(`_fseq`(`_lhs`)), env, env)
4. eval(expr, envir, enclos)
5. `_fseq`(`_lhs`)
6. freduce(value, `_function_list`)
7. withVisible(function_list[[k]](value))
8. function_list[[k]](value)
9. anti_join(., stop_words)
10. anti_join.tbl_df(., stop_words)
11. common_by(by, x, y)
12. stop("No common variables. Please specify `by` param.", call. = FALSE)

tidy_documentstop_words 都在名为 word 的列下列出了单词列表;但是,列是倒置的:在 stop_words 中,它是第一列,而在您的数据集中,它是第二列。这就是为什么该命令无法 "match" 两列并比较单词的原因。试试这个:

tidy_document <- tidy_document %>% 
      anti_join(stop_words, by = c("word" = "word"))

by 命令强制脚本比较称为 word 的列,而不考虑它们的位置。

您可以使用更简单的 filter() 来避免像这样使用令人困惑的 anti_join() 函数:

tidy_documents <- tidy_documents %>%
  filter(!word %in% stop_words$word)