-
Notifications
You must be signed in to change notification settings - Fork 49
/
build_vocab.py
72 lines (57 loc) · 2.03 KB
/
build_vocab.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from os.path import join
import pickle as pkl
from collections import Counter
import argparse
START_TOKEN = 0
PAD_TOKEN = 1
END_TOKEN = 2
UNK_TOKEN = 3
# buid sign2id
class Vocab(object):
def __init__(self):
self.sign2id = {"<s>": START_TOKEN, "</s>": END_TOKEN,
"<pad>": PAD_TOKEN, "<unk>": UNK_TOKEN}
self.id2sign = dict((idx, token)
for token, idx in self.sign2id.items())
self.length = 4
def add_sign(self, sign):
if sign not in self.sign2id:
self.sign2id[sign] = self.length
self.id2sign[self.length] = sign
self.length += 1
def __len__(self):
return self.length
def build_vocab(data_dir, min_count=10):
"""
traverse training formulas to make vocab
and store the vocab in the file
"""
vocab = Vocab()
counter = Counter()
formulas_file = join(data_dir, 'im2latex_formulas.norm.lst')
with open(formulas_file, 'r') as f:
formulas = [formula.strip('\n') for formula in f.readlines()]
with open(join(data_dir, 'im2latex_train_filter.lst'), 'r') as f:
for line in f:
_, idx = line.strip('\n').split()
idx = int(idx)
formula = formulas[idx].split()
counter.update(formula)
for word, count in counter.most_common():
if count >= min_count:
vocab.add_sign(word)
vocab_file = join(data_dir, 'vocab.pkl')
print("Writing Vocab File in ", vocab_file)
with open(vocab_file, 'wb') as w:
pkl.dump(vocab, w)
def load_vocab(data_dir):
with open(join(data_dir, 'vocab.pkl'), 'rb') as f:
vocab = pkl.load(f)
print("Load vocab including {} words!".format(len(vocab)))
return vocab
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Building vocab for Im2Latex")
parser.add_argument("--data_path", type=str,
default="./data/", help="The dataset's dir")
args = parser.parse_args()
vocab = build_vocab(args.data_path)