From badc35cac508424daf4546e27d207d8a19d7a529 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Oct 2025 13:10:36 +0000 Subject: [PATCH] Fix AttributeError for GloVe vocab in encode function Co-authored-by: leestott <2511341+leestott@users.noreply.github.com> --- lessons/5-NLP/14-Embeddings/torchnlp.py | 6 +++++- lessons/5-NLP/16-RNN/torchnlp.py | 6 +++++- lessons/5-NLP/17-GenerativeNetworks/torchnlp.py | 6 +++++- lessons/5-NLP/18-Transformers/torchnlp.py | 6 +++++- 4 files changed, 20 insertions(+), 4 deletions(-) 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)]