13
$\begingroup$

Where can I find a reliable word2vec model trained on some English articles?

I need a word2vec black box, where I, for example, can pass a sentence as array: ["London", "is", "the", "capital", "of", "Great", "Britain"]

and receive: [some_vector_of_floats1, some_vector_of_floats2, some_vector_of_floats3, some_vector_of_floats4, some_vector_of_floats5, some_vector_of_floats6, some_vector_of_floats7]

$\endgroup$

2 Answers 2

18
$\begingroup$

In Python, you can use Gensim

import gensim
model = gensim.models.Word2Vec.load_word2vec_format('path-to-vectors.txt', binary=False)
# if you vector file is in binary format, change to binary=True
sentence = ["London", "is", "the", "capital", "of", "Great", "Britain"]
vectors = [model[w] for w in sentence]

These vectors should give you better performance than the pre-trained ones you'd get with word2vec.

$\endgroup$
12
  • $\begingroup$ thank you very much. it probably works, but I receive the error KeyError: 'of' $\endgroup$ Mar 13, 2017 at 14:05
  • $\begingroup$ That means of is not in your model's vocabulary. Which vectors are you using? $\endgroup$
    – Alexandre
    Mar 13, 2017 at 14:06
  • $\begingroup$ GoogleNews-vectors-negative300.bin $\endgroup$ Mar 13, 2017 at 14:06
  • $\begingroup$ I use that model in node-word2vec and it works there with sentence about London $\endgroup$ Mar 13, 2017 at 14:09
  • $\begingroup$ Did you change binary=True to binary=False as noted? The pre-trained Google vectors are in binary format. $\endgroup$
    – Alexandre
    Mar 13, 2017 at 14:16
1
$\begingroup$

I slightly modified the code - for my purposes

vocab = model.vocab.keys()
sentence = ["London", "is", "the", "capital", "of", "Great", "Britain"]
vectors=[]
for w in sentence:
    if w in vocab:
        vectors.append(model[w])
    else:
        print("Word {} not in vocab".format(w))
        vectors.append([0])

You could also use a try/catch - your call.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.