def compact_word_vectors(self, vocab, filename=None, array=None,
                             use_spacy=True):
        """ Retrieve pretrained word spectors for our vocabulary.
        The returned word array has row indices corresponding to the
        compact index of a word, and columns correponding to the word
        vector.

        Arguments
        ---------
        vocab : dict
            Dictionary where keys are the loose index, and values are
            the word string.

        use_spacy : bool
            Use SpaCy to load in word vectors. Otherwise Gensim.

        filename : str
            Filename for SpaCy-compatible word vectors or if use_spacy=False
            then uses word2vec vectors via gensim.

        Returns
        -------
        data : numpy float array
            Array such that data[compact_index, :] = word_vector

        Examples
        --------
        >>> import numpy.linalg as nl
        >>> vocab = {19: 'shuttle', 5: 'astronomy', 7: 'cold', 3: 'hot'}
        >>> word_indices = np.zeros(50).astype('int32')
        >>> word_indices[:25] = 19  # 'Shuttle' shows 25 times
        >>> word_indices[25:35] = 5  # 'astronomy' is in 10 times
        >>> word_indices[40:46] = 7  # 'cold' is in 6 times
        >>> word_indices[46:] = 3  # 'hot' is in 3 times
        >>> corpus = Corpus()
        >>> corpus.update_word_count(word_indices)
        >>> corpus.finalize()
        >>> v, s, f = corpus.compact_word_vectors(vocab)
        >>> sim = lambda x, y: np.dot(x, y) / nl.norm(x) / nl.norm(y)
        >>> vocab[corpus.compact_to_loose[2]]
        'shuttle'
        >>> vocab[corpus.compact_to_loose[3]]
        'astronomy'
        >>> vocab[corpus.compact_to_loose[4]]
        'cold'
        >>> sim_shuttle_astro = sim(v[2, :], v[3, :])
        >>> sim_shuttle_cold = sim(v[2, :], v[4, :])
        >>> sim_shuttle_astro > sim_shuttle_cold
        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 __HOLE__:
                        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
dataset/ETHPy150Open cemoody/lda2vec/lda2vec/corpus.py/Corpus.compact_word_vectors