Skip to content

Instantly share code, notes, and snippets.

@amitness
Last active June 24, 2020 12:00
Show Gist options
  • Save amitness/6bceaac0934a1240453a01047ebdc1d6 to your computer and use it in GitHub Desktop.
Save amitness/6bceaac0934a1240453a01047ebdc1d6 to your computer and use it in GitHub Desktop.
Class for Emoji Cloud
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]
# Use 50% opacity for emojis with 20% or more coverage
if current_emoji_probability >= 0.20:
opacity = 50
else:
# Use an opacity between 70 to 75 for other emojis
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)
# Calculate probabilities of emojis from frequency counts
self.emoji_probability = {emoji: count/total_count for emoji, count in emoji_frequencies.items()}
# Use wordcloud library to generate a wordcloud of emojis
wc = self.word_cloud.generate_from_frequencies(emoji_frequencies)
# Recolor emojis according to their frequency
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