r - 在 R 中使用带有 'dictionary' 参数的 DocumentTermMatrix

标签 r tm corpus term-document-matrix

我想使用 R 进行文本分类。我使用 DocumentTermMatrix 返回单词矩阵:

library(tm)
crude <- "japan korea usa uk albania azerbaijan"
corps <- Corpus(VectorSource(crude))
dtm <- DocumentTermMatrix(corps)
inspect(dtm)

words <- c("australia", "korea", "uganda", "japan", "argentina", "turkey")
test <- DocumentTermMatrix(corps, control=list(dictionary = words))
inspect(test)

第一个 inspect(dtm) 按预期工作,结果:

    Terms
Docs albania azerbaijan japan korea usa
   1       1          1     1     1   1

但是第二个 inspect(test) 显示这个结果:

    Terms
Docs argentina australia japan korea turkey uganda
   1         0         1     0     1      0      0

虽然预期的结果是:

    Terms
Docs argentina australia japan korea turkey uganda
   1         0         0     1     1      0      0

是bug还是我用错了?

最佳答案

Corpus() 在索引词频时似乎有一个错误。

改为使用 VCorpus(),这将为您提供预期的结果。

关于r - 在 R 中使用带有 'dictionary' 参数的 DocumentTermMatrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643961/

相关文章:

带有训练和测试集的removeSparseTerms

database - R tm : reloading a 'PCorpus' backend filehash database as corpus (e. g。在重新启动的 session /脚本中)

python - 了解使用 "gensim.corpora.Dictionary(TEXT)"后单词如何存储在 gensim 语料库中

r - 基于网络图相似度的前 10 位好友

r - 根据现有行向数据框添加行

r - 使用 tm 和 RWeka 创建 N-Grams - 适用于 VCorpus,但不适用于 Corpus

tm - R : find most frequent group of words in corpus

r - 组合 tm R 中的单词未达到预期结果

sql - 如何将 SQL 导入 R 的 dplyr?

重新排序树状图