深度学习模型-快速构建词典和id的映射

直接上代码

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()))
原文地址:https://www.cnblogs.com/demo-deng/p/12367468.html