LoginSignup
5
1

More than 3 years have passed since last update.

word2vecに「吾輩は猫である」を食わせた話

Last updated at Posted at 2019-04-27

目標

漱石先生をもっとよく知るために、

  • 青空文庫から名作「吾輩は猫である」を拝借
  • mecabで分解
  • word2vec

をやる。
意外とめんどくさかったのでメモ。

環境

AMIID:amzn-ami-hvm-2018.03.0.20181129-x86_64-gp2 (ami-0cd3dfa4e37921605)
形態素解析:mecab-0.996
ベクトル解析:word2vec Latest commit 99e546e on 31 Jan 2015

吾輩は猫である

ShiftJISのルビあり版を落としてくる→utf8に変換
ルビはそのまま残す
本文だけほしいのでヘッダ部分を削除する

$ wget https://www.aozora.gr.jp/cards/000148/files/789_ruby_5639.zip
$ unzip 789_ruby_5639.zip
$ iconv -f SJIS -t utf8 wagahaiwa_nekodearu.txt > wagahaiwa_nekodearu_utf8.txt
$ vi wagahaiwa_nekodearu_utf8.txt

編集したファイルをmecabで分解

mecabが「input-buffer overflow. The line is split. use -b #SIZE option.」を吐くときはバッファサイズを-bオプションで指定。
《 》で囲まれたルビをケアしてみる。

$ cat wagahaiwa_nekodearu_utf8.txt | mecab -Owakati -b 64000 > wagahaiwa_nekodearu_utf8_w.txt
$ cat wagahaiwa_nekodearu_utf8_w.txt | sed 's/《 /《/g' | sed 's/ 》/》/g' > wagahaiwa_nekodearu_utf8_w_ruby.txt

word2vec

demo用のスクリプトが用意されているのでそれをパクる

$ git clone https://github.com/svn2github/word2vec.git
$ cd word2vec/
$ cp -p demo-word.sh wagahaiha.sh
$ chmod +x wagahaiha.sh
$ vi wagahaiha.sh
wagahaiha.sh
make
trainfile=../wagahaiwa_nekodearu_utf8_w_ruby.txt
time ./word2vec -train $trainfile -output vectors.bin -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 20 -binary 1 -iter 15
./distance vectors.bin

実行してみる

$ ./wagahaiha.sh
make: Nothing to be done for `all'.
Starting training using file ../wagahaiwa_nekodearu_utf8_w_ruby.txt
Vocab size: 4050
Words in train file: 209578
Alpha: 0.036586  Progress: 27.59%  Words/thread/sec: 138.34k

猫は自由

Enter word or sentence (EXIT to break): 猫

Word: 猫  Position in vocabulary: 94

                                              Word       Cosine distance
------------------------------------------------------------------------
                                            自由                0.938686
                                            人間                0.938450
                                            世間                0.934593
                                      に対して          0.933217
                                            必要                0.929376
                                            結果                0.928381
                                               者               0.926694
                                            個性                0.924676

吾輩は鏡が好き

Enter word or sentence (EXIT to break): 吾輩

Word: 吾輩  Position in vocabulary: 48

                                              Word       Cosine distance
------------------------------------------------------------------------
                                               鏡               0.881376
                                            なき                0.878047
                                               点               0.876055
                                            運動                0.872787
                                            ため                0.870062
                                            動物                0.868611
                                            観察                0.861668
                                            あろ                0.860611
                                            普通                0.857614
                                            のみ                0.849649
                                      あらわれ          0.845795
                                            もっ                0.845196
                                            何等                0.845080
                                            逆上                0.843071

先生といえばくしゃみ先生

Enter word or sentence (EXIT to break): 先生

Word: 先生  Position in vocabulary: 81

                                              Word       Cosine distance
------------------------------------------------------------------------
                                《くしゃみ》            0.909737
                                            沙弥                0.878514
                                               苦               0.873232
                                               話               0.833417
                                         たまえ         0.832324
                                            寒月                0.823155
                                            鈴木                0.810209
                                            水島                0.804593
                                            甘木                0.803114
                                      《さい》          0.799587
                                            八木                0.799231
                                            三平                0.796375

深い。

Enter word or sentence (EXIT to break): 自由

Word: 自由  Position in vocabulary: 1649

                                              Word       Cosine distance
------------------------------------------------------------------------
                                            自殺                0.974958
                                            強く                0.969904
                                            世間                0.966709
                                      それだけ          0.964157
                                            個性                0.962231
                                            個人                0.961875
                       インスピレーション               0.961103
                                《けいべつ》            0.958479
5
1
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
5
1