{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Emoji Example\nA simple example that shows how to include emoji.  Note that this example does not seem to work on OS X, but does\nwork correctly in Ubuntu.\n\nThere are 3 important steps to follow to include emoji:\n1) Read the text input with io.open instead of the built in open.  This ensures that it is loaded as UTF-8\n2) Override the regular expression used by word cloud to parse the text into words.  The default expression\nwill only match ascii words\n3) Override the default font to something that supports emoji.  The included Symbola font includes black and\nwhite outlines for most emoji.  There are currently issues with the PIL/Pillow library that seem to prevent\nit from functioning correctly on OS X (https://github.com/python-pillow/Pillow/issues/1774), so try this\non ubuntu if you are having problems.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import io\nimport os\nimport string\nfrom os import path\nfrom wordcloud import WordCloud\n\n# get data directory (using getcwd() is needed to support running example in generated IPython notebook)\nd = path.dirname(__file__) if \"__file__\" in locals() else os.getcwd()\n\n# It is important to use io.open to correctly load the file as UTF-8\ntext = io.open(path.join(d, 'happy-emoji.txt')).read()\n\n# the regex used to detect words is a combination of normal words, ascii art, and emojis\n# 2+ consecutive letters (also include apostrophes), e.x It's\nnormal_word = r\"(?:\\w[\\w']+)\"\n# 2+ consecutive punctuations, e.x. :)\nascii_art = r\"(?:[{punctuation}][{punctuation}]+)\".format(punctuation=string.punctuation)\n# a single character that is not alpha_numeric or other ascii printable\nemoji = r\"(?:[^\\s])(?<![\\w{ascii_printable}])\".format(ascii_printable=string.printable)\nregexp = r\"{normal_word}|{ascii_art}|{emoji}\".format(normal_word=normal_word, ascii_art=ascii_art,\n                                                     emoji=emoji)\n\n# Generate a word cloud image\n# The Symbola font includes most emoji\nfont_path = path.join(d, 'fonts', 'Symbola', 'Symbola.ttf')\nwc = WordCloud(font_path=font_path, regexp=regexp).generate(text)\n\n# Display the generated image:\n# the matplotlib way:\nimport matplotlib.pyplot as plt\nplt.imshow(wc)\nplt.axis(\"off\")\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.13.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}