def compact_word_vectors(self, vocab, filename=None, array=None,
                             use_spacy=True):
        if use_spacy:
            import spacy.en
            nlp = spacy.en.English()
            if filename:
                nlp.vocab.load_vectors(filename)
            n_dim = nlp.vocab.vectors_length
        else:
            from gensim.models.word2vec import Word2Vec
            model = Word2Vec.load_word2vec_format(filename, binary=True)
            n_dim = model.syn0.shape[1]
        n_words = len(self.compact_to_loose)
        data = np.random.randn((n_words, n_dim), dtype='float32')
        if not use_spacy:
            data -= model.syn0.mean()
            data /= data.std()
            data *= model.syn0.std()
        if array is not None:
            data = array
            n_words = data.shape[0]
        s, f = 0, 0
        for compact, loose in self.compact_to_loose.items():
            word = vocab.get(loose, None)
            if compact >= n_words:
                continue
            if word is None:
                continue
            word = word.strip()
            if use_spacy:
                token = nlp.vocab[unicode(word)]
                if not token.has_vector:
                    token = nlp.vocab[unicode(word.replace(' ', '_'))]
                if not token.has_vector:
                    f += 1
                    if f > 30:
                        print token.orth_
                    continue
                vector = token.vector
            else:
                vector = None
                try:
                    vector = model[word]
                except KeyError:
                    pass
                if vector is None:
                    try:
                        vector = model[word.replace(' ', '_')]
                    except KeyError:
                        pass
                if vector is None:
                    choices = model.vocab.keys()
                    try:
                        choice = difflib.get_close_matches(word, choices)[0]
                        vector = model[choice]
                        print word, ' --> ', choice
                    except IndexError:
                        pass
                if vector is None:
                    f += 1
                    continue
            s += 1
            data[compact, :] = vector[:]
        return data, s, f
Retrieve pretrained word spectors for our vocabulary.
dataset/ETHPy150Open cemoody/lda2vec/lda2vec/corpus.py/Corpus.compact_word_vectors