【发布时间】:2012-01-26 08:18:44
【问题描述】:
我正在尝试使用包 tm 过滤以下文档中的停用词。
library(tm)
documents <- c("the quick brown fox jumps over the lazy dog", "i am the walrus")
corpus <- Corpus(VectorSource(documents))
matrix <- DocumentTermMatrix(corpus,control=list(stopwords=TRUE))
但是,当我运行此代码时,我仍然会在 DocumentTermMatrix 中得到以下内容。
colnames(matrix)
[1] "brown" "dog" "fox" "jumps" "lazy" "over" "quick" "the" "walrus"
“The”在包tm 使用的列表中被列为停用词。我对stopwords 参数做错了什么,还是tm 包中的错误?
编辑:我联系了 Ingo Feinerer,他指出这在技术上不是错误:
首先处理用户提供的选项,然后处理所有剩余的选项 选项。因此停用词删除是在标记化之前完成的(如 已经由 Vincent Zoonekynd 在 stackoverflow.com 上编写),它给出了 正是你的结果。
因此,解决方案是在stopwords参数之前显式列出默认的分词选项,例如:
library(tm)
documents <- c("the quick brown fox jumps over the lazy dog", "i am the walrus")
corpus <- Corpus(VectorSource(documents))
matrix <- DocumentTermMatrix(corpus,control=list(tokenize=scan_tokenizer,stopwords=TRUE))
colnames(matrix)
【问题讨论】:
-
感谢您的问题和答案......尽管它们在我的问题中根本不起作用。似乎 TM 中的停用词目前只是一个令人头疼的问题。