Spaces:
Runtime error
Runtime error
Download utils.py from Pro-Coder/Sentiment_Analysis: direct link, hf CLI and curl.
- Browser
- Download file 796 Bytes
-
https://huggingface.co/spaces/Pro-Coder/Sentiment_Analysis/resolve/dc5b0227efed10285e52e776397fdcd79b4fa546/utils.py
- Command line
-
hf download hf://spaces/Pro-Coder/Sentiment_Analysis@dc5b0227efed10285e52e776397fdcd79b4fa546/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Pro-Coder/Sentiment_Analysis/resolve/dc5b0227efed10285e52e776397fdcd79b4fa546/utils.py
796 Bytes
| 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() | |