Skip to content

Instantly share code, notes, and snippets.

@hanneshapke
Created May 24, 2018 21:38
Show Gist options
  • Save hanneshapke/17e241e978d1c14cb1a895d89efa4f5b to your computer and use it in GitHub Desktop.
Save hanneshapke/17e241e978d1c14cb1a895d89efa4f5b to your computer and use it in GitHub Desktop.
Load word vectors into a redis db
import bz2
import pickle
from django.conf import settings
from djang_redis import get_redis_connection
from tqdm import tqdm
from .constants import GOOGLE_WORD2VEC_MODEL_NAME
def load_word2vec_into_redis(rs, wvmodel, key=GOOGLE_WORD2VEC_MODEL_NAME):
""" This function loops over all available words in the loaded word2vec model and loads
them into the redis instance via the rs object.
:param rs: redis connection object from django_redis
:param wvmodel: word vector model loaded into the memory of this machine.
Once the loading is completed, the memory will be available again.
:param key: suffix for the redis keys
"""
print("Update Word2Vec model in redis ...")
for word in tqdm(list(wvmodel.vocab.keys())):
rs.set(key + word, bz2.compress(pickle.dumps(wvmodel[word])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment