LoginSignup
1
4

More than 5 years have passed since last update.

R でのテキストマイニングに入門してみた

Posted at

Rによるテキストマイニングを読んでいまして、業務に使えないかなぁと思いつつ、試してみたことを残しておこうと思います。

今回の記事の実行環境

  • Windows 7

参考書籍

書籍に記載のコードはこちらにあるとのことです。

R 実行環境の準備

chocolatey にパッケージがあったので、それでインストールしました。

> choco install R
> choco install R.Studio

パッケージのインストール

R では、CRAN を経由してパッケージをインストールする、と。
R Studio であれば、[Tools] - [Install Packages...] で、GUI でインストールできます。今回は下記のパッケージをインストールしました。

  • dplyr
  • tidytext
  • quanteda

試す

RStudio の Console で実行しました。

unnest_tokens() によるトークン化

> text <- c("test desu -",
+           "okok -",
+           "hoge fuga moge -",
+           "upupup")
> text
[1] "test desu -"     "okok -"           "hoge fuga moge -"
[4] "upupup"          

> library(dplyr)

> text_df <- data_frame(line=1:4, text=text)
> text_df
# A tibble: 4 x 2
   line text            
  <int> <chr>           
1     1 test desu -    
2     2 okok -          
3     3 hoge fuga moge -
4     4 upupup          

> library(tidytext)
> text_df %>%
+     unnest_tokens(word, text)
# A tibble: 7 x 2
   line word  
  <int> <chr> 
1     1 test  
2     1 desu 
3     2 okok  
4     3 hoge  
5     3 fuga  
6     3 moge  
7     4 upupup

おー。できました。

nグラムでトークン化

n=2 で試してみます。

> text_df %>%
+     unnest_tokens(bigram, text, token = "ngrams", n = 2)
# A tibble: 5 x 2
   line bigram    
  <int> <chr>     
1     1 test desu
2     2 NA        
3     3 hoge fuga 
4     3 fuga moge 
5     4 NA 

こんな感じ。NA のところは、1単語しかない行があったからですね。

大統領演説中の単語の出現頻度が時代とともにどう変わるか

ほぼ写経です ^^;

> library(methods)
> data("data_corpus_inaugural", package = "quanteda")
> inaug_dfm <- quanteda::dfm(data_corpus_inaugural, verbose = FALSE)
> 
> inaug_td <- tidy(inaug_dfm)
> inaug_td
# A tibble: 44,709 x 3
   document        term            count
   <chr>           <chr>           <dbl>
 1 1789-Washington fellow-citizens     1
 2 1797-Adams      fellow-citizens     3
 3 1801-Jefferson  fellow-citizens     2
 4 1809-Madison    fellow-citizens     1
 5 1813-Madison    fellow-citizens     1
 6 1817-Monroe     fellow-citizens     5
 7 1821-Monroe     fellow-citizens     1
 8 1841-Harrison   fellow-citizens    11
 9 1845-Polk       fellow-citizens     1
10 1849-Taylor     fellow-citizens     1
# ... with 44,699 more rows
>
> inaug_tf_idf <- inaug_td %>%
+     bind_tf_idf(term, document, count) %>%
+     arrange(desc(tf_idf))
> 
> inaug_tf_idf
# A tibble: 44,709 x 6
   document        term        count      tf   idf tf_idf
   <chr>           <chr>       <dbl>   <dbl> <dbl>  <dbl>
 1 1793-Washington arrive          1 0.00680  4.06 0.0276
 2 1793-Washington upbraidings     1 0.00680  4.06 0.0276
 3 1793-Washington violated        1 0.00680  3.37 0.0229
 4 1793-Washington willingly       1 0.00680  3.37 0.0229
 5 1793-Washington incurring       1 0.00680  3.37 0.0229
 6 1793-Washington previous        1 0.00680  2.96 0.0201
 7 1793-Washington knowingly       1 0.00680  2.96 0.0201
 8 1793-Washington injunctions     1 0.00680  2.96 0.0201
 9 1793-Washington witnesses       1 0.00680  2.96 0.0201
10 1793-Washington besides         1 0.00680  2.67 0.0182
# ... with 44,699 more rows

> library(tidyr)
> year_term_counts <- inaug_td %>%
+     extract(document, "year", "(\\d+)", convert=TRUE) %>%
+     complete(year, term, fill = list(count = 0)) %>%
+     group_by(year) %>%
+     mutate(year_total = sum(count))

> library(ggplot2)
> year_term_counts %>%
+     filter(term %in% c("god", "america")) %>%
+     ggplot(aes(year, count / year_total)) +
+     geom_point() +
+     geom_smooth() +
+     facet_wrap(~ term, scales = "free_y") +
+     scale_y_continuous(labels = scales::percent_format()) +
+     ylab("% freq of word in inaugural address")
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

うぅ・・・ちゃんと理解せねば・・・

そして、出力されたグラフがこちら
image.png
おー。ちゃんとできた。それなりにキレイ・・・!

最後に

R は初めて触りましたが、分析からグラフ化までサクっとできるのは、とても手を動かしやすいなぁと思いました。

もっと色々データの持ち方などを勉強しなければとは思いますが、「どういう風に単語の出現頻度が変わるか」といったあたりは、すごく興味を持ちました。トレンド分析とかにも使えそう・・・!

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4