Skip to content

Instantly share code, notes, and snippets.

@x0x8x
Created July 8, 2020 18:14
Show Gist options
  • Save x0x8x/45653a0cc611ef2875536d2a1ccaba3a to your computer and use it in GitHub Desktop.
Save x0x8x/45653a0cc611ef2875536d2a1ccaba3a to your computer and use it in GitHub Desktop.
# by @arthurwayne, I guess...
from datetime import datetime, date
import time
import os
import nltk
from nltk.corpus import stopwords
nltk.download("stopwords")
from PIL import Image
import numpy as np
from scipy.ndimage import gaussian_gradient_magnitude
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
d = "."
f = "."
channel = "pyrogramlounge"
today_date = datetime.now().date()
today = datetime.now().strftime("%H_%M_%S-%Y-%m-%d")
# today_unixtime = int(time.mktime(today_date.timetuple()))
messages = await client.get_history(
channel, limit=100, reverse=False
) # offset_date=int(today_unixtime)
msges = ""
for message in messages:
if message.text:
msg = str(message.text) + "\n"
msges = msges + msg
with open("messages-today-pyrogramlounge.text", "w") as file:
file.write(msges)
text = open(
os.path.join(d, "messages-today-pyrogramlounge.text"), encoding="UTF-8"
).read()
stop_words = stopwords.words("english")
new_stopwords = ["'"]
stop_words.extend(new_stopwords)
stop_words = set(stop_words)
clean_text = [word for word in text.split() if word not in stop_words]
cloud_image = np.array(Image.open(os.path.join(d, "/tmp/tmp_cloud.png")))
cloud_image = cloud_image[::3, ::3]
cloud_mask = cloud_image.copy()
cloud_mask[cloud_mask.sum(axis=2) == 0] = 255
image_edges = np.mean(
[gaussian_gradient_magnitude(cloud_image[:, :, i] / 255.0, 2) for i in range(3)],
axis=0,
)
cloud_mask[image_edges > 0.08] = 255
wc = WordCloud(
max_words=2000,
scale=3,
mask=cloud_mask,
relative_scaling=1,
repeat=False,
colormap="Pastel1",
stopwords=str(clean_text),
)
wc.generate(str(clean_text))
wc.to_file("blue_cloud_pastel-pyrogramlounge.png")
image_colors = ImageColorGenerator(cloud_image)
wc.recolor(color_func=image_colors)
wc.to_file("cloud.png")
await client.send_photo(message.chat.id, "cloud.png")
await client.send_photo(message.chat.id, "blue_cloud_pastel-pyrogramlounge.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment