forked from LSmyrnaios/Articles-Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyWordcloud.py
More file actions
57 lines (45 loc) · 1.69 KB
/
myWordcloud.py
File metadata and controls
57 lines (45 loc) · 1.69 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
# coding=utf-8
from wordcloud import WordCloud
from supportFuncs import stopWords
import csv
def show_wordcloud(stop_words, data, title=None):
print("Creating wordcloud \"" + title + '\" img...')
WordCloud(
background_color='black',
stopwords=stop_words,
max_words=200,
max_font_size=40,
scale=5,
random_state=1
).generate(str(data)).to_file("Resources/img/" + title + ".png")
def my_wordcloud(stop_words):
print('Running myWordcloud...\n')
# print 'StopWords ', stop_words
businessStr = ''
politicsStr = ''
footballStr = ''
filmStr = ''
technologyStr = ''
with open('Resources/csv/train_set.csv', mode='r', encoding="utf8") as csvfile:
csvReader = csv.DictReader(csvfile, delimiter='\t', quotechar='|')
for row in csvReader:
category = row["Category"]
if category == 'Business':
businessStr += row["Content"]
elif category == 'Politics':
politicsStr += row["Content"]
elif category == 'Football':
footballStr += row["Content"]
elif category == 'Film':
filmStr += row["Content"]
elif category == 'Technology':
technologyStr += row["Content"]
show_wordcloud(stop_words, businessStr, 'Business')
show_wordcloud(stop_words, politicsStr, 'Politics')
show_wordcloud(stop_words, footballStr, 'Football')
show_wordcloud(stop_words, filmStr, 'Film')
show_wordcloud(stop_words, technologyStr, 'Technology')
print('myWordcloud finished!\n')
# Run myWordcloud directly:
if __name__ == '__main__':
my_wordcloud(stopWords.get_stop_words())