import re import nltk from wordcloud import WordCloud import matplotlib.pyplot as plt import io nltk.download("stopwords") from nltk.corpus import stopwords stop_words = set(stopwords.words("english")) def clean_text(text): text = re.sub(r"http\S+", "", text) # remove URLs text = re.sub(r"[^a-zA-Z\s]", "", text) # remove special chars text = text.lower() tokens = [w for w in text.split() if w not in stop_words] return " ".join(tokens) def generate_wordcloud(text): wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text) img = io.BytesIO() plt.figure(figsize=(8, 4)) plt.imshow(wordcloud, interpolation="bilinear") plt.axis("off") plt.savefig(img, format="png") plt.close() return img.getvalue()