【R】RMeCab
2020年12月9日
1. はじめに
Rで言語の最小単位(形態素)を処理できる形態素解析器MeCabを扱えるRMeCabを使ってみます。
2. インストール
2.1 MeCabのインストール
まずは、MeCabのインストールです。公式サイトから、Windowsバイナリをダウンロードしてインストールします。文字コードは、Shift-JISです。
2.2 RMeCabのインストール
Rで、次のようにインストールします。
install.packages("RMeCab", repos = "http://rmecab.jp/R")
3. 使ってみる
青空文庫から、新渡戸稲造の「我が教育の欠陥」を取得し、解析してみます。”第5回 青空文庫のテキストマイニングをRMeCabパッケージでやってみた”と”Rによるテキスト分析入門”を参考にさせていただきました(本当は、Webスクレイピングで文章を取得したかったのだけど、なかなかできず・・・。時間がかかりそうだったので、あきらめた。)。
”RMeCabパッケージのサポートページに、URLを引数として青空文庫からテキストデータをダウンロードし、さらにルビの削除まで行ってくれる関数( Aozora() )が公開されている”とのことなので、有難く使わせていただきます。
library("tidyverse")
library("rvest")
library("RMeCab")
Aozora <- function(url = NULL, txtname = NULL){
enc <- switch(.Platform$pkgType, "win.binary" = "CP932", "UTF-8")
if (is.null(url)) stop ("specify URL")
tmp <- unlist (strsplit (url, "/"))
tmp <- tmp [length (tmp)]
curDir <- getwd()
tmp <- paste(curDir, tmp, sep = "/")
download.file (url, tmp)
textF <- unzip (tmp)
unlink (tmp)
if(!file.exists (textF)) stop ("something wrong!")
if (is.null(txtname)) txtname <- paste(unlist(strsplit(basename (textF), ".txt$")))
if (txtname != "NORUBY") {
newDir <- paste(dirname (textF), "NORUBY", sep = "/")
if (! file.exists (newDir)) dir.create (newDir)
newFile <- paste (newDir, "/", txtname, "2.txt", sep = "")
con <- file(textF, 'r', encoding = "CP932" )
outfile <- file(newFile, 'w', encoding = enc)
flag <- 0;
reg1 <- enc2native ("\U005E\U5E95\U672C")
reg2 <- enc2native ("\U3010\U5165\U529B\U8005\U6CE8\U3011")
reg3 <- enc2native ("\UFF3B\UFF03\U005B\U005E\UFF3D\U005D\U002A\UFF3D")
reg4 <- enc2native ("\U300A\U005B\U005E\U300B\U005D\U002A\U300B")
reg5 <- enc2native ("\UFF5C")
while (length(input <- readLines(con, n=1, encoding = "CP932")) > 0){
if (grepl(reg1, input)) break ;
if (grepl(reg2, input)) break;
if (grepl("^------", input)) {
flag <- !flag
next;
}
if (!flag){
input <- gsub (reg3, "", input, perl = TRUE)
input <- gsub (reg4, "", input, perl = TRUE)
input <- gsub (reg5, "", input, perl = TRUE)
writeLines(input, con=outfile)
}
}
close(con); close(outfile)
return (newDir);
}
}
### 新渡戸 稲造
Aozora ("https://www.aozora.gr.jp/cards/000718/files/50532_ruby_37830.zip", "50532_")
これで、/NORUBYフォルダに、50532_2.txtというファイルで内容がダウンロードされました。
冒頭は、こんな感じ。
我が教育の欠陥
新渡戸稲造
我政府が教育上に於ける施設の多大なることは否むべからず。明治年代の教育法は、維新前の教育法を継承せるものに非ずして、全く新軌道を取れるものなれば、その事業の宏大なることもまた否むべからず。この新教育制度の成功の量の大なることも、また否むべからず。されどああその成功や過ぎ
ファイルを読み込んで、名詞の数を数えます(重複は除く)。
dat_txt <- "NORUBY/50532_2.txt" %>%
read.table(stringsAsFactors = FALSE,
header = FALSE)
mecab_results <- dat_txt %>%
RMeCabDF()
dat <- mecab_results %>%
imap_dfr( ~ data.frame(term = .,
class = names(.),
sentences = .y,
stringsAsFactors = F))
dat_count <- dat %>%
filter(class == "名詞") %>%
group_by(term, sentences) %>%
mutate(wordCount = n()) %>%
distinct() %>%
arrange(desc(sentences))
> dat_count
# A tibble: 389 x 4
# Groups: term, sentences [389]
term class sentences wordCount
<chr> <chr> <int> <int>
1 一 名詞 13 2
2 九 名詞 13 1
3 〇 名詞 13 1
4 七 名詞 13 1
5 年 名詞 13 1
6 八月 名詞 13 1
7 五 名詞 13 1
8 日 名詞 13 1
9 随想 名詞 13 1
10 録 名詞 13 1
# ... with 379 more rows
4. さいごに
スクレイピングでつまずいた・・・。形態素解析は、さわっただけだけど、面白そうですね。