• Home
  • Akiko Yokoyama
  • Contact
  • Feed
ja | en |

Trying to train a Machine learning use word2vec model using as a corpus a Wikipedia

word2vecがMacで動く環境までと、wikipediaのデータを加工して学習させるまで。

参考サイト

  • wikipediaのデータを加工するまで
    • word2vecを使って、日本語wikipediaのデータを学習する - Qiita
  • word2vecのインストール、実行まで
    • 今からでもword2vecを触ってみよう - わいさわのエンジニアリング
  • word2vecにmecabで分かち書きしたwikipediaを学習させる
    • Word2Vec, MeCab, ComeJisyo で病気の症状類似語を出してみた - Qiita

word2vecのインストール

word2vecはこちらからgit cloneしたらうまく行った

dav/word2vec: This tool provides an efficient implementation of the continuous bag-of-words and skip-gram architectures for computing vector representations of words. These representations can be subsequently used in many natural language processing applications and for further research.

1
$ git clone git@github.com:dav/word2vec.git word2vec

word2vecデモコードの実行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
$ cd scripts && ./demo-word.sh

--
Enter word or sentence (EXIT to break): venture

Word: venture  Position in vocabulary: 6603

                                              Word       Cosine distance
------------------------------------------------------------------------
                                          ventures		0.599036
                                           company		0.483345
                                       investments		0.458870
                                            buyout		0.451463
                                        subsidiary		0.431377
                                         companies		0.430938
                                              firm		0.421534
                                            unocal		0.414683
                                              amex		0.414066
                                      conglomerate		0.413784
                                             joint		0.396755
                                              nyse		0.396211
                                        mitsubishi		0.384251
                                      crosscountry		0.383103
                                           bechtel		0.382134
                                           kleiner		0.379211
                                   daimlerchrysler		0.378791
                                          marketer		0.376548
                                              coms		0.376233
                                         financing		0.374592
                                         investors		0.373564
                                             matra		0.373524
                                            invest		0.370923
                                          sumitomo		0.369687
                                      subsidiaries		0.369541
                                              tata		0.369004
                                            paypal		0.366303
                                               psa		0.364887
                                              corp		0.364374
                                         investing		0.363979
                                          keiretsu		0.363619
                                             nynex		0.363340
                                        contractor		0.360837
                                            prisma		0.359162
                                             lycos		0.357675
                                               plc		0.357285
                                          refinery		0.356072
                                       corporation		0.354995
                                     conglomerates		0.354956
                                          holdings		0.353605




Enter word or sentence (EXIT to break): programming

Word: programming  Position in vocabulary: 876

                                              Word       Cosine distance
------------------------------------------------------------------------
                                         scripting		0.638737
                                         smalltalk		0.609791
                                               tcl		0.600699
                                            matlab		0.577854
                                           fortran		0.532493
                                           iverson		0.532436
                                              lisp		0.531992
                                       applescript		0.529222
                                       prototyping		0.522920
                                               awk		0.522085
                                      polymorphism		0.520571
                                             mumps		0.514660
                                              rexx		0.514517
                                          intercal		0.514188
                                              bcpl		0.512607
                                             ocaml		0.512148
                                       concurrency		0.511565
                                        programmer		0.509215
                                               oop		0.507332
                                           haskell		0.506930
                                               clu		0.506697
                                           jscript		0.505970
                                       interfacing		0.502187
                                               vba		0.493627
                                            markup		0.492610
                                       programmers		0.488856
                                        stroustrup		0.488107
                                           niklaus		0.486320
                                               apl		0.482244
                                              cweb		0.480151
                                          software		0.479891
                                              vhdl		0.479674
                                          blitzmax		0.478465
                                          datatype		0.477591
                                            prolog		0.475617
                                    implementation		0.473823
                                             booch		0.473752
                                         assembler		0.473023
                                         compilers		0.472527
                                            modula		0.472105

Wikipediaデータ関連

Wikipediaの日本語データのダウンロード

2.4GBあり、1時間くらいかかった

1
$ curl https://dumps.wikimedia.org/jawiki/latest/jawiki-latest-pages-articles.xml.bz2 -o jawiki-latest-pages-articles.xml.bz2

データの加工のためのruby製wp2txtインストール

WikipediaのデータがXMLのため、txtに変換させるライブラリをインストールする

1
gem install wp2txt bundler

wp2txtを実行しXMLをテキストにコンバート、加工する

実行完了まで1時間以上かかる。 517ファイル作られていた。

1
2
3
4
5
$ wp2txt --input-file jawiki-latest-pages-articles.xml.bz2

# 実行結果:こんな感じの連番のファイルが生成される
start:  jawiki-latest-pages-articles.xml-001.txt  
finish: jawiki-latest-pages-articles.xml-517.txt

連番のファイルをひとつのファイルにまとめる

1
$ cat jawiki-latest-pages-articles.xml-* > jawiki_wakati.txt

mecabを使ってコーパスファイルの作成

テキストにはコンバートできたけど、実際に使うためには文章がダラダラと書かれている状態なので mecabを使って、単語だけを切り出すように加工する。

homebrewを使ってmecabのインストール

1
$ brew install mecab

mecab実行コマンドを試す:mecabの分かち書きコマンド mecab -Owakatiを実行

コンソール的な標準入力・標準出力なモードになるので、適当な文章を入れる。・

1
2
3
4
5
6
7
8
9
$ mecab -Owakati

<入力待機状態>

今日はお疲れ様でした。明日も頑張りましょう。            ←  入力した文章
今日 は お疲れ様 でし た 。 明日 も 頑張り ましょ う 。 ←  結果

こんにちは、私の名前は横山 彰子です。                   ←  入力した文章
こんにちは 、 私 の 名前 は 横山 彰子 です 。           ←  結果

mecabを使って単語だけ抽出する

1
2
3
4
5
6
7
$ mecab -Owakati jawiki_wakati.txt -o jawiki-mecab-data.txt

input-buffer overflow. The line is split. use -b #SIZE option.
input-buffer overflow. The line is split. use -b #SIZE option.
input-buffer overflow. The line is split. use -b #SIZE option.
input-buffer overflow. The line is split. use -b #SIZE option.

何度も input-buffer overflow. The line is split. use -b #SIZE option.が出てしまった。

バッファサイズ指定し、再実行-bオプションを使い再挑戦

1
$ mecab -Owakati -b 100000 jawiki_wakati.txt -o jawiki-mecab-data.txt

word2vecにmecabの分かち書きデータを学習させる

1
$ time ./word2vec -train jawiki-mecab-data.txt -output jawiki_wakati_mecab.bin -cbow 0 -size 200 -window 5 -negative 0 -hs 1 -sample 1e-3 -binary 1

バックアップ

これは古い方。 Wikipediaデータの学習、単語だけじゃなくてフルだから結果おかしくなった

1
2
3
4
5
6
$ word2vec -train jawiki_wakati.txt -output jawiki_wakati.bin -size 200 -window 5 -sample 1e-3 -negative 5 -hs 0 -binary 1
--
Starting training using file jawiki_wakati.txt
Vocab size: 1213139
Words in train file: 157879973
Alpha: 0.000005  Progress: 100.00%  Words/thread/sec: 182.72k  %

wikipediaの分かち書き

1
word2vec-distance jawiki_wakati.bin
user-image
Akiko yokoyama in Coding
10 minute read

Similar Posts

Actually Made This Blog Multilingual Three Years Ago

Review of the Leica Q2

Re-Builded the Blog with Jekyll + Netlify + Github

Excel Tips vol.1

Setup for WSL / Windows Subsystem for Linux + Ubuntu

user-image

Published Feb 22, 2018

Akiko yokoyama in Coding

Also found in

  • Coding

Share this article

Actually Made This Blog Multilingual Three Years Ago

Review of the Leica Q2

Re-Builded the Blog with Jekyll + Netlify + Github

Excel Tips vol.1

Setup for WSL / Windows Subsystem for Linux + Ubuntu

Setting hubot adapter slack & chatwork

  • Home
  • Akiko Yokoyama
  • Contact
  • Feed