我想使用 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/