【问题标题】:Specific list of stopwords quanteda停用词的具体列表 quanteda
【发布时间】:2019-01-14 23:07:23
【问题描述】:

我想使用 quanteda 删除包含停用词的特定列表。

我用这个:

df <- data.frame(data = c("Here is an example text and why I write it", "I can explain and here you but I can help as I would like to help"))
mystopwords <- c("is","an")
corpus<- dfm(tokens_remove(tokens(df$data, remove_punct = TRUE, remove_numbers = TRUE, remove_symbols = TRUE), remove = c(stopwords(language = "el", source = "misc"), mystopwords), ngrams = c(4,6)))

但我收到此错误:

> Error in tokens_select(x, ..., selection = "remove") : 
  unused arguments (remove = c(stopwords(language = "en", source = "misc"), stopwords1), ngrams = c(4, 6))

在 quanteda 中使用 mystopwords 列表的正确方法是什么?

【问题讨论】:

    标签: r quanteda


    【解决方案1】:

    基于@phiver 的回答,这是删除 quanteda 中特定标记的标准方法。没有必要使用stopwords(),因为您提供了要删除的标记向量,patterns 参数可以采用向量,但请改用valuetype = 'fixed'

    我使用 dplyr 来提高代码的可读性,但您不必这样做。

    library(quanteda)
    library(dplyr)
    df <- data.frame(data = c("Here is an example text and why I write it", 
                              "I can explain and here you but I can help as I would like to help"),
                     stringsAsFactors = FALSE)
    
    mystopwords <- c("is","an")
    corpus <- 
      tokens(df$data,
             remove_punct = TRUE, 
             remove_numbers = TRUE, 
             remove_symbols = TRUE) %>%
      tokens_remove(pattern = mystopwords,
                    valuetype = 'fixed') %>%
      dfm(ngrams = c(4,6))
    

    【讨论】:

      【解决方案2】:

      这会奏效。首先,我在 data.frame 中添加了stringAsFactors = FALSE。提供给tokens 的文本必须是字符向量,而不是因子。接下来,我从您的代码中更改了remove =,因为这需要是pattern =。最后,我的 ngram 部分需要在 dfm 函数中,而不是在 token_remove 函数中。

      嵌套函数时,最好多格式化代码。它更好地显示了可能犯的错误。

      library(quanteda)
      df <- data.frame(data = c("Here is an example text and why I write it", 
                                "I can explain and here you but I can help as I would like to help"),
                       stringsAsFactors = FALSE)
      
      mystopwords <- c("is","an")
      corpus <- dfm(tokens_remove(tokens(df$data, 
                                         remove_punct = TRUE, 
                                         remove_numbers = TRUE, 
                                         remove_symbols = TRUE), 
                                  pattern = c(stopwords(language = "el", source = "misc"), 
                                             mystopwords) 
                                  ), 
                    ngrams = c(4,6)
                    )
      

      【讨论】:

        猜你喜欢
        • 2021-08-26
        • 2021-03-17
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 2013-02-02
        • 2014-05-10
        • 2020-07-08
        • 1970-01-01
        相关资源
        最近更新 更多