Introduction. Throughout history, industrious men…
By: Micheal Jooste
Tags: Jupyter Notebooks, Wordcloud, Python, Wikipedia

Throughout history, industrious men have invented things without stopping to think if they should, looking at you Mary Shelley! With these high aspirations in mind I have leveraged the Wikipedia library in Python to create custom WordClouds from any specified Wikipedia Article.

Required libraries

Make sure to install the wikipedia library if you haven’t already. Jupyter notebooks doesn’t seem to come with these preinstalled. “pip/conda install wikipedia” from the CLI “pip/conda install wordcloud” from the CLI

import pandas as pd

import os

import numpy as np

import wikipedia

from PIL import Image

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

import matplotlib.pyplot as plt

%matplotlib inline

import warnings

warnings.filterwarnings("ignore")

Import Data

The library is incredibly easy to use. Simply call the wikipedia library and function, pass a parameter which would be the keyword/title of the page you’re looking for and you get a summary of the wikipeadia page in the form of a string. Appreciate that chessboxing is a real sport for lovers of chess and boxing everywhere.

print(type(wikipedia.WikipediaPage(title = 'Chess Boxing').summary))

data_science= wikipedia.WikipediaPage(title = 'Chess Boxing').summary

data_science = data_science.replace('

','').replace("\'","")

print(type(data_science))

print(data_science) <class 'str'>

<class 'str'>

Chessboxing, is a hybrid that combines two traditional pastimes: chess and boxing. The competitors fight in alternating rounds of chess and boxing. Chessboxing was invented by French comic book artist Enki Bilal and adapted by Dutch performance artist Iepe Rubingh as an art performance and has subsequently grown into a competitive sport. Chessboxing is particularly popular in Germany, the United Kingdom, India, and Russia.

Data Wrangling

Some extremely minor data wrangling is required to assign all the text to position [0:0] of a data frame.

#Now I convert the string I generated from wikipedia and convert it to an object

df=pd.Series(data_science)

#then I pass the object into a dataframe at position [0:0]

df1=pd.DataFrame(df)

#then I rename the column Wiki_Text

df1.columns=['Wiki_Text']

df1.head()

Create a WordCloud

Now just pass the dataframe to the WordCloud function and you are good to go!

# Now we start creating a wordcloud

text = df1.Wiki_Text[0]



# Create and generate a word cloud image:

wordcloud = WordCloud(max_words=100, background_color="black").generate(text)

plt.figure()

plt.imshow(wordcloud, interpolation="bilinear")

plt.axis("off")

plt.show()

Save it to a file and finally impress your colleagues

Now if you want to save it to a file use the wordcloud file writer.

wordcloud.to_file("C:\\Users\\mike\\documents\\Python_Visualisations\\Data_Science.png") <wordcloud.wordcloud.WordCloud at 0x25f51d38508>

Its alive! Now what?

This was maybe not the best use of my time or yours and for that I apologise. But it was a fun way to spend an evening. Features I will be adding in the future will include, containerising this with Docker and also publishing this with the excellent Datapane https://datapane.com.