【问题标题】:Remove stopwords based on a long list根据长列表删除停用词
【发布时间】:2018-04-19 04:39:47
【问题描述】:

我有一个包含 60000 行/短语的数据框,我想将其用作停用词并从文本中删除。

我使用 tm 包,并在读取包含停用词列表的 csv 文件后使用这一行:

corpus <- tm_map(corpus, removeWords, df$mylistofstopwords)

但我收到此错误:

In addition: Warning message:
In gsub(sprintf("(*UCP)\\b(%s)\\b", paste(sort(words, decreasing = TRUE),  :
  PCRE pattern compilation error
    'regular expression is too large'
    at ''

有什么问题是因为列表太大了吗?有什么我可以解决的吗?

【问题讨论】:

  • 拆分列表并使用两个不同的removeWords 列表调用tm_map 两次?
  • @MrFlick 我试图泄露完整列表,但问题还是一样。我只尝试了前 2000 行并且它有效。我只是想知道是否有更高效的代码解决方案并且可能更快
  • 这些词很长吗? range(nchar(df$mylistofstopwords)) 是什么?
  • @MrFlick 我尝试输入range(nchar(df$mylistofstopwords)),但收到此错误:Error in nchar(df$mylistofstopwords) : 'nchar()' requires a character vector
  • 该列不是字符类吗? class(df$mylistofstopwords) 返回什么?也许range(nchar(as.character(df$mylistofstopwords)))’ or the mean()`

标签: r tm


【解决方案1】:

您可以通过将停用词列表分成多个部分来解决您的问题,如下所示:

chunk <- 1000
i <- 0
n <- length(df$mylistofstopwords)
while (i != n) {
    i2 <- min(i + chunk, n)
    corpus <- tm_map(corpus, removeWords, df$mylistofstopwords[(i+1):i2])
    i <- i2
}

或者,您可以只使用可以处理长停用词列表的包。 corpus 就是这样一个包。 quanteda 是另一个。以下是如何在 corpus

中获取文档逐项矩阵
library(corpus)
x <- term_matrix(corpus, drop = df$mylistofstopwords)

这里,输入参数corpus可以是一个tm语料库。

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 2018-09-28
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多