After I saw a wordcloud on the blog of my father, I decided to create one for my blog too. After a call with him I found out, that he created it once and never regenerated it, because of performance reasons due of his blog software. I want it to be dynamic created by a self written pelican plugin. Unfortunately I am not finished installing all the required modules on the webserver, so I don not have a wordcloud with all my tags yet.
My python script
I found a python script on the blog of my father and modified it for general use.
#!/usr/bin/env python3
from os import path
from wordcloud import WordCloud, STOPWORDS
#function to map color to my blog scheme
def color_func(word, font_size, position, orientation, random_state=None, **kwargs):
x = position[1] / 400.0
g = x * 153 + (1-x) * 87
b = x * 87 + (1-x) * 153
return (21, int(g), int(b))
#Main function
def create(in_f, out_f):
d = path.dirname(__file__)
stopwords = STOPWORDS.copy()
text = open(path.join(d, in_f)).read()
wordcloud = WordCloud(background_color="white", mac_words=25, stopwords=stopwords).generate(text)
wordcloud.recolor(color_func=color_func, random_state=3)
wordcloud.to_file(path.join(d, out_f))
#To be usable as shell program
if __name__ == "__main__":
import sys
in_f = sys.argv[0]
out_f = sys.argv[1]
create(in_f, out_f)
Usage
Now I have two methods to use it
As a cli with
wordcloudcli.py text.txt out.png
As a python module
import wordcloudcli
wordcloudcli.create("text.txt", "out.png")