Skip to content

Commit

Permalink
modifying default index API to accept None (#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
parmeet authored May 17, 2021
1 parent 3858bae commit ccfba91
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions test/test_vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def test_default_index(self):
v.set_default_index(0)
self.assertEqual(v['not in vocab'], 0)

v.set_default_index(None)
with self.assertRaises(RuntimeError):
v['not in vocab']

def test_default_index_jit(self):
token_to_freq = {'<unk>': 2, 'a': 2, 'b': 2}
sorted_by_freq_tuples = sorted(token_to_freq.items(), key=lambda x: x[1], reverse=True)
Expand All @@ -80,6 +84,10 @@ def test_default_index_jit(self):
v_jit = torch.jit.script(v)
self.assertEqual(v_jit['not in vocab'], 0)

v_jit.set_default_index(None)
with self.assertRaises(RuntimeError):
v_jit['not in vocab']

def test_vocab_insert_token(self):
c = OrderedDict({'<unk>': 2, 'a': 2})

Expand Down
2 changes: 1 addition & 1 deletion torchtext/csrc/vocab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int64_t Vocab::__getitem__(const c10::string_view &token) const {
return default_index_.value();
}

void Vocab::set_default_index(int64_t index) { default_index_ = index; }
void Vocab::set_default_index(c10::optional<int64_t> index) { default_index_ = index; }

c10::optional<int64_t> Vocab::get_default_index() const {
return default_index_;
Expand Down
2 changes: 1 addition & 1 deletion torchtext/csrc/vocab.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Vocab : torch::CustomClassHolder {
int64_t __len__() const;
int64_t __getitem__(const c10::string_view &token) const;
bool __contains__(const c10::string_view &token) const;
void set_default_index(int64_t index);
void set_default_index(c10::optional<int64_t> index);
c10::optional<int64_t> get_default_index() const;
void reassign_token(const std::string &token, const int64_t &index);
void insert_token(const std::string &token, const int64_t &index);
Expand Down
2 changes: 1 addition & 1 deletion torchtext/vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __getitem__(self, token: str) -> int:
return self.vocab[token]

@torch.jit.export
def set_default_index(self, index: int) -> None:
def set_default_index(self, index: Optional[int]) -> None:
r"""
Args:
index: Value of default index. This index will be returned when OOV token is queried.
Expand Down

0 comments on commit ccfba91

Please sign in to comment.