c使用代表或复制或类似的语料库

时间:2017-08-28 06:55:27

标签: r text-mining corpus

我有一个小型语料库,例如

myvec <- c("n417", "disturbance", "grand theft auto", "assault", "burglary", 
"vandalism", "atmt to locate", "drug arrest", "traffic stop", 
"larceny", "graffiti complaint / reporting")

corpus <- VCorpus(VectorSource(myvec))

如果我想要将语料库大10倍,我该如何做到这一点,以便生成的变量是VCorpus而不是列表?

尝试:

corpus <- replicate(10, corpus) # returns a list
corpus <- VCorpus(replicate(10, corpus)) # Error: inherits(x, "Source") is not TRUE
corpus <- c(corpus, corpus, corpus, corpus, corpus, corpus, corpus) # works, returns a corpus 7 times bigger but involves lots of typing)

如果我有一个小型语料库,并且我想为了示例目的而使其大十倍,我该怎么办呢?

1 个答案:

答案 0 :(得分:2)

我们可以在复制后使用do.call c

library(tm)
do.call(c, rep(list(corpus), 7))
# <<VCorpus>>
#Metadata:  corpus specific: 0, document level (indexed): 0
#Content:  documents: 77

同样适用于replicate

do.call(c, replicate(7, corpus, simplify = FALSE))
#<<VCorpus>>
#Metadata:  corpus specific: 0, document level (indexed): 0
#Content:  documents: 77

此处simplify = FALSE

不需要replicate
do.call(c, replicate(7, corpus))
#<<VCorpus>>
#Metadata:  corpus specific: 0, document level (indexed): 0
#Content:  documents: 77