R 大文本语料库打破了tm_地图

R 大文本语料库打破了tm_地图,r,text-mining,tm,text-analysis,term-document-matrix,R,Text Mining,Tm,Text Analysis,Term Document Matrix,在过去的几天里,我一直在为这件事伤脑筋。我搜索了所有的SO档案,并尝试了建议的解决方案,但似乎无法实现这一点。我在文件夹中有几组txt文档,如2000 06、1995-99等,我想运行一些基本的文本挖掘操作,例如创建文档术语矩阵和术语文档矩阵,以及基于单词的共位执行一些操作。我的脚本在较小的语料库上工作,但是,当我在较大的语料库上尝试时,我失败了。我已经为一个这样的文件夹操作粘贴了代码 library(tm) # Framework for text mining. library(Snowba

在过去的几天里,我一直在为这件事伤脑筋。我搜索了所有的SO档案,并尝试了建议的解决方案,但似乎无法实现这一点。我在文件夹中有几组txt文档,如2000 06、1995-99等,我想运行一些基本的文本挖掘操作,例如创建文档术语矩阵和术语文档矩阵,以及基于单词的共位执行一些操作。我的脚本在较小的语料库上工作,但是,当我在较大的语料库上尝试时,我失败了。我已经为一个这样的文件夹操作粘贴了代码

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地图、所有地图和一个地图(ToToTower)
  • 将“lazy=T”添加到tm_映射
  • 尝试了一些并行计算包
虽然它不适用于我的两个脚本,但它每次都适用于第三个脚本。但是这三个脚本的代码都是相同的,只是我加载的.rda文件的大小不同。这三种方法的数据结构也完全相同

  • 数据集1:Size-493.3KB=错误
  • 数据集2:Size-630.6KB=错误
  • 数据集3:Size-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)

MyCorpus谢谢,帮了很多忙!谢谢!我丢失了SO帐户的密码,所以现在才看到:)
MyCorpus <- tm_map(MyCorpus,
                     content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')),
                     mc.cores=1)