本文整理匯總了Python中utils.Vocab.add_word方法的典型用法代碼示例。如果您正苦於以下問題:Python Vocab.add_word方法的具體用法?Python Vocab.add_word怎麽用?Python Vocab.add_word使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utils.Vocab
的用法示例。
在下文中一共展示了Vocab.add_word方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: set
# 需要導入模塊: from utils import Vocab [as 別名]
# 或者: from utils.Vocab import add_word [as 別名]
if __name__ == "__main__":
#Create a set of all words
all_words = set()
vocab = Vocab()
count_files = 0
for name in ['test', 'train', 'val']:
filename = name + '_tokens.txt'
f = open(filename, 'r')
for line in f:
sp_line = line.strip().split()
for token in sp_line:
all_words.add(token)
vocab.add_word(token)
f.close()
glove_dir = '/media/sf_kickstarter/CS224D/Project/glove.840B.300d'
glove_f = open(os.path.join(glove_dir, 'glove.840B.300d.txt'), 'r')
embedding_matrix = np.zeros((len(vocab.word_to_index),300))
count = 0
for line in glove_f:
line_sp = line.strip().split()
word = line_sp[0]
line_sp_vec = [float(line_num) for line_num in line_sp[1:]]
if word in vocab.word_to_index:
line_sp_vec = [float(line_num) for line_num in line_sp[1:]]
index = vocab.word_to_index[word]