word2vec demo code raise IndexError
Created by: jacquesqiao
When I run the example script in the documentation for 01.word2vec
, it gives me an IndexError
from scipy import spatial
import numpy
def load_dict_and_embedding():
word_dict = dict()
with open("word_dict", "r") as f:
for line in f:
key, value = line.strip().split(" ")
word_dict[key] = value
embeddings = numpy.loadtxt("embedding_table", delimiter=",")
return word_dict, embeddings
# load word dict and embedding table
word_dict, embedding_table = load_dict_and_embedding()
print(spatial.distance.cosine(embedding_table[word_dict['car']], embedding_table[word_dict['world']]))
print(spatial.distance.cosine(embedding_table[word_dict['say']], embedding_table[word_dict['talking']]))