teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

おまけ

2018/06/19 03:41

投稿

fiwa
fiwa

スコア1192

answer CHANGED
@@ -29,4 +29,39 @@
29
29
  library(wordcloud)
30
30
  wordcloud(results.3, min.freq = 1, colors = brewer.pal(8, "Dark2"))
31
31
 
32
+ ```
33
+
34
+ ####追記
35
+ エラーの原因はよくわかりませんが、扱うファイルが大きくなると問題が起きる傾向があるようなので RMeCab は使用せずに system() から シェルの mecab を使用して処理させる方法を試してみました。
36
+ 中間ファイル word_POS.txt を生成してしまいますが、こちらの方法だとエラーは全く起きませんでした。60万語でも大丈夫でした。
37
+
38
+ ```R
39
+ library(stringr)
40
+ library(wordcloud)
41
+
42
+ system('mecab -b 32768 -F"%m_%f[0]\n" -E "" extrabig.txt > word_POS.txt')
43
+ word.vector <- scan("word_POS.txt", what = character(), sep = "\n")
44
+
45
+ # あとは全く同じ
46
+ search.word <- "^国民_名詞$" # 検索語を「単語_品詞」という形式で指定
47
+ span <- 5 # スパンの指定(ここでは、左右5語まで)
48
+ span <- (-span : span)
49
+
50
+ positions.of.matches <- grep(search.word, word.vector, perl = TRUE)
51
+ results <- NULL
52
+ for(i in 1 : length(span)) {
53
+ collocate.positions <- positions.of.matches + span[i]
54
+ collocates <- word.vector[collocate.positions]
55
+ results <- append(results, collocates)
56
+ }
57
+
58
+ results <- str_replace_all(string = results, pattern = search.word, replacement = "")
59
+ not.blank <- which(results != "")
60
+ results <- results[not.blank]
61
+
62
+ positions.of.matches.2 <- grep("_名詞", results, perl = TRUE) # ここでは名詞
63
+ results.2 <- results[positions.of.matches.2]
64
+ results.3 <- str_replace_all(string = results.2, pattern = "_名詞", replacement = "") # 品詞の情報(ここでは、"_名詞"の部分)を削除
65
+
66
+ wordcloud(results.3, min.freq = 1, colors = brewer.pal(8, "Dark2"))
32
67
  ```