Last active
June 22, 2020 13:19
-
-
Save amitness/a8136e5d57f28d81c20c635fd5f78ad3 to your computer and use it in GitHub Desktop.
Generating EmojiCloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
from wordcloud import WordCloud | |
from collections import Counter | |
import matplotlib.pyplot as plt | |
class EmojiCloud: | |
def __init__(self, | |
font_path='Symbola.ttf', | |
color='yellow'): | |
self.font_path = font_path | |
self.color = color | |
self.word_cloud = self.initialize_wordcloud() | |
self.emoji_probability = None | |
def initialize_wordcloud(self): | |
word_cloud = WordCloud(font_path=self.font_path, | |
width=2000, | |
height=1000, | |
background_color='white', | |
random_state=42, | |
collocations=False) | |
return word_cloud | |
def color_func(self, word, font_size, position, orientation, random_state=None, | |
**kwargs): | |
hue_saturation = { | |
'yellow': '42, 88%', | |
'blue': '194, 49%', | |
'green': '159, 42%', | |
'grey': '45, 2%' | |
}.get(self.color) | |
current_emoji_probability = self.emoji_probability[word] | |
if current_emoji_probability >= 0.20: | |
opacity = 50 | |
else: | |
opacity = 75 - current_emoji_probability/0.2 * 5 | |
return f"hsl({hue_saturation},{opacity}%)" | |
def generate(self, emojis): | |
emoji_frequencies = Counter(emojis) | |
total_count = len(emojis) | |
self.emoji_probability = {emoji: count/total_count for emoji, count in emoji_frequencies.items()} | |
wc = self.word_cloud.generate_from_frequencies(emoji_frequencies) | |
plt.imshow(wc.recolor(color_func=self.color_func, random_state=42), | |
interpolation="bilinear") | |
plt.axis("off") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment