直接上代码
from collections import Counter import numpy as np text = 'I love china. the dog on the ground' text = text.split() # print(text) vocab = dict(Counter(text).most_common(5)) vocab['<unk>'] = len(text) - np.sum(list(vocab.values())) id_to_word = [word for word in vocab.keys()] word_to_id = {word:i for i, word in enumerate(id_to_word)} print(word_to_id) # print(list(vocab.values()))