6

私はここ数日、これについて頭を悩ませてきました。私はすべての SO アーカイブを検索し、提案された解決策を試しましたが、これを機能させることができないようです。2000 06、1995 -99 などのフォルダーに txt ドキュメントのセットがあり、ドキュメント用語マトリックスや用語ドキュメント マトリックスの作成、単語のコロケーションに基づく操作など、いくつかの基本的なテキスト マイニング操作を実行したいと考えています。私のスクリプトは小さなコーパスで動作しますが、大きなコーパスで試してみるとうまくいきません。そのようなフォルダー操作のコードを貼り付けました。

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

4

1 に答える 1

13

うまくいく解決策を見つけました。

バックグラウンド/デバッグ手順

うまくいかないことをいくつか試しました:

  • 「content_transformer」を一部の tm_map、すべて、1 つ (タワー) に追加する
  • 「lazy = T」を tm_map に追加
  • いくつかの並列計算パッケージを試しました

2 つのスクリプトでは機能しませんが、3 番目のスクリプトでは毎回機能します。しかし、3 つのスクリプトすべてのコードは同じで、ロードする .rda ファイルのサイズが異なるだけです。データ構造も 3 つすべてで同じです。

  • データセット 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 日にエラーが発生したため、コードを更新しました。)

于 2014-12-15T22:42:52.713 に答える