-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcloud.py
More file actions
63 lines (50 loc) · 1.38 KB
/
wordcloud.py
File metadata and controls
63 lines (50 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
"""Wordcloud
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1XFaD-5P5Vc8GMKKVUz_YoOgbzSpUYUKs
"""
import matplotlib.pyplot as plt
import numpy as np
from wordcloud import WordCloud
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import nltk
nltk.download('stopwords')
nltk.download('punkt')
with open("/content/text.txt", "r", encoding="UTF-8") as file:
text= file.read()
stop_words=set(stopwords.words("english"))
def data_preprocessing(data):
data=data.lower()
data_tokens=word_tokenize(data)
preprocessed_text=[w for w in data_tokens if not w in stop_words]
return " ".join(preprocessed_text)
data=data_preprocessing(text)
print(data)
word_cloud=WordCloud(
background_color="black",
colormap="viridis",
contour_color="steelblue",
contour_width=2
)
word_cloud.generate(data)
plt.figure(figsize=(12,12))
plt.imshow(word_cloud, interpolation='bilinear')
plt.axis("off")
plt.show()
"""**By using a Mask**"""
from PIL import Image
Mask=np.array(Image.open("/content/Twitter.png"))
word_cloud=WordCloud(
mask=Mask,
background_color="black",
colormap="viridis",
contour_color="steelblue",
contour_width=2
)
word_cloud.generate(data)
plt.figure(figsize=(12,12))
plt.imshow(word_cloud, interpolation='bilinear')
plt.axis("off")
plt.show()