Skip to content

Commit

Permalink
Merge pull request #234 from rmelick/emoji-support
Browse files Browse the repository at this point in the history
Add support for emoji.
  • Loading branch information
amueller committed Apr 6, 2017
2 parents b319cf4 + 6a3a6de commit 6a36626
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -5,3 +5,5 @@ wordcloud/query_integral_image.so
doc/_build
doc/auto_examples
doc/generated
.venv/
.idea/
47 changes: 47 additions & 0 deletions examples/emoji.py
@@ -0,0 +1,47 @@
#!/usr/bin/env python
"""
Emoji Example
===============
A simple example that shows how to include emoji. Note that this example does not seem to work on OS X, but does
work correctly in Ubuntu.
There are 3 important steps to follow to include emoji:
1) Read the text input with io.open instead of the built in open. This ensures that it is loaded as UTF-8
2) Override the regular expression used by word cloud to parse the text into words. The default expression
will only match ascii words
3) Override the default font to something that supports emoji. The included Symbola font includes black and
white outlines for most emoji. There are currently issues with the PIL/Pillow library that seem to prevent
it from functioning correctly on OS X (https://github.com/python-pillow/Pillow/issues/1774), so try this
on ubuntu if you are having problems.
"""
import io
import string
from os import path
from wordcloud import WordCloud

d = path.dirname(__file__)

# It is important to use io.open to correctly load the file as UTF-8
text = io.open(path.join(d, 'happy-emoji.txt')).read()

# the regex used to detect words is a combination of normal words, ascii art, and emojis
# 2+ consecutive letters (also include apostrophes), e.x It's
normal_word = r"(?:\w[\w']+)"
# 2+ consecutive punctuations, e.x. :)
ascii_art = r"(?:[{punctuation}][{punctuation}]+)".format(punctuation=string.punctuation)
# a single character that is not alpha_numeric or other ascii printable
emoji = r"(?:[^\s])(?<![\w{ascii_printable}])".format(ascii_printable=string.printable)
regexp = r"{normal_word}|{ascii_art}|{emoji}".format(normal_word=normal_word, ascii_art=ascii_art,
emoji=emoji)

# Generate a word cloud image
# The Symbola font includes most emoji
font_path = path.join(d, 'fonts', 'Symbola', 'Symbola.ttf')
wordcloud = WordCloud(font_path=font_path, regexp=regexp).generate(text)

# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
4 changes: 4 additions & 0 deletions examples/fonts/Symbola/README.md
@@ -0,0 +1,4 @@
The symbola font was downloaded from George Douros at http://users.teilar.gr/~g1951d/

On that site (and inside the pdf), the license is described as follows:
In lieu of a licence; fonts and documents in this site are free for any use; George Douros
Binary file added examples/fonts/Symbola/Symbola.pdf
Binary file not shown.
Binary file added examples/fonts/Symbola/Symbola.ttf
Binary file not shown.
42 changes: 42 additions & 0 deletions examples/happy-emoji.txt
@@ -0,0 +1,42 @@
happy happy happy happy happy happy happy happy happy happy
smiley smiley smiley smiley smiley smiley smiley smiley smiley smiley
happy happy happy happy happy happy happy happy happy happy
smiley smiley smiley smiley smiley smiley smiley smiley smiley smiley
happy happy happy happy happy happy happy happy happy happy
smiley smiley smiley smiley smiley smiley smiley smiley smiley smiley
πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€
:) :) :) :) :) :) :) :) :) :)
πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€
:) :) :) :) :) :) :) :) :) :)
πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€
:) :) :) :) :) :) :) :) :) :)
πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€
:) :) :) :) :) :) :) :) :) :)
πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€
:) :) :) :) :) :) :) :) :) :)
smile smile smile smile smile smile smile smile smile smile
face face face face face face face face face face face face
contented, content, cheerful, cheery, merry, joyful, jovial, jolly, joking, jocular, gleeful, carefree, untroubled, delighted, smiling, beaming, grinning, glowing, satisfied, gratified, buoyant, radiant, sunny, blithe, joyous, beatific, blessed
😊

πŸ˜ƒ Smileys & People

πŸ˜€ Grinning Face
😁 Grinning Face With Smiling Eyes
πŸ˜‚ Face With Tears of Joy
🀣 Rolling on the Floor Laughing
πŸ˜ƒ Smiling Face With Open Mouth
πŸ˜„ Smiling Face With Open Mouth & Smiling Eyes
πŸ˜… Smiling Face With Open Mouth & Cold Sweat
πŸ˜† Smiling Face With Open Mouth & Closed Eyes
πŸ˜‰ Winking Face
😊 Smiling Face With Smiling Eyes
πŸ˜‹ Face Savouring Delicious Food
😎 Smiling Face With Sunglasses
😍 Smiling Face With Heart-eyes
😘 Face Blowing a Kiss
πŸ˜— Kissing Face
πŸ˜™ Kissing Face With Smiling Eyes
😚 Kissing Face With Closed Eyes
☺️ Smiling Face
πŸ™‚ Slightly Smiling Face

0 comments on commit 6a36626

Please sign in to comment.