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

Use DocumentTermMatrix in R with 'dictionary' parameter

我想使用 R 进行文本分类。我使用 DocumentTermMatrix 来 return 单词的矩阵:

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(),这会给您预期的结果。