Categories
level 1 python

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.

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.