KeyedVectors API

We now have to perform the simple task of loading vectors from a file. We do this using the KeyedVectors API in Gensim. The word we want to look up is the key, and the numerical representation of that word is the corresponding value.

Let's first import the API and set up the target filename as follows:

from gensim.models import KeyedVectors
filename = word2vec_output_file

We will load the entire text file into our memory, thus including the read from disk time. In most running processes, this is a one-off I/O step and is not repeated for every new data pass. This becomes our Gensim model, detailed as follows:

%%time
# load the Stanford GloVe model from file, this is Disk I/O and can be slow
pretrained_w2v_model = KeyedVectors ...

Get Natural Language Processing with Python Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.