Create a Word Cloud from Tweets

Word clouds are a fun way to analyze a text and Twitter is a treasure trove of information. Word clouds can help you find the most commonly mentioned words in a text at a glance. Twitter contains information from knowledgeable people like Elon Musk, Bill Gates, and many other entrepreneurs.

This post will cover how to create a word cloud from tweets in two steps:

  • Get all the text from tweets
  • Create a word cloud from tweets and save the image

We’ll need to get a Twitter API token first. Then, you’ll also need to download two Python modules, wordcloud, matplotlib, numpy, and requests. You can do that with the following line in the terminal:

pip install wordcloud matplotlib numpy requests

Get All the Text from Tweets

We basically follow this exact guide on how to Scrape the Text from All Tweets for a Search Term. I’ll give a basic summary of the code, but the guide contains a more detailed step-by-step breakdown.

We’ll start by handling our imports of the requests and json library as well as the Bearer Token we got from the Twitter API. Then we set up the request by defining headers and the API endpoint. Next, we’ll create a function that will perform the search and parse the response.

Our search function will take one parameter, the search term. It will check if the search term starts with an “@” or not to see if we’re reading a person’s Tweets or simply searching for a term. Then, it will build a request and scrape some Tweets. When we get the request back, we’ll join all the Tweets into a string and return it.

import requests
import json
 
from twitter_config import bearertoken
 
search_recent_endpoint = "https://api.twitter.com/2/tweets/search/recent"
headers = {
    "Authorization": f"Bearer {bearertoken}"
}
 
# automatically builds a search query from the requested term
# looks for english tweets with no links that are not retweets
# returns the tweets
def search(term: str):
    if term[0] == '@':
        params = {
            "query": f'from:{term[1:]} lang:en -has:links -is:retweet',
            'max_results': 25
        }
    else:
        params = {
            "query": f'{term} lang:en -has:links -is:retweet',
            'max_results': 25
        }
    response = requests.get(url=search_recent_endpoint, headers=headers, params=params)
    res = json.loads(response.text)
    tweets = res["data"]
    text = ". ".join( for tweet in tweets])
    return text

Create Word Cloud from Tweets and Save the Image

To create a word cloud from the tweets, we’re basically going to follow the guide on How to Create a Word Cloud in 10 Lines of Python. All we need to do is import the libraries we need, wordcloud, matplotlib, numpy, and PIL, and then create a function to make the word cloud. Note that PIL comes with matplotlib.

Our word_cloud function will take two parameters, the text to create the word cloud from and the filename we want to save the image to. We’ll set up the “stopwords” or words that we want to ignore, and then the mask. Note that you’ll have to have some image saved for the mask.

Once we’ve selected our image, we’ll create a word cloud using the WordCloud class. We’ll use matplotlib to show our word cloud and save it as a png image.

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
 
# wordcloud function
def word_cloud(text, filename):
    stopwords = set(STOPWORDS)
    frame_mask=np.array(Image.open("cloud_shape.png"))
    wordcloud = WordCloud(max_words=50, mask=frame_mask, stopwords=stopwords, background_color="white").generate(text)
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.savefig(f'{filename}.png')

Summary

In this post we went over how to scrape tweets from Twitter using the Twitter API and how to create a word cloud from the texts. We used the requests library to get the Tweets. We used the wordcloud, matplotlib, and numpy libraries to create and save the word cloud.

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

One thought on “Create a Word Cloud from Tweets

Leave a Reply

%d bloggers like this: