2012-03-26 42 views
2

我正尝试在Windows 7计算机上使用R的wordnet软件包进行并行处理。具体来说,我试图找到名词列表的同义词。我在下面做了一些示例代码来展示我想要做的事情,但似乎并没有正确执行。它启动了工人,它正在计算其中一个工人,而不是其他工人。下面列出的名单长度为4,每个单位有4个字。我试图按可用内核的数量划分列表,并将列表的子集发送给每个内核。然后sapply函数获取4个单词的同义词(在并行循环内)。我也试过用降雪做这个,但是我无法得到它来导出字典(sfExport似乎没有这样做)。我没有在foreach循环中使用“.export”,因为它也给字典找不到的错误,但把它放在并行循环内似乎使它工作。任何帮助将非常感激。并行使用r中的wordnet(使用Windows 7)

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")} 

回答

1

我觉得你的问题是你如何设置你的foreachi变量。应该循环的是words对象,而不是内核数量。此代码的工作:

library(wordnet) 
library(foreach) 
# library(doSMP) # I don't think you want to use this package anymore. 
library(rJava) 
require(snow) # Add the snow packages 
require(doSNOW) 

NbrOfCores <- 2 
cl.tmp = makeCluster(rep('localhost',NbrOfCores), type='SOCK') 
registerDoSNOW(cl.tmp) 

words <- list(c("cat", "dog", "bird"),c("mouse", "iguana", "fish"),c("car", "tree", "house"),c("shoe", "shirt", "hat")) 

foreach(words=iter(words), .packages='wordnet') %dopar% { 
    setDict<-setDict("C:\\Program Files (x86)\\WordNet\\2.1\\dict\\") 
    initDict<-initDict("C:\\Program Files (x86)\\WordNet\\2.1\\dict\\") 
    dict<-getDictInstance() 
    sapply(words,synonyms,pos='NOUN') 
} 

它看起来像doSMP包不适用于我的R版本,所以我只是把它切换到snow,但你可以使用任何你想要的后端。

+0

谢谢,代码。我昨天计算出我的主要问题是我没有正确定义我的环境变量。我需要为用户和系统定义WNHOME。我已经有了WNSEARCHDIR,但是没有WNHOME,它似乎无法找到字典。尽管我喜欢你的代码,但最近我一直对doSMP感到恼火,但在集群的启动和结束方面却是不可预测的,所以我认为我会换成雪(或降雪)。再次感谢! – 2012-04-03 13:27:14