我正在尝试在 Windows 7 计算机上使用 R 的 wordnet 包进行并行处理。具体来说,我正在尝试查找名词列表的同义词。我在下面制作了一些示例代码来展示我正在尝试做的事情,但它似乎无法正确并行执行。它正在启动工人,它正在计算其中一个工人,而不是其他工人。我在下面制作的列表长度为 4,每个插槽中有 4 个单词。我试图将列表除以可用的核心数量,并将列表的一个子集发送到每个核心。然后 sapply 函数获取 4 个单词的同义词(在并行循环内)。我也尝试过使用 Snowfall 执行此操作,但无法导出字典(sfExport 似乎没有这样做)。我没有使用“.export” 在 foreach 循环中,因为它也给出了没有找到字典的错误,但是将它放在并行循环中似乎可以使它工作。任何帮助将非常感激。
library(wordnet)
library(foreach)
library(doSMP)
library(rJava)
NbrOfCores <- 2
workers <- startWorkers(NbrOfCores) # number of cores
registerDoSMP(workers)
getDoParName() # check name of parallel backend
getDoParVersion() # check version of parallel backend
getDoParWorkers() # check number of workers
set.seed(1)
setDict<-setDict("C:\\Program Files (x86)\\WordNet\\2.1\\dict\\")
initDict<-initDict("C:\\Program Files (x86)\\WordNet\\2.1\\dict\\")
dict<-getDictInstance()
words <- list(c("cat", "dog", "bird"),c("mouse", "iguana", "fish"),c("car", "tree", "house"),c("shoe", "shirt", "hat"))
rows=length(words) #4
prow<-floor(rows/NbrOfCores) #2
nouns<-foreach(i=1:NbrOfCores, .combine = c, .packages ="wordnet","rJava") %dopar% {
setDict<-setDict("C:\\Program Files (x86)\\WordNet\\2.1\\dict\\")
initDict<-initDict("C:\\Program Files (x86)\\WordNet\\2.1\\dict\\")
dict<-getDictInstance()
foreach(j=(prow*(i-1)+1):(prow*i)) %do% sapply(words[[j]],synonyms,"NOUN")}