Spaces:
Runtime error
Runtime error
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import nltk
|
| 3 |
+
from wordcloud import WordCloud
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
nltk.download("stopwords")
|
| 8 |
+
from nltk.corpus import stopwords
|
| 9 |
+
|
| 10 |
+
stop_words = set(stopwords.words("english"))
|
| 11 |
+
|
| 12 |
+
def clean_text(text):
|
| 13 |
+
text = re.sub(r"http\S+", "", text) # remove URLs
|
| 14 |
+
text = re.sub(r"[^a-zA-Z\s]", "", text) # remove special chars
|
| 15 |
+
text = text.lower()
|
| 16 |
+
tokens = [w for w in text.split() if w not in stop_words]
|
| 17 |
+
return " ".join(tokens)
|
| 18 |
+
|
| 19 |
+
def generate_wordcloud(text):
|
| 20 |
+
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
|
| 21 |
+
img = io.BytesIO()
|
| 22 |
+
plt.figure(figsize=(8, 4))
|
| 23 |
+
plt.imshow(wordcloud, interpolation="bilinear")
|
| 24 |
+
plt.axis("off")
|
| 25 |
+
plt.savefig(img, format="png")
|
| 26 |
+
plt.close()
|
| 27 |
+
return img.getvalue()
|