Commit 9121a31d authored by Andreas Mueller's avatar Andreas Mueller

update website

parent ea6a122b
{
"nbformat_minor": 0,
"cells": [
{
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
],
"outputs": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"\nUsing custom colors\n===================\n\nUsing the recolor method and custom coloring functions.\n\n"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"import numpy as np\nfrom PIL import Image\nfrom os import path\nimport matplotlib.pyplot as plt\nimport random\n\nfrom wordcloud import WordCloud, STOPWORDS\n\n\ndef grey_color_func(word, font_size, position, orientation, random_state=None,\n **kwargs):\n return \"hsl(0, 0%%, %d%%)\" % random.randint(60, 100)\n\nd = path.dirname(__file__)\n\n# read the mask image\n# taken from\n# http://www.stencilry.org/stencils/movies/star%20wars/storm-trooper.gif\nmask = np.array(Image.open(path.join(d, \"stormtrooper_mask.png\")))\n\n# movie script of \"a new hope\"\n# http://www.imsdb.com/scripts/Star-Wars-A-New-Hope.html\n# May the lawyers deem this fair use.\ntext = open(\"a_new_hope.txt\").read()\n\n# preprocessing the text a little bit\ntext = text.replace(\"HAN\", \"Han\")\ntext = text.replace(\"LUKE'S\", \"Luke\")\n\n# adding movie script specific stopwords\nstopwords = set(STOPWORDS)\nstopwords.add(\"int\")\nstopwords.add(\"ext\")\n\nwc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,\n random_state=1).generate(text)\n# store default colored image\ndefault_colors = wc.to_array()\nplt.title(\"Custom colors\")\nplt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),\n interpolation=\"bilinear\")\nwc.to_file(\"a_new_hope.png\")\nplt.axis(\"off\")\nplt.figure()\nplt.title(\"Default colors\")\nplt.imshow(default_colors, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.show()"
],
"outputs": [],
"metadata": {
"collapsed": false
}
}
],
"nbformat": 4,
"metadata": {
"kernelspec": {
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"mimetype": "text/x-python",
"file_extension": ".py"
}
}
}
\ No newline at end of file
#!/usr/bin/env python2
#!/usr/bin/env python
"""
Using custom colors
====================
===================
Using the recolor method and custom coloring functions.
"""
import numpy as np
from PIL import Image
from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
import random
from wordcloud import WordCloud, STOPWORDS
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
def grey_color_func(word, font_size, position, orientation, random_state=None,
**kwargs):
return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)
d = path.dirname(__file__)
......@@ -21,7 +24,7 @@ d = path.dirname(__file__)
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/star%20wars/storm-trooper.gif
mask = imread(path.join(d, "stormtrooper_mask.png"))
mask = np.array(Image.open(path.join(d, "stormtrooper_mask.png")))
# movie script of "a new hope"
# http://www.imsdb.com/scripts/Star-Wars-A-New-Hope.html
......@@ -33,7 +36,7 @@ text = text.replace("HAN", "Han")
text = text.replace("LUKE'S", "Luke")
# adding movie script specific stopwords
stopwords = STOPWORDS.copy()
stopwords = set(STOPWORDS)
stopwords.add("int")
stopwords.add("ext")
......@@ -42,11 +45,12 @@ wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,
# store default colored image
default_colors = wc.to_array()
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3))
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),
interpolation="bilinear")
wc.to_file("a_new_hope.png")
plt.axis("off")
plt.figure()
plt.title("Default colors")
plt.imshow(default_colors)
plt.imshow(default_colors, interpolation="bilinear")
plt.axis("off")
plt.show()
{
"nbformat_minor": 0,
"cells": [
{
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
],
"outputs": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"\nImage-colored wordcloud\n=======================\n\nYou can color a word-cloud by using an image-based coloring strategy\nimplemented in ImageColorGenerator. It uses the average color of the region\noccupied by the word in a source image. You can combine this with masking -\npure-white will be interpreted as 'don't occupy' by the WordCloud object when\npassed as mask.\nIf you want white as a legal color, you can just pass a different image to\n\"mask\", but make sure the image shapes line up.\n\n"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"from os import path\nfrom PIL import Image\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom wordcloud import WordCloud, STOPWORDS, ImageColorGenerator\n\nd = path.dirname(__file__)\n\n# Read the whole text.\ntext = open(path.join(d, 'alice.txt')).read()\n\n# read the mask / color image taken from\n# http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010\nalice_coloring = np.array(Image.open(path.join(d, \"alice_color.png\")))\nstopwords = set(STOPWORDS)\nstopwords.add(\"said\")\n\nwc = WordCloud(background_color=\"white\", max_words=2000, mask=alice_coloring,\n stopwords=stopwords, max_font_size=40, random_state=42)\n# generate word cloud\nwc.generate(text)\n\n# create coloring from image\nimage_colors = ImageColorGenerator(alice_coloring)\n\n# show\nplt.imshow(wc, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.figure()\n# recolor wordcloud and show\n# we could also give color_func=image_colors directly in the constructor\nplt.imshow(wc.recolor(color_func=image_colors), interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.figure()\nplt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.show()"
],
"outputs": [],
"metadata": {
"collapsed": false
}
}
],
"nbformat": 4,
"metadata": {
"kernelspec": {
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"mimetype": "text/x-python",
"file_extension": ".py"
}
}
}
\ No newline at end of file
#!/usr/bin/env python2
#!/usr/bin/env python
"""
Image-colored wordcloud
========================
You can color a word-cloud by using an image-based coloring strategy implemented in
ImageColorGenerator. It uses the average color of the region occupied by the word
in a source image. You can combine this with masking - pure-white will be interpreted
as 'don't occupy' by the WordCloud object when passed as mask.
If you want white as a legal color, you can just pass a different image to "mask",
but make sure the image shapes line up.
=======================
You can color a word-cloud by using an image-based coloring strategy
implemented in ImageColorGenerator. It uses the average color of the region
occupied by the word in a source image. You can combine this with masking -
pure-white will be interpreted as 'don't occupy' by the WordCloud object when
passed as mask.
If you want white as a legal color, you can just pass a different image to
"mask", but make sure the image shapes line up.
"""
from os import path
from scipy.misc import imread
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
......@@ -21,13 +24,14 @@ d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()
# read the mask / color image
# taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
alice_coloring = imread(path.join(d, "alice_color.png"))
# read the mask / color image taken from
# http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
alice_coloring = np.array(Image.open(path.join(d, "alice_color.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
stopwords=STOPWORDS.add("said"),
max_font_size=40, random_state=42)
stopwords=stopwords, max_font_size=40, random_state=42)
# generate word cloud
wc.generate(text)
......@@ -35,14 +39,14 @@ wc.generate(text)
image_colors = ImageColorGenerator(alice_coloring)
# show
plt.imshow(wc)
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.figure()
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
plt.imshow(wc.recolor(color_func=image_colors))
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray)
plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear")
plt.axis("off")
plt.show()
{
"nbformat_minor": 0,
"cells": [
{
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
],
"outputs": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"\nColored by Group Example\n========================\n\nGenerating a word cloud that assigns colors to words based on\na predefined mapping from colors to words\n\n"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"from wordcloud import (WordCloud, get_single_color_func)\nimport matplotlib.pyplot as plt\n\n\nclass SimpleGroupedColorFunc(object):\n \"\"\"Create a color function object which assigns EXACT colors\n to certain words based on the color to words mapping\n\n Parameters\n ----------\n color_to_words : dict(str -> list(str))\n A dictionary that maps a color to the list of words.\n\n default_color : str\n Color that will be assigned to a word that's not a member\n of any value from color_to_words.\n \"\"\"\n\n def __init__(self, color_to_words, default_color):\n self.word_to_color = {word: color\n for (color, words) in color_to_words.items()\n for word in words}\n\n self.default_color = default_color\n\n def __call__(self, word, **kwargs):\n return self.word_to_color.get(word, self.default_color)\n\n\nclass GroupedColorFunc(object):\n \"\"\"Create a color function object which assigns DIFFERENT SHADES of\n specified colors to certain words based on the color to words mapping.\n\n Uses wordcloud.get_single_color_func\n\n Parameters\n ----------\n color_to_words : dict(str -> list(str))\n A dictionary that maps a color to the list of words.\n\n default_color : str\n Color that will be assigned to a word that's not a member\n of any value from color_to_words.\n \"\"\"\n\n def __init__(self, color_to_words, default_color):\n self.color_func_to_words = [\n (get_single_color_func(color), set(words))\n for (color, words) in color_to_words.items()]\n\n self.default_color_func = get_single_color_func(default_color)\n\n def get_color_func(self, word):\n \"\"\"Returns a single_color_func associated with the word\"\"\"\n try:\n color_func = next(\n color_func for (color_func, words) in self.color_func_to_words\n if word in words)\n except StopIteration:\n color_func = self.default_color_func\n\n return color_func\n\n def __call__(self, word, **kwargs):\n return self.get_color_func(word)(word, **kwargs)\n\n\ntext = \"\"\"The Zen of Python, by Tim Peters\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!\"\"\"\n\n# Since the text is small collocations are turned off and text is lower-cased\nwc = WordCloud(collocations=False).generate(text.lower())\n\ncolor_to_words = {\n # words below will be colored with a green single color function\n '#00ff00': ['beautiful', 'explicit', 'simple', 'sparse',\n 'readability', 'rules', 'practicality',\n 'explicitly', 'one', 'now', 'easy', 'obvious', 'better'],\n # will be colored with a red single color function\n 'red': ['ugly', 'implicit', 'complex', 'complicated', 'nested',\n 'dense', 'special', 'errors', 'silently', 'ambiguity',\n 'guess', 'hard']\n}\n\n# Words that are not in any of the color_to_words values\n# will be colored with a grey single color function\ndefault_color = 'grey'\n\n# Create a color function with single tone\n# grouped_color_func = SimpleGroupedColorFunc(color_to_words, default_color)\n\n# Create a color function with multiple tones\ngrouped_color_func = GroupedColorFunc(color_to_words, default_color)\n\n# Apply our color function\nwc.recolor(color_func=grouped_color_func)\n\n# Plot\nplt.figure()\nplt.imshow(wc, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.show()"
],
"outputs": [],
"metadata": {
"collapsed": false
}
}
],
"nbformat": 4,
"metadata": {
"kernelspec": {
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"mimetype": "text/x-python",
"file_extension": ".py"
}
}
}
\ No newline at end of file
#!/usr/bin/env python
"""
Colored by Group Example
========================
Generating a word cloud that assigns colors to words based on
a predefined mapping from colors to words
"""
from wordcloud import (WordCloud, get_single_color_func)
import matplotlib.pyplot as plt
class SimpleGroupedColorFunc(object):
"""Create a color function object which assigns EXACT colors
to certain words based on the color to words mapping
Parameters
----------
color_to_words : dict(str -> list(str))
A dictionary that maps a color to the list of words.
default_color : str
Color that will be assigned to a word that's not a member
of any value from color_to_words.
"""
def __init__(self, color_to_words, default_color):
self.word_to_color = {word: color
for (color, words) in color_to_words.items()
for word in words}
self.default_color = default_color
def __call__(self, word, **kwargs):
return self.word_to_color.get(word, self.default_color)
class GroupedColorFunc(object):
"""Create a color function object which assigns DIFFERENT SHADES of
specified colors to certain words based on the color to words mapping.
Uses wordcloud.get_single_color_func
Parameters
----------
color_to_words : dict(str -> list(str))
A dictionary that maps a color to the list of words.
default_color : str
Color that will be assigned to a word that's not a member
of any value from color_to_words.
"""
def __init__(self, color_to_words, default_color):
self.color_func_to_words = [
(get_single_color_func(color), set(words))
for (color, words) in color_to_words.items()]
self.default_color_func = get_single_color_func(default_color)
def get_color_func(self, word):
"""Returns a single_color_func associated with the word"""
try:
color_func = next(
color_func for (color_func, words) in self.color_func_to_words
if word in words)
except StopIteration:
color_func = self.default_color_func
return color_func
def __call__(self, word, **kwargs):
return self.get_color_func(word)(word, **kwargs)
text = """The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
# Since the text is small collocations are turned off and text is lower-cased
wc = WordCloud(collocations=False).generate(text.lower())
color_to_words = {
# words below will be colored with a green single color function
'#00ff00': ['beautiful', 'explicit', 'simple', 'sparse',
'readability', 'rules', 'practicality',
'explicitly', 'one', 'now', 'easy', 'obvious', 'better'],
# will be colored with a red single color function
'red': ['ugly', 'implicit', 'complex', 'complicated', 'nested',
'dense', 'special', 'errors', 'silently', 'ambiguity',
'guess', 'hard']
}
# Words that are not in any of the color_to_words values
# will be colored with a grey single color function
default_color = 'grey'
# Create a color function with single tone
# grouped_color_func = SimpleGroupedColorFunc(color_to_words, default_color)
# Create a color function with multiple tones
grouped_color_func = GroupedColorFunc(color_to_words, default_color)
# Apply our color function
wc.recolor(color_func=grouped_color_func)
# Plot
plt.figure()
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
{
"nbformat_minor": 0,
"cells": [
{
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
],
"outputs": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"\nMasked wordcloud\n================\n\nUsing a mask you can generate wordclouds in arbitrary shapes.\n\n"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"from os import path\nfrom PIL import Image\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom wordcloud import WordCloud, STOPWORDS\n\nd = path.dirname(__file__)\n\n# Read the whole text.\ntext = open(path.join(d, 'alice.txt')).read()\n\n# read the mask image\n# taken from\n# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg\nalice_mask = np.array(Image.open(path.join(d, \"alice_mask.png\")))\n\nstopwords = set(STOPWORDS)\nstopwords.add(\"said\")\n\nwc = WordCloud(background_color=\"white\", max_words=2000, mask=alice_mask,\n stopwords=stopwords)\n# generate word cloud\nwc.generate(text)\n\n# store to file\nwc.to_file(path.join(d, \"alice.png\"))\n\n# show\nplt.imshow(wc, interpolation='bilinear')\nplt.axis(\"off\")\nplt.figure()\nplt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')\nplt.axis(\"off\")\nplt.show()"
],
"outputs": [],
"metadata": {
"collapsed": false
}
}
],
"nbformat": 4,
"metadata": {
"kernelspec": {
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"mimetype": "text/x-python",
"file_extension": ".py"
}
}
}
\ No newline at end of file
#!/usr/bin/env python2
#!/usr/bin/env python
"""
Masked wordcloud
================
Using a mask you can generate wordclouds in arbitrary shapes.
"""
from os import path
from scipy.misc import imread
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
......@@ -19,10 +21,13 @@ text = open(path.join(d, 'alice.txt')).read()
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
alice_mask = imread(path.join(d, "alice_mask.png"))
alice_mask = np.array(Image.open(path.join(d, "alice_mask.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
stopwords=STOPWORDS.add("said"))
stopwords=stopwords)
# generate word cloud
wc.generate(text)
......@@ -30,9 +35,9 @@ wc.generate(text)
wc.to_file(path.join(d, "alice.png"))
# show
plt.imshow(wc)
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray)
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()
{
"nbformat_minor": 0,
"cells": [
{
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
],
"outputs": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"\nMinimal Example\n===============\n\nGenerating a square wordcloud from the US constitution using default arguments.\n\n"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"from os import path\nfrom wordcloud import WordCloud\n\nd = path.dirname(__file__)\n\n# Read the whole text.\ntext = open(path.join(d, 'constitution.txt')).read()\n\n# Generate a word cloud image\nwordcloud = WordCloud().generate(text)\n\n# Display the generated image:\n# the matplotlib way:\nimport matplotlib.pyplot as plt\nplt.imshow(wordcloud, interpolation='bilinear')\nplt.axis(\"off\")\n\n# lower max_font_size\nwordcloud = WordCloud(max_font_size=40).generate(text)\nplt.figure()\nplt.imshow(wordcloud, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.show()\n\n# The pil way (if you don't have matplotlib)\n# image = wordcloud.to_image()\n# image.show()"
],
"outputs": [],
"metadata": {
"collapsed": false
}
}
],
"nbformat": 4,
"metadata": {
"kernelspec": {
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"mimetype": "text/x-python",
"file_extension": ".py"
}
}
}
\ No newline at end of file
#!/usr/bin/env python2
#!/usr/bin/env python
"""
Minimal Example
===============
Generating a square wordcloud from the US constitution using default arguments.
"""
from os import path
import matplotlib.pyplot as plt
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
# Open a plot of the generated image.
plt.imshow(wordcloud)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()