-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrdcld.py
103 lines (88 loc) · 3.12 KB
/
wrdcld.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from os import path
from wordcloud import WordCloud, STOPWORDS
import pandas as pd
# Read the whole text.
print("*****************Reading train_set.csv file.*****************")
df = pd.read_csv('train_set.csv',sep='\t')
politics_text = ""
film_text = ""
football_text = ""
business_text = ""
technology_text = ""
for i in range(0, len(df["Content"])):
if df["Category"][i] == "Politics":
politics_text += df["Content"][i] + " "
elif df["Category"][i] == "Film":
film_text += df["Content"][i] + " "
elif df["Category"][i] == "Football":
football_text += df["Content"][i] + " "
elif df["Category"][i] == "Business":
business_text += df["Content"][i] + " "
elif df["Category"][i] == "Technology":
technology_text += df["Content"][i] + " "
print("*****************Reading train_set.csv file done.*****************")
stopwords = STOPWORDS.copy()
stopwords.add("said")
stopwords.add("also")
stopwords.add("told")
stopwords.add("one")
stopwords.add("last")
stopwords.add("new")
stopwords.add("say")
stopwords.add("year")
stopwords.add("will")
stopwords.add("yes")
stopwords.add("no")
stopwords.add("although")
stopwords.add("first")
stopwords.add("day")
# Generate a word cloud image
wordcloud1 = WordCloud(stopwords=stopwords).generate(politics_text)
wordcloud2 = WordCloud(stopwords=stopwords).generate(film_text)
wordcloud3 = WordCloud(stopwords=stopwords).generate(football_text)
wordcloud4 = WordCloud(stopwords=stopwords).generate(business_text)
wordcloud5 = WordCloud(stopwords=stopwords).generate(technology_text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud1, interpolation='bilinear')
plt.axis("off")
plt.imshow(wordcloud2, interpolation='bilinear')
plt.axis("off")
plt.imshow(wordcloud3, interpolation='bilinear')
plt.axis("off")
plt.imshow(wordcloud4, interpolation='bilinear')
plt.axis("off")
plt.imshow(wordcloud5, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
print("*****************Generating wordlcloud for politics.*****************")
wordcloud1 = WordCloud(max_font_size=40).generate(politics_text)
plt.figure()
plt.imshow(wordcloud1, interpolation="bilinear")
plt.axis("off")
plt.show()
print("*****************Generating wordlcloud for films.*****************")
wordcloud2 = WordCloud(max_font_size=40).generate(film_text)
plt.figure()
plt.imshow(wordcloud2, interpolation="bilinear")
plt.axis("off")
plt.show()
print("*****************Generating wordlcloud for football.*****************")
wordcloud3 = WordCloud(max_font_size=40).generate(football_text)
plt.figure()
plt.imshow(wordcloud3, interpolation="bilinear")
plt.axis("off")
plt.show()
print("*****************Generating wordlcloud for business.*****************")
wordcloud4 = WordCloud(max_font_size=40).generate(business_text)
plt.figure()
plt.imshow(wordcloud4, interpolation="bilinear")
plt.axis("off")
plt.show()
print("*****************Generating wordlcloud for technology.*****************")
wordcloud5 = WordCloud(max_font_size=40).generate(technology_text)
plt.figure()
plt.imshow(wordcloud5, interpolation="bilinear")
plt.axis("off")
plt.show()