-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_dic.py
32 lines (27 loc) · 967 Bytes
/
make_dic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
from __future__ import print_function
from params import Params as pm
import codecs
import os
from collections import Counter
def make_dic(path, fname):
'''
Constructs vocabulary as a dictionary
Args:
path: [String], Input file path
fname: [String], Output file name
Build vocabulary line by line to dictionary/ path
'''
text = codecs.open(path, 'r', 'utf-8').read()
words = text.split()
wordCount = Counter(words)
if not os.path.exists('dictionary'):
os.mkdir('dictionary')
with codecs.open('dictionary/{}'.format(fname), 'w', 'utf-8') as f:
f.write("{}\t1000000000\n{}\t1000000000\n{}\t1000000000\n{}\t1000000000\n".format("<PAD>","<UNK>","<STR>","<EOS>"))
for word, count in wordCount.most_common(len(wordCount)):
f.write(u"{}\t{}\n".format(word, count))
if __name__ == '__main__':
make_dic(pm.src_train, "en.vocab.tsv")
make_dic(pm.tgt_train, "de.vocab.tsv")
print("MSG : Constructing Dictionary Finished!")