2014-11-09 9 views
6

나는 지난 며칠 동안 내 머리를 아프게했습니다. 나는 모든 SO 아카이브를 검색하고 제안 된 솔루션을 시도했지만이 작업을 수행 할 수없는 것 같습니다. 나는 2000 년 6 월, 1995 -99 년 등과 같은 폴더에 txt 문서 세트를 가지고 있으며 문서 용어 행렬 및 용어 문서 행렬을 만들고 단어의 일부 위치 기반 공동 위치를 만드는 것과 같은 기본적인 텍스트 마이닝 작업을 실행하려고합니다. 내 스크립트는 더 작은 코퍼스에서 작동하지만 더 큰 코퍼스로 시도 할 때 실패합니다. 이러한 폴더 작업을위한 코드를 붙여 넣었습니다.빅 텍스트 코퍼스가 tm_map을 깨기

library(tm) # Framework for text mining. 
library(SnowballC) # Provides wordStem() for stemming. 
library(RColorBrewer) # Generate palette of colours for plots. 
library(ggplot2) # Plot word frequencies. 
library(magrittr) 
library(Rgraphviz) 
library(directlabels) 

setwd("/ConvertedText") 
txt <- file.path("2000 -06") 

docs<-VCorpus(DirSource(txt, encoding = "UTF-8"),readerControl = list(language = "UTF-8")) 
docs <- tm_map(docs, content_transformer(tolower), mc.cores=1) 
docs <- tm_map(docs, removeNumbers, mc.cores=1) 
docs <- tm_map(docs, removePunctuation, mc.cores=1) 
docs <- tm_map(docs, stripWhitespace, mc.cores=1) 
docs <- tm_map(docs, removeWords, stopwords("SMART"), mc.cores=1) 
docs <- tm_map(docs, removeWords, stopwords("en"), mc.cores=1) 
#corpus creation complete 

setwd("/ConvertedText/output") 
dtm<-DocumentTermMatrix(docs) 
tdm<-TermDocumentMatrix(docs) 
m<-as.matrix(dtm) 
write.csv(m, file="dtm.csv") 
dtms<-removeSparseTerms(dtm, 0.2) 
m1<-as.matrix(dtms) 
write.csv(m1, file="dtms.csv") 
# matrix creation/storage complete 

freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE) 
wf <- data.frame(word=names(freq), freq=freq) 
freq[1:50] 
#adjust freq score in next line 
p <- ggplot(subset(wf, freq>100), aes(word, freq))+ geom_bar(stat="identity")+ theme(axis.text.x=element_text(angle=45, hjust=1)) 
ggsave("frequency2000-06.png", height=12,width=17, dpi=72) 
# frequency graph generated 


x<-as.matrix(findFreqTerms(dtm, lowfreq=1000)) 
write.csv(x, file="freqterms00-06.csv") 
png("correlation2000-06.png", width=12, height=12, units="in", res=900) 
graph.par(list(edges=list(col="lightblue", lty="solid", lwd=0.3))) 
graph.par(list(nodes=list(col="darkgreen", lty="dotted", lwd=2, fontsize=50))) 
plot(dtm, terms=findFreqTerms(dtm, lowfreq=1000)[1:50],corThreshold=0.7) 
dev.off() 

tm_map에서 mc.cores = 1 인수를 사용하면 작업이 무기한 계속됩니다. 그러나 tm_map에 lazy = TRUE 인수를 사용하면 겉으로보기에는 잘되지만 이후 작업에서이 오류가 발생합니다.

Error in UseMethod("meta", x) : 
    no applicable method for 'meta' applied to an object of class "try-error" 
In addition: Warning messages: 
1: In mclapply(x$content[i], function(d) tm_reduce(d, x$lazy$maps)) : 
    all scheduled cores encountered errors in user code 
2: In mclapply(unname(content(x)), termFreq, control) : 
    all scheduled cores encountered errors in user code 

나는 모든 해결책을 찾고 있지만 일관되게 실패했습니다. 어떤 도움이라도 대단히 감사하겠습니다!

최고! k

답변

13

작동하는 해결책을 찾았습니다.

배경은/디버깅 단계

내가 작동하지 않았다 몇 가지 시도 : 하나에

  • 은, 모든 일부 tm_map에 "content_transformer"를 추가을 (totower) 추가
  • " 게으름 = T "에서 tm_map까지
  • 일부 병렬 컴퓨팅 패키지를 시도했습니다.

내 스크립트 2 개에서 작동하지 않지만 세 번째 스크립트에서는 매번 작동합니다. 그러나 세 스크립트 모두의 코드는로드하는 .rda 파일의 크기 만 다릅니다. 데이터 구조도 세 가지 모두 동일합니다.

  • 데이터 세트 1 : 크기 - 493.3KB = 오류
  • 데이터 집합 2 : 크기 - 630.6KB = 오류
  • 데이터 세트 3 : 크기 - 300.2KB = 작품!

그냥 이상합니다.

sessionInfo() 출력 :

R version 3.1.2 (2014-10-31) 
Platform: x86_64-apple-darwin13.4.0 (64-bit) 

locale: 
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8 

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] snowfall_1.84-6 snow_0.3-13  Snowball_0.0-11 RWekajars_3.7.11-1 rJava_0.9-6    RWeka_0.4-23  
[7] slam_0.1-32  SnowballC_0.5.1 tm_0.6    NLP_0.1-5   twitteR_1.1.8  devtools_1.6  

loaded via a namespace (and not attached): 
[1] bit_1.1-12  bit64_0.9-4 grid_3.1.2  httr_0.5  parallel_3.1.2 RCurl_1.95-4.3 rjson_0.2.14 stringr_0.6.2 
[9] tools_3.1.2 

솔루션

난 그냥 데이터를로드 한 후이 줄을 추가하고 모든 이제 작동

:

MyCorpus <- tm_map(MyCorpus, 
        content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')), 
        mc.cores=1) 

여기에 힌트를 찾았습니다 http://davetang.org/muse/2013/04/06/using-the-r_twitter-package/를 (작성자가 2014 년 11 월 26 일 오류로 인해 코드를 업데이트했습니다.

+1

감사합니다. – xbsd

+0

고마워요! 내 SO 계정에 대한 암호를 잃어 버렸으므로 이제 이것을 보았습니다 :) – Kartik