-
Notifications
You must be signed in to change notification settings - Fork 2
/
didemo_vocab.py
95 lines (74 loc) · 2.78 KB
/
didemo_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Create a vocabulary wrapper
import nltk
import pickle
from collections import Counter
import json
import argparse
import os
from tqdm import tqdm
annotations = {
'didemo': ['../../../playground/vsepp-develop-develop/data/data/train_data.json', '../../../playground/vsepp-develop-develop/data/data/val_data.json', '../../../playground/vsepp-develop-develop/data/data/test_data.json']
}
class Vocabulary(object):
"""Simple vocabulary wrapper."""
def __init__(self):
self.word2idx = {}
self.idx2word = {}
self.idx = 0
def add_word(self, word):
if word not in self.word2idx:
if word in glove:
self.word2idx[word] = self.idx
self.idx2word[self.idx] = word
self.idx += 1
def __call__(self, word):
if word not in self.word2idx:
return self.word2idx['UNK']
return self.word2idx[word]
def __len__(self):
return len(self.word2idx)
def build_vocab(data_path, data_name, jsons, threshold):
global glove
"""Build a simple vocabulary wrapper."""
counter = Counter()
f_glove = open('/data1/bwzhang/glove.840B.300d.txt','r')
glove = {}
for line in tqdm(f_glove):
line = line[0:-1]
line_split = line.split(' ')
word = line_split[0]
glove[word] = 1
for path in jsons[data_name]:
full_path = os.path.join(path)
print full_path
dic = json.load(open(full_path,'r'))
for i in range(len(dic)):
sen = dic[i]['description']
tokens = nltk.tokenize.word_tokenize(sen.lower())
counter.update(tokens)
# Discard if the occurrence of the word is less than min_word_cnt.
words = [word for word, cnt in counter.items() if cnt >= threshold]
# Create a vocab wrapper and add some special tokens.
# vocab = pickle.load(open('vocab/coco_vocab.pkl','r'))
vocab = Vocabulary()
vocab.add_word('PAD')
vocab.add_word('BOS')
vocab.add_word('EOS')
vocab.add_word('UNK')
# Add words to the vocabulary.
for i, word in enumerate(words):
vocab.add_word(word)
print (len(vocab))
return vocab
def main(data_path, data_name):
vocab = build_vocab(data_path, data_name, jsons=annotations, threshold=1)
with open('./vocab/%s_precomp_vocab_total.pkl' % data_name, 'wb') as f:
pickle.dump(vocab, f, pickle.HIGHEST_PROTOCOL)
print("Saved vocabulary file to ", './vocab/%s_precomp_vocab_total.pkl' % data_name)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default='/w/31/faghri/vsepp_data/')
parser.add_argument('--data_name', default='didemo',
help='coco|anet|didemo')
opt = parser.parse_args()
main(opt.data_path, opt.data_name)