tm包中的DocumentTermMatrix不返回所有单词
我正在用R中的tm软件包创建一个文档术语矩阵,但是我的语料库中的一些单词在某个地方丢失了 我会举例说明。假设我有这个小语料库tm包中的DocumentTermMatrix不返回所有单词,r,text-mining,tm,R,Text Mining,Tm,我正在用R中的tm软件包创建一个文档术语矩阵,但是我的语料库中的一些单词在某个地方丢失了 我会举例说明。假设我有这个小语料库 library(tm) crps <- " more hours to my next class bout to go home and go night night" crps <- VCorpus(VectorSource(crps)) 然而,我想要(也期望)的是: 为什么DocumentTermMatrix()跳过“我的”、“去”和“去”这些词?有没
library(tm)
crps <- " more hours to my next class bout to go home and go night night"
crps <- VCorpus(VectorSource(crps))
然而,我想要(也期望)的是:
为什么DocumentTermMatrix()
跳过“我的”、“去”和“去”这些词?有没有办法控制和修复此函数?DocumentTermMatrix()
会自动丢弃少于三个字符的单词。因此,在构建文档术语矩阵时,不考虑单词到
、my
和go
从帮助页面?DocumentTermMatrix
,您可以看到一个名为control
的可选参数。此可选参数具有许多默认值(有关更多详细信息,请参阅帮助页面?termFreq
)。其中一个默认值是字长至少为三个字符,即wordlength=c(3,Inf)
。您可以更改此选项以适应所有单词,而不考虑单词长度:
dm <- DocumentTermMatrix(my_corpus, control = list(wordLengths=c(1, Inf))
inspect(dm)
# <<DocumentTermMatrix (documents: 1, terms: 11)>>
# Non-/sparse entries: 11/0
# Sparsity : 0%
# Maximal term length: 5
# Weighting : term frequency (tf)
#
# Terms
# Docs and bout class go home hours more my next night to
# 1 1 1 1 2 1 1 1 1 1 2 2
dm我假设您正在使用tm
-软件包?crps
是什么类型的对象?您是如何获得CRP的?您是否使用了类似于crps的内容是的,我使用了crps作为记录,这也发生在TermDocumentMatrix(text)
中。我有一个很长的NLP管道,我一直困惑于为什么一些预处理步骤是丢弃单词而不是增加它们的频率。我已经通过TermDocumentMatrix(text,control=list(wordlength=c(1,Inf))
解决了这个问题,如答案所示。
# Docs and bout class home hours more next night my go to
# 1 1 1 1 1 1 1 1 2 1 2 1
dm <- DocumentTermMatrix(my_corpus, control = list(wordLengths=c(1, Inf))
inspect(dm)
# <<DocumentTermMatrix (documents: 1, terms: 11)>>
# Non-/sparse entries: 11/0
# Sparsity : 0%
# Maximal term length: 5
# Weighting : term frequency (tf)
#
# Terms
# Docs and bout class go home hours more my next night to
# 1 1 1 1 2 1 1 1 1 1 2 2