Jyothirmai commited on
Commit
8daae4c
1 Parent(s): caaba7e

Update tester.py

Browse files
Files changed (1) hide show
  1. tester.py +28 -0
tester.py CHANGED
@@ -14,6 +14,34 @@ from build_tag import *
14
  from build_vocab import Vocabulary, JsonReader
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  class CaptionSampler(object):
19
  def __init__(self):
 
14
  from build_vocab import Vocabulary, JsonReader
15
 
16
 
17
+ class Vocabulary(object):
18
+ def __init__(self):
19
+ self.word2idx = {}
20
+ self.id2word = {}
21
+ self.idx = 0
22
+ self.add_word('<pad>')
23
+ self.add_word('<end>')
24
+ self.add_word('<start>')
25
+ self.add_word('<unk>')
26
+
27
+ def add_word(self, word):
28
+ if word not in self.word2idx:
29
+ self.word2idx[word] = self.idx
30
+ self.id2word[self.idx] = word
31
+ self.idx += 1
32
+
33
+ def get_word_by_id(self, id):
34
+ return self.id2word[id]
35
+
36
+ def __call__(self, word):
37
+ if word not in self.word2idx:
38
+ return self.word2idx['<unk>']
39
+ return self.word2idx[word]
40
+
41
+ def __len__(self):
42
+ return len(self.word2idx)
43
+
44
+
45
 
46
  class CaptionSampler(object):
47
  def __init__(self):