首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >特定的停用词列表quanteda

特定的停用词列表quanteda
EN

Stack Overflow用户
提问于 2019-01-14 22:05:17
回答 2查看 432关注 0票数 1

我想使用quanteda删除一个带有停用词的特定列表。

我使用的是:

代码语言:javascript
复制
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)))

但是我收到了这个错误:

代码语言:javascript
复制
> Error in tokens_select(x, ..., selection = "remove") : 
  unused arguments (remove = c(stopwords(language = "en", source = "misc"), stopwords1), ngrams = c(4, 6))

如何在quanteda中正确使用mystopwords列表?

EN

回答 2

Stack Overflow用户

发布于 2019-01-14 23:07:24

基于@phiver的回答,这是在quanteda中删除特定标记的标准方法。不需要使用stopwords(),因为您提供了要删除的令牌的向量,并且patterns参数可以接受向量,但请使用valuetype = 'fixed'

为了代码的可读性,我使用了dplyr,但你不必这样做。

代码语言:javascript
复制
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))
票数 1
EN

Stack Overflow用户

发布于 2019-01-14 22:26:41

这将会起作用。首先,我将stringAsFactors = FALSE添加到data.frame中。提供给tokens的文本需要是一个字符矢量,而不是一个因子。接下来,我更改了代码中的remove =,因为它需要为pattern =。最后,ngram部分需要在dfm函数中,而不是在token_remove函数中。

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

代码语言:javascript
复制
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)
              )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54183040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档