From b455c9194043d0c5f768dd6826ab1f33d57cc67e Mon Sep 17 00:00:00 2001 From: Tucker Morgan Date: Sat, 13 Mar 2021 16:45:04 -0800 Subject: [PATCH] Helper function for Vocab object --- torchtext/vocab.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/torchtext/vocab.py b/torchtext/vocab.py index 516f158a16..ddc159bded 100755 --- a/torchtext/vocab.py +++ b/torchtext/vocab.py @@ -103,6 +103,12 @@ def __init__(self, counter, max_size=None, min_freq=1, specials=('', ' def _default_unk_index(self): return self.unk_index + def get_token(self, index): + result = self.itos[index] + if result is None: + return Vocab.UNK + return result + def __getitem__(self, token): return self.stoi.get(token, self.stoi.get(Vocab.UNK))