So, I’m creating two random word generators, one based on bigrams and the other based on trigrams. In each case I’ve set up a dictionary (either called bigrams, which has two nested dictionaries or trigrams, which has three nested dictionaries)…and there is a lot of other code but here’s the line that causes a problem in the trigram generator:
#generates random phonemes
def generate_trigramphoneme(phoneme1, phoneme2):
rand = random.uniform(0,1)
**for phoneme3 in trigrams[phoneme1][phoneme2]:**
rand -= trigrams[phoneme1][phoneme2][phoneme3]
if rand < 0.0: return phoneme3
return phoneme3
where the variable “phoneme3” produces a local unbound error.
Here, though, in my bigram generator (which works), the variable “Following” is fine, and doesn’t produce an error:
def generate_bigramphoneme(phoneme):
rand = random.uniform(0,1)
for following in bigrams[phoneme]:
rand -= bigrams[phoneme][following]
if rand < 0.0: return following
return following
I looked up unbound local errors in python on eli bendersky’s website, which helped me understand the error, but I still don’t know how to get rid of it, or why the bigram code doesn’t produce an error…
Assuming you do have a
trigramsdefined somewhere, it may be that with your arguments,trigrams[phoneme1][phoneme2]is an empty iterable, therefore the loop never executes andphoneme3doesn’t get bound.