首页 > 解决方案 > 如何删除不同版本的停用词

问题描述

我用这种方式从文本中删除停用词

dfm <- 
    tokens(df$text,
           remove_punct = TRUE, 
           remove_numbers = TRUE, 
           remove_symbols = TRUE) %>%
    tokens_remove(pattern = stopwords(source = "smart")) %>%
      tokens_wordstem()

但是在结果中我发现有这样的停用词:

dont

有没有办法在不使用自定义停用词列表的情况下删除它们?

标签: rquanteda

解决方案


当您说“删除它们”时,我假设您的意思是dont从标记中删除,而现有的停用词列表仅删除don’t. (尽管从您的问题或某些答案的解释中,这一点并不完全清楚。)quanteda框架中存在两个简单的解决方案。

首先,您可以在tokens_remove()调用中附加额外的移除模式。

其次,您可以处理由返回的字符向量stopwords()以包含不带撇号的版本。

插图:

library("quanteda")
## Package version: 1.5.1

toks <- tokens("I don't know what I dont or cant know.")

# original
tokens_remove(toks, c(stopwords("en")))
## tokens from 1 document.
## text1 :
## [1] "know" "dont" "cant" "know" "."

# manual addition
tokens_remove(toks, c(stopwords("en"), "dont", "cant"))
## tokens from 1 document.
## text1 :
## [1] "know" "know" "."

# automatic addition to stopwords
tokens_remove(toks, c(
  stopwords("en"),
  stringi::stri_replace_all_fixed(stopwords("en"), "'", "")
))
## tokens from 1 document.
## text1 :
## [1] "know" "know" "."    

推荐阅读