WordCloud 'local variable 'n' referenced before assignment

286 views Asked by At

I am a newbie with python and I am trying to define a function that will return a WordCloud of job tokens from a specific city_state. The code runs independently, but as a function, I can't figure out how to fix the error. I am getting a

UnboundLocalError: local variable 'comment_words' referenced before assignment

def jobs_wordcloud(city_state):
cols = ['base_tokens']
for val in jobs_df.loc[jobs_df['city_state'] == city_state, cols]:
    val = str(val)
    tokens = word_tokenize(val)
    tokens = re.sub('[^a-zA-Z 0-9]', '', val)
    tokens = tokens.lower().split()
    lemmatizer = WordNetLemmatizer()
    tokens = [w for w in tokens if not w in stop_words]
    tokens = [lemmatizer.lemmatize(w.lower().strip()) for w in tokens]
    comment_words += " ".join(tokens) + " "

wordcloud = WordCloud(width = 800, height = 800,
    background_color = 'white',
    stopwords = stop_words,
    min_font_size = 10).generate(comment_words)
  
plt.figure(figsize = (10, 10), facecolor = None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()

return plt.show()

This is the error I am getting:

UnboundLocalError                         Traceback (most recent call last)
<ipython-input-31-8df36e8cfe41> in <module>()
----> 1 jobs_wordcloud('San Francisco, CA')

<ipython-input-30-1a42912ec34f> in jobs_wordcloud(city_state)
     11         tokens = [w for w in tokens if not w in stop_words]
     12         tokens = [lemmatizer.lemmatize(w.lower().strip()) for w in tokens]
---> 13         comment_words += " ".join(tokens) + " "
     14 
     15     wordcloud = WordCloud(width = 800, height = 800,

UnboundLocalError: local variable 'comment_words' referenced before assignment
1

There are 1 answers

0
lllrnr101 On BEST ANSWER

Define comment_words = '' before you begin the loop in the jobs_wordcloud function.

Since you are effectively doing comment_words = comment_words + SOMETHING, python needs to know what is the value of comment_words.