无法让tm_map使用mc.cores参数

时间:2017-08-22 14:38:39

标签: r tm

我有一个包含超过1000万份文档的大型语料库。每当我尝试使用mc.cores参数对多个核心进行转换时,我都会收到错误:

Error in FUN(content(x), ...) : unused argument (mc.cores = 10)

我目前托管的r studio中有15个可用内核。

# I have a corpus
> inspect(corpus[1])
<<VCorpus>>
Metadata:  corpus specific: 0, document level (indexed): 0
Content:  documents: 1

[[1]]
<<PlainTextDocument>>
Metadata:  7
Content:  chars: 46

> length(corpus)
[1] 10255313

观察当我尝试使用tm_map

进行转换时会发生什么
library(tidyverse)
library(qdap)
library(stringr)
library(tm)
library(textstem)
library(stringi)
library(SnowballC)

E.g。

> corpus <- tm_map(corpus, content_transformer(replace_abbreviation), mc.cores = 10)
Error in FUN(content(x), ...) : unused argument (mc.cores = 10)

尝试添加lazy = T

corpus <- tm_map(corpus, content_transformer(replace_abbreviation), mc.cores = 10, lazy = T) # read the documentation, still don't really get what this does

转换后如果我去,例如

> corpus[[1]][1] I get:
Error in FUN(content(x), ...) : unused argument (mc.cores = 10)

在我得到之前:

> corpus.beforetransformation[[1]][1]
$content
[1] "here is some text"

我在这里做错了什么?如何使用mc.cores参数来使用更多的处理器?

可重复的例子:

sometext <- c("cats dogs rabbits", "oranges banannas pears", "summer fall winter") %>% 
  data.frame(stringsAsFactors = F) %>% DataframeSource %>% VCorpus

corpus.example <- tm_map(sometext, content_transformer(replace_abbreviation), mc.cores = 2, lazy = T)
corpus.example[[1]][1]

1 个答案:

答案 0 :(得分:2)

tm documentation开始,尝试以下操作:

options(mc.cores = 10)  # or whatever
tm_parLapply_engine(parallel::mclapply)  # mclapply gets the number of cores from global options
tm_map(sometext, content_transformer(replace_abbreviation))