text_plain_output_1.png
import collections

import os

folder_dir = '/kaggle/input/108-2-ntut-dl-app-hw2'
filename = 'tag_list.txt'
vocabulary_size = 990

file_path = os.path.join(folder_dir, filename)
with open(file_path, 'r', encoding='utf-8') as f:
    words = f.read().split()

word_count = [['UNK', -1]]
word_count.extend(collections.Counter(words).most_common(vocabulary_size - 1))

dictionary = dict()
for word, _ in word_count:
    dictionary[word] = len(dictionary)
reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))

data = list()
unk_count = 0
for word in words:
    if word in dictionary:
        index = dictionary[word]
    else:
        index = 0
        unk_count += 1
    data.append(index)
word_count[0][1] = unk_count

print('Sample data corresponds to\n__________________')
for i in range(10):
    print('%d->%s' % (data[i], reverse_dictionary[data[i]]))