[PYTHON] Easy! Use gensim and word2vec with MAMP.

**Note! It is intended for environments where python, mamp, and gensim are installed. If you can't, please install it. ** ** ** Also, instead of learning word2vec, just look for similar words. ** **

First, connect to python from php.

Put this in php ~! The code exec can access the command line from php. (The same one as the mac terminal.)


<?php
exec('ls')
?>

Use this to run python with word2vec written on it. For example ..., you can run a python file like test.py from php.


<?php
exec('python test.py')
?>

Then bring the data to look for similar words in word2vec. Let's download the zip. http://www.cl.ecei.tohoku.ac.jp/~m-suzuki/jawiki_vector/ I will use this later.

Next, on the python side, create a new python file. Write like this. This is ok ~! Please answer the zip you downloaded earlier and place it nearby. That is "entity_vector / entity_vector.model.bin". ** (If you do not delete the commented out part in Japanese now, an error will occur when reading, so please delete it! (Maybe you can read it if you devise it. I think there is something.)) **

I have to pass the path with sys.path as follows .. I get the error "Import Error: No module named'gensim'". This means that the location to look for the module set in mamp is different. So let's find the path of gensim already installed in mac in the terminal. (Pip show gensim)

word2vec.py


#I can import gensim models with python on a mac terminal, but there is no path on mamp.
#So sys.I will enter the path with path.
import sys
sys.path.append('path')
#In the "Path" section above, enter the absolute path to look up next. (Example:/Users/Taro/anaconda3/lib/python3.6/site-packages)

#Below is the code that executes word2vec.
#Look for words that are similar to "graphic".
from gensim.models import KeyedVectors
model_dir = 'entity_vector/entity_vector.model.bin'
model = KeyedVectors.load_word2vec_format(model_dir, binary=True)

results = model.similar_by_vector("graphic", topn=10, restrict_vocab=None)

for result in results:
    print(result)

Now back to php. Execute the command and execute the python file with exec (). The second argument is the result. In the third argument, if there is an error, something will be returned. Now .. the result is ..

index.php


$command = 'python wd2vc.py';
exec($command, $output, $return_var);
var_dump($output, $return_var);

I got it! !! Words similar to "graphic" are output as an array. You did it!

image.png

Reference article: PHP --Summary of the relationship between exec () error handling and standard error output https://qiita.com/smd8122/items/65b552f1d53bfb7fad9a I was a little addicted to using Mecab from PHP in MAMP! https://dbym4820.hatenablog.com/entry/2017/10/18/171259

Recommended Posts

Easy! Use gensim and word2vec with MAMP.
Easy to use Nifty Cloud API with botocore and python
Easy modeling with Blender and Python
Use MeCab and neologd with Google Colab
Use Jupyter Lab and Jupyter Notebook with EC2
Use TPU and Keras with Google Colaboratory
Easy web scraping with Python and Ruby
Use Python and MeCab with Azure Functions
100 language processing knock-90 (using Gensim): learning with word2vec
[Python] I introduced Word2Vec and played with it.
Use dein.vim and ckw-mod with Windows7 32bit PowerShell
Word2Vec with BoUoW
Easy X-Ray with Lambda Layer and CloudFormation / sam-cli
"Learning word2vec" and "Visualization with Tensorboard" on Colaboratory
Easy IoT to start with Raspberry Pi and MESH
[Introduction to WordCloud] It's easy to use even with Jetson-nano ♬
Easy generation of stylistic pakuri sentences with MeCab + gensim
Easy partial download of mp4 with python and youtube-dl!
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Easy face recognition try with Jetson Nano and webcam
Install tweepy with pip and use it for API 1.1
Easy Grad-CAM with pytorch-gradcam
Use mecab-ipadic-neologd with igo-python
Use RTX 3090 with PyTorch
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use indicator with pd.merge
Easy to use Flask
Use mecab with Python3
Use tensorboard with Chainer
Use SQLAlchemy and multiprocessing
Use pip with MSYS2
Use Python 3.8 with Anaconda
Use pyright with Spacemacs
Use python with docker
Use TypeScript with django-compressor
Use LESS with Django
Easy to use SQLite3
Use MySQL with Django
Use Enums with SQLAlchemy
Easy debugging with ipdb
With and without WSGI
Use tensorboard with NNabla
Use GPS with Edison
Easy TopView with OpenCV
Use nim with Jupyter
Easy introduction to home hack with Raspberry Pi and discord.py
I tried follow management with Twitter API and Python (easy)
Reinforcement learning 23 Create and use your own module with Colaboratory
Let's move word2vec with Chainer and see the learning progress
Easy deep learning web app with NNC and Python + Flask