0
votes
PAD = 0
UNK = 1
START = 2
END = 3
def make_vocab(wc, vocab_size):
    word2id, id2word = {}, {}
    word2id['<pad>'] = PAD
    word2id['<unk>'] = UNK
    word2id['<start>'] = START
    word2id['<end>'] = END
    for i, (w, _) in enumerate(wc.most_common(vocab_size), 4):
        word2id[w] = i
    return word2id

I got this error "AttributeError: 'Word2Vec' object has no attribute 'most_common'" when calling this function. I tried with different version of gensim. Could you give me some hints to solve this.

1
As @cecil-cox notes, there's no most_common() method in Gensim. (Why did you think there was?) But also, what are you trying to do with this word2id dict and why? (If those tokens are in your training data, they'll get their own positions which should be just as good as 0-3. If they're not in your training data, reserving them positions could make other things broken/nonsensical/fragile.) - gojomo

1 Answers

1
votes

Gensim's Word2Vec doesn't contain a most_common method.

If, for whatever reason you must extract word,frequency pairs from your model you can use

[(word, wc.w2v.vocab[word]) for word in wc.wv.vocab]

and sort the resulting list. This is a decidedly strange use case, however.