包tm:如何避免删除停用词

时间:2015-08-12 05:04:34

标签: r tm stop-words

我想避免删除停用词,但我发现无论tm的参数设置如何,它都会删除一些停用词。

library(tm)
documents <- c("This is a list containing the tallest buildings in San    Francisco")
corpus <- Corpus(VectorSource(documents))
matrix <- DocumentTermMatrix(corpus,control=list(stopwords=FALSE))
colnames(matrix)
# [1] "buildings"  "containing" "francisco"  "list"       "san"       
# [6] "tallest"    "the"        "this"  

DocumentTermMatrix似乎删除了“is”和“in”中的停用词。

我该如何避免这种情况?设置stopwords=TRUE只会阻止删除“the”。 如何防止删除“是”和“进入”?

1 个答案:

答案 0 :(得分:5)

你的问题不是DocumentTermMatrix将“是”和“in”视为停用词,而是因为它们是短于3个字符的单词。标记生成器的默认值是将长度为3的字符串视为单词,即排除短于3的字符串。

您可以按照以下方式修改您的控件,以包含单字母开头的字词

matrix <- DocumentTermMatrix(corpus,control=list(stopwords=FALSE,
                                                 wordLengths=c(1, Inf)))

我相信这就是你想要的

> colnames(matrix)
 [1] "a"          "buildings"  "containing" "francisco"  "in"         "is"        
 [7] "list"       "san"        "tallest"    "the"        "this" 

您的问题中未提及“a”,因此如果您想要排除(以及其他类似“我”),请将wordLength设置为从2开始。

相关问题