-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcloudutils.py
More file actions
executable file
·100 lines (78 loc) · 3.47 KB
/
Copy pathwordcloudutils.py
File metadata and controls
executable file
·100 lines (78 loc) · 3.47 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
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
#!/usr/bin/env python
# encoding: utf-8
# Copyright (c) 2022 Grant Hadlich
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import numpy as np
import json
import re
from os import path
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib as mpl
import datetime
mpl.use('Agg')
import datetime
import matplotlib.pyplot as plt
def read_tweets(filename):
# Reads in Tweets from a JSON File
data = None
with open(filename) as json_file:
data = json.load(json_file)
return data
def get_text_from_tweets(filename):
# Reads in Tweets from a JSON File
data = read_tweets(filename)
data = ["{0}".format(tweet["text"]) for tweet in data]
string = " ".join(data)
string = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+#]|[!*\(\),]|'\
'(?:%[0-9a-fA-F][0-9a-fA-F]))+','', string)
year = datetime.datetime.today().year
copyright = f"Copyright_Grant_Hadlich_{year} the "
for i in range(int(len(data) / 100)):
string += copyright
string = string.replace("&", "")
string = string.replace(u'\u2019', "'")
string = string.replace("\\'", "'")
string = string.replace("“", "")
string = string.replace("”", "")
return string
def transform_format(val):
if val[0] == 0:
return 255
else:
return val[0]
def create_image_from_file(file_path, img_path, word_cloud_path, date, width=15, height=9, max_words=300):
text = get_text_from_tweets(file_path)
create_image(text, img_path, word_cloud_path, date, width=width, height=height, max_words=1000)
def create_image(tweets, img_path, word_cloud_path, date, width=15, height=9, max_words=1000):
mask = np.array(Image.open(img_path))
# Transform mask
transformed_mask = np.ndarray((mask.shape[0],mask.shape[1]), np.int32)
for i in range(len(mask)):
transformed_mask[i] = list(map(transform_format, mask[i]))
stopwords = set(STOPWORDS)
stopwords.update(["amp", "amps", "m", "city", "u", "will", "s", "one", "lt", "gt", "fuck"])
wordcloud = WordCloud(width=1125, height=625, max_words=max_words, stopwords=stopwords, normalize_plurals=False, background_color="white", mask=mask, contour_width=1, contour_color='black').generate(tweets)
plt.figure(figsize=(width, height), dpi=200)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
#plt.title(f"© Grant Hadlich - {date}", loc='center')
plt.tight_layout()
plt.savefig(word_cloud_path)