DocumentTermMatrix与字典

问题描述 投票:1回答:1

我想将语料库转换为DocumentTermMatrix,只选择单词列表。我知道控件列表中的“dictionary”参数执行此操作:

     a = list("I am a big big big apple", "Petter Petter Peter Peter")
     v = VCorpus(VectorSource(a))
     my_terms = c("peter", "petter")
     DocumentTermMatrix(v, control = list(dictionary = my_terms)) %>% as.matrix()

它给了我这个:

        Terms
    Docs peter petter
       1     0      0
       2     1      1

虽然我想要的是这样的:

        Terms
    Docs peter petter
       1     0      0
       2     2      2
  1. 第一份文件虽然是空的,但必须保留在那里。 (因为它必须与元数据匹配)
  2. 必须在输出中显示该字的频率。

我想知道是否有功能/参数这样做。

r text-mining tm
1个回答
0
投票

它工作正常:

library(magrittr)
library(tm)

a <- list("I am a big big big apple", "Petter Petter Peter Peter")
v <- VCorpus(VectorSource(a))
my_terms <- c("peter", "petter")
DocumentTermMatrix(v, control = list(dictionary = my_terms)) %>% 
         as.matrix()
© www.soinside.com 2019 - 2024. All rights reserved.