word2vecとgensimの環境構築と動作確認

Linuxの環境を前提とする。

コマンドラインで以下のコードを実行して、環境構築。

git clone https://github.com/svn2github/word2vec.git  
cd word2vec  
sed -i 's/<malloc.h>/<stdlib.h>/g' compute-accuracy.c  
sed -i 's/<malloc.h>/<stdlib.h>/g' distance.c  
sed -i 's/<malloc.h>/<stdlib.h>/g' word-analogy.c  
make  
chmod +x *.sh   
  
wget http://public.shiroyagi.s3.amazonaws.com/latest-ja-word2vec-gensim-model.zip > latest-ja-word2vec-gensim-model.zip  
unzip latest-ja-word2vec-gensim-model.zip
  
pip install gensim  

Pythonを起動して、以下のコードで動作確認。

from gensim.models.word2vec import Word2Vec  
model_path = 'word2vec.gensim.model'  
model = Word2Vec.load(model_path)  
for r in model.wv.most_similar(positive=['野球']):  
    print(r)