1#!/usr/local/bin/python3.8 2""" 3Emoji Example 4=============== 5A simple example that shows how to include emoji. Note that this example does not seem to work on OS X, but does 6work correctly in Ubuntu. 7 8There are 3 important steps to follow to include emoji: 91) Read the text input with io.open instead of the built in open. This ensures that it is loaded as UTF-8 102) Override the regular expression used by word cloud to parse the text into words. The default expression 11will only match ascii words 123) Override the default font to something that supports emoji. The included Symbola font includes black and 13white outlines for most emoji. There are currently issues with the PIL/Pillow library that seem to prevent 14it from functioning correctly on OS X (https://github.com/python-pillow/Pillow/issues/1774), so try this 15on ubuntu if you are having problems. 16""" 17import io 18import os 19import string 20from os import path 21from wordcloud import WordCloud 22 23# get data directory (using getcwd() is needed to support running example in generated IPython notebook) 24d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() 25 26# It is important to use io.open to correctly load the file as UTF-8 27text = io.open(path.join(d, 'happy-emoji.txt')).read() 28 29# the regex used to detect words is a combination of normal words, ascii art, and emojis 30# 2+ consecutive letters (also include apostrophes), e.x It's 31normal_word = r"(?:\w[\w']+)" 32# 2+ consecutive punctuations, e.x. :) 33ascii_art = r"(?:[{punctuation}][{punctuation}]+)".format(punctuation=string.punctuation) 34# a single character that is not alpha_numeric or other ascii printable 35emoji = r"(?:[^\s])(?<![\w{ascii_printable}])".format(ascii_printable=string.printable) 36regexp = r"{normal_word}|{ascii_art}|{emoji}".format(normal_word=normal_word, ascii_art=ascii_art, 37 emoji=emoji) 38 39# Generate a word cloud image 40# The Symbola font includes most emoji 41font_path = path.join(d, 'fonts', 'Symbola', 'Symbola.ttf') 42wordcloud = WordCloud(font_path=font_path, regexp=regexp).generate(text) 43 44# Display the generated image: 45# the matplotlib way: 46import matplotlib.pyplot as plt 47plt.imshow(wordcloud) 48plt.axis("off") 49plt.show() 50