diff --git a/lessons/5-NLP/14-Embeddings/torchnlp.py b/lessons/5-NLP/14-Embeddings/torchnlp.py index e8563177..76870f56 100644 --- a/lessons/5-NLP/14-Embeddings/torchnlp.py +++ b/lessons/5-NLP/14-Embeddings/torchnlp.py @@ -30,7 +30,11 @@ def encode(x,voc=None,unk=0,tokenizer=tokenizer): if v in stoi_hash.keys(): stoi = stoi_hash[v] else: - stoi = v.get_stoi() + # Handle both vocab.vocab objects (with get_stoi() method) and GloVe objects (with stoi attribute) + if hasattr(v, 'get_stoi'): + stoi = v.get_stoi() + else: + stoi = v.stoi stoi_hash[v]=stoi return [stoi.get(s,unk) for s in tokenizer(x)] diff --git a/lessons/5-NLP/16-RNN/torchnlp.py b/lessons/5-NLP/16-RNN/torchnlp.py index e8563177..76870f56 100644 --- a/lessons/5-NLP/16-RNN/torchnlp.py +++ b/lessons/5-NLP/16-RNN/torchnlp.py @@ -30,7 +30,11 @@ def encode(x,voc=None,unk=0,tokenizer=tokenizer): if v in stoi_hash.keys(): stoi = stoi_hash[v] else: - stoi = v.get_stoi() + # Handle both vocab.vocab objects (with get_stoi() method) and GloVe objects (with stoi attribute) + if hasattr(v, 'get_stoi'): + stoi = v.get_stoi() + else: + stoi = v.stoi stoi_hash[v]=stoi return [stoi.get(s,unk) for s in tokenizer(x)] diff --git a/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py b/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py index e8563177..76870f56 100644 --- a/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py +++ b/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py @@ -30,7 +30,11 @@ def encode(x,voc=None,unk=0,tokenizer=tokenizer): if v in stoi_hash.keys(): stoi = stoi_hash[v] else: - stoi = v.get_stoi() + # Handle both vocab.vocab objects (with get_stoi() method) and GloVe objects (with stoi attribute) + if hasattr(v, 'get_stoi'): + stoi = v.get_stoi() + else: + stoi = v.stoi stoi_hash[v]=stoi return [stoi.get(s,unk) for s in tokenizer(x)] diff --git a/lessons/5-NLP/18-Transformers/torchnlp.py b/lessons/5-NLP/18-Transformers/torchnlp.py index e8563177..76870f56 100644 --- a/lessons/5-NLP/18-Transformers/torchnlp.py +++ b/lessons/5-NLP/18-Transformers/torchnlp.py @@ -30,7 +30,11 @@ def encode(x,voc=None,unk=0,tokenizer=tokenizer): if v in stoi_hash.keys(): stoi = stoi_hash[v] else: - stoi = v.get_stoi() + # Handle both vocab.vocab objects (with get_stoi() method) and GloVe objects (with stoi attribute) + if hasattr(v, 'get_stoi'): + stoi = v.get_stoi() + else: + stoi = v.stoi stoi_hash[v]=stoi return [stoi.get(s,unk) for s in tokenizer(x)]