How to make a word cloud with Python [Beginner Friendly]

Learn to build a word cloud from Wikipedia using Python and Google Colab. Install wikipedia and wordcloud libraries, fetch text from any Wikipedia page, clean it with regex, configure the WordCloud with stop-words and figure size, then plot and save the result as a PNG.
Chapters
Hello there, welcome to my channel. Today we'll be covering how to create a word cloud with Python or using Python. On my screen you can see an example of one I created just with a couple lines of Python. It's pretty easy and straightforward. It's one I created for my friend Paul Chalimo. I pulled his data from Wikipedia and created a word cloud out of the content that are under his name. I'll walk you through the code real fast and get you going building yours or whatever topic you would like that's already on Wikipedia. So before we start, I would like to mention that I'm using Google Colab. You can use any other IDE that you would like to use. Just Google Google Colab and you can log in using your Gmail account and you can start playing around writing Python code. We'll start by installing dependencies. So first one we want to get is Wikipedia, but since it's not a standard module that comes already built in, we have to install it. So we have to do pip install wikipedia and just click that, and it's going to start installing the package. But since I had already installed it, it might say here, you know, installed, so requirement already satisfied. So that's good. Once that's successful, close that, and I'll create another tab and import Wikipedia. All right, so we have finished importing all our dependencies. The next step is to scrape the data from Wikipedia and generate a word cloud and also save the PNG. All right, now that we have our dependencies we walk through how to get data from Wikipedia. I'll start off by mentioning that Wikipedia supports a lot of languages. So we can just do check supported languages, and this will show us all the languages that are supported by Wikipedia. And we'll go ahead and set the language that we want to use for scraping Wikipedia languages. And you can see from this output, we have so many languages. There's so many here with abbreviations. So you can see the abbreviation of the language and the full name of the language. The language that we need to use into English. But you can just put any of this, like for example French. So specify title or text to scrape from Wikipedia. First, or how I usually like to do it, you can write the code and just put the word in it, but I like asking input from the user. That way it makes it a little more user-friendly. So enter text, and from there what I'll do is print what you searched. And for example, let's just run this and see what it gives us. Nairobi, for example. Let's go ahead and get the data from Wikipedia. So how we do this is we use Wikipedia module that we've already downloaded. So let's call it wiki, right? So let's do wikipedia.page. So the page we want to get, and then we pass in the word. So we pass in the input. So if I search Nairobi, is Nairobi is going to be inputted here so that Wikipedia goes to that page and searches that page on Wikipedia and tries to grab all the information from there, all the content. And then let's figure out if it did actually grab the page. So let's extract plain text from content of the page, cast this to text, and then let's just call it wiki_content. We just need to get the content of wiki. So let's just print that out and see what it gives. Run that. Let's just give it Nairobi. And boom, see, gave us all this information. But you can see it's a lot of information. So it basically went to Wikipedia and scraped everything about Nairobi. So if we come here and Google Wikipedia, and from there let's go to Wikipedia and search here Nairobi. Grabbed all this information about Nairobi, but without the pictures of course. This is all the information that is scraped from that specific site, and this is what we wanted. From this we'll make a word cloud. The next step would be to clean up this text. So we can create a word cloud out of it. Let's just go ahead and open another cell. And in this cell, what we need to do is clean the data. Down here there's footnotes from the page, and we will also need to take out this ends. This means there's a new line after this text. So text, which is what we've already scrubbed, and re is basically regex. So we'll use regex to, to edit or, you know, parse our data the way we want it to. So If you don't know much about regex, I won't cover it in this topic, but feel free to go ahead and look it up. So if you come here, you see that it's cleaned out most of the stuff that we wanted. This is much cleaner than the first one. The next step is to generate and plot our word cloud. We'll go ahead and create a function to do that. We'll give it the parameter of word cloud. Let's set the size of the figure to generate. Figure size. So this will be the size of the canvas. So we'll use matplotlib. So this is basically the size of the figure. Let's give it an arbitrary number. This is from me testing it out previously, and this is the size I think it works best to print out the word cloud. Then the next thing we want to do is display the image, of course. We're basically done with the function there. So the next thing we want to do is generate the word cloud itself. And random state here is just an arbitrary number that you can tweak around to randomize the words already in your text. If you want to understand more about these parameters, just hover over them and it will give you more information. The next thing would be to plot the cloud itself. Here you go. So it's already produced Nairobi, and these are the words it aggregated and gave us. Words randomly from the text above and just displayed here. Stop words like the have been taken out. You can't see the in here because We use the stopwatch to take those words out. And you can also replace specific stop words, like for example, the, you can replace it with a different word that you want. If you want to create one of this, this is how you do it. And you can save it as a background or create a presentation out of it. You can do so many things with it. So how do we save this first of all? Take the word that you entered plus PNG. So for example, the one I created up here, I'm going to add the word Nairobi plus PNG. So it's going to be Nairobi PNG. Come down to the files and you will see the file right here. If you want to download to your system, you click download and you can just save it downloads and boom, there you go. And you can set it as your background lock screen or whatever you want to do with it. You can put it in a presentation, in your next presentation, you can do so many stuff.
More from Tony

Summarize Videos and Research Companies with CrewAI + Gemini Models [Quickstart Guide]

How to Make Your AI Agents More Reliable with CrewAI Task Guardrails (Step-by-Step Tutorial)








