diff --git a/README.md b/README.md index ec4d9b9..e943690 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ python word2vec-api --model path/to/the/model [--host host --port 1234] * Example calls ``` -curl http://127.0.0.1:5000/wor2vec/n_similarity?ws1=Sushi&ws1=Shop&ws2=Japanese&ws2=Restaurant -curl http://127.0.0.1:5000/wor2vec/similarity?w1=Sushi&w2=Japanese -curl http://127.0.0.1:5000/wor2vec/most_similar?positive=indian&positive=food[&negative=][&topn=] -curl http://127.0.0.1:5000/wor2vec/model?word=restaurant +curl http://127.0.0.1:5000/word2vec/n_similarity?ws1=Sushi&ws1=Shop&ws2=Japanese&ws2=Restaurant +curl http://127.0.0.1:5000/word2vec/similarity?w1=Sushi&w2=Japanese +curl http://127.0.0.1:5000/word2vec/most_similar?positive=indian&positive=food[&negative=][&topn=] +curl http://127.0.0.1:5000/word2vec/model?word=restaurant ``` Note: The "model" method returns a base64 encoding of the Word2Vec vector. diff --git a/word2vec-api.py b/word2vec-api.py index 66a60e6..6c2c4d7 100644 --- a/word2vec-api.py +++ b/word2vec-api.py @@ -11,7 +11,7 @@ from gensim.models.word2vec import Word2Vec as w from gensim import utils, matutils from numpy import exp, dot, zeros, outer, random, dtype, get_include, float32 as REAL,\ - uint32, seterr, array, uint8, vstack, argsort, fromstring, sqrt, newaxis, ndarray, empty, sum as np_sum + uint32, seterr, array, uint8, vstack, argsort, fromstring, sqrt, newaxis, ndarray, empty, sum as np_sum import argparse import base64 import sys @@ -20,6 +20,8 @@ def filter_words(words): + if words is None: + return return [word for word in words if word in model.vocab] @@ -94,7 +96,7 @@ def raiseError(error): if __name__ == '__main__': global model - + #----------- Parsing Arguments --------------- p = argparse.ArgumentParser() p.add_argument("--model", help="Path to the trained model") @@ -102,12 +104,12 @@ def raiseError(error): p.add_argument("--host", help="Host name (default: localhost)") p.add_argument("--port", help="Port (default: 5000)") args = p.parse_args() - + model_path = args.model if args.model else "./model.bin.gz" binary = True if args.binary else False host = args.host if args.host else "localhost" port = int(args.port) if args.port else 5000 if not args.model: - print "Usage: wor2vec-apy.py --model path/to/the/model [--host host --port 1234]" + print "Usage: word2vec-apy.py --model path/to/the/model [--host host --port 1234]" model = w.load_word2vec_format(model_path, binary=binary) app.run(host=host, port=port)