回答編集履歴

1

おまけ

2018/06/19 03:41

投稿

fiwa
fiwa

スコア1192

test CHANGED
@@ -61,3 +61,73 @@
61
61
 
62
62
 
63
63
  ```
64
+
65
+
66
+
67
+ ####追記
68
+
69
+ エラーの原因はよくわかりませんが、扱うファイルが大きくなると問題が起きる傾向があるようなので RMeCab は使用せずに system() から シェルの mecab を使用して処理させる方法を試してみました。
70
+
71
+ 中間ファイル word_POS.txt を生成してしまいますが、こちらの方法だとエラーは全く起きませんでした。60万語でも大丈夫でした。
72
+
73
+
74
+
75
+ ```R
76
+
77
+ library(stringr)
78
+
79
+ library(wordcloud)
80
+
81
+
82
+
83
+ system('mecab -b 32768 -F"%m_%f[0]\n" -E "" extrabig.txt > word_POS.txt')
84
+
85
+ word.vector <- scan("word_POS.txt", what = character(), sep = "\n")
86
+
87
+
88
+
89
+ # あとは全く同じ
90
+
91
+ search.word <- "^国民_名詞$" # 検索語を「単語_品詞」という形式で指定
92
+
93
+ span <- 5 # スパンの指定(ここでは、左右5語まで)
94
+
95
+ span <- (-span : span)
96
+
97
+
98
+
99
+ positions.of.matches <- grep(search.word, word.vector, perl = TRUE)
100
+
101
+ results <- NULL
102
+
103
+ for(i in 1 : length(span)) {
104
+
105
+ collocate.positions <- positions.of.matches + span[i]
106
+
107
+ collocates <- word.vector[collocate.positions]
108
+
109
+ results <- append(results, collocates)
110
+
111
+ }
112
+
113
+
114
+
115
+ results <- str_replace_all(string = results, pattern = search.word, replacement = "")
116
+
117
+ not.blank <- which(results != "")
118
+
119
+ results <- results[not.blank]
120
+
121
+
122
+
123
+ positions.of.matches.2 <- grep("_名詞", results, perl = TRUE) # ここでは名詞
124
+
125
+ results.2 <- results[positions.of.matches.2]
126
+
127
+ results.3 <- str_replace_all(string = results.2, pattern = "_名詞", replacement = "") # 品詞の情報(ここでは、"_名詞"の部分)を削除
128
+
129
+
130
+
131
+ wordcloud(results.3, min.freq = 1, colors = brewer.pal(8, "Dark2"))
132
+
133
+ ```