Building a Simple Meme Phrase Generator in Python

Have you ever wanted to create memes but felt like your sense of humor wasn’t quite up to the task?

Or maybe you’re just looking for a fun and quirky Python project to showcase your skills?

In this article, I’ll walk you through building a simple yet creative Meme Phrase Generator using pure Python.

While I admit that my sense of humor might not be the best, this project is an excellent starting point for generating random, funny phrases.

And who knows?

You might even get a laugh or two out of it! (No that’s quite tough)

Let’s get started…

Basic Structure

Here, we’re defining a class named MyMemeGenerator.

__init__ Method (Constructor):

In this method, we’re planning to initialize the class with several lists.

Method Definition:
Inside our class, we’ve defined another method named generate_meme. This method will eventually be responsible for creating the meme phrase by combining words from the lists and a template.

self Parameter:
The self parameter is a reference to the current instance of the class. It allows you to access variables and methods associated with that specific object.

Return Type:
The -> str syntax is a type hint that indicates this method is expected to return a string. This string will be the generated meme phrase.

Note: The pass statement is used as a placeholder, indicating that we’ll fill in this method later.

import random

class MyMemeGenerator:
    def __init__ (self, nouns:list[str], verbs:list[str], adjectives:list[str], popular_phrases:list[str], templates:list[str],):
        pass

    def generate_meme(self) -> str:
        pass

Initializing The Constructor

In this step, we’re adding functionality to the __init__ method of our MyMemeGenerator class.

The constructor is like the setup phase for our class, where we prepare everything needed for the meme generator to work.

def __init__ (self, nouns:list[str], verbs:list[str], adjectives:list[str], popular_phrases:list[str], templates:list[str],):
        """The templates should have {nouns} {verbs} {adjectives} {popular_phrases} in them to be replaced"""
        self.word_bank = {
            "nouns": nouns,
            "verbs": verbs,
            "adjectives": adjectives,
            "popular_phrases": popular_phrases
        }
        self.templates = [templates]

Core Meme Generator

This step is where the magic happens!

The generate_meme method picks a random template, replaces placeholders like {nouns} with actual words from your lists, and returns a complete and funny meme phrase.

It’s like filling in the blanks in a mad-lib to create a random and quirky sentence!

def generate_meme(self) -> str:
        phrase = str(random.choice(self.templates)).split(' ')

        for index, word in enumerate(phrase):
            if "{nouns}" in word:
                replace_with = random.choice(self.word_bank['nouns'])
                phrase[index] = replace_with

            elif "{verbs}" in word:
                replace_with = random.choice(self.word_bank['verbs'])
                phrase[index] = replace_with

            elif "{adjectives}" in word:
                replace_with = random.choice(self.word_bank['adjectives'])
                phrase[index] = replace_with

            elif "{popular_phrases}" in word:
                replace_with = random.choice(self.word_bank['popular_phrases'])
                phrase[index] = replace_with

        return ' '.join(phrase)

Usage

This is where your creativity needs to takeover and mine didn’t. 😅

# Example usage:
nouns = ["cat", "pineapple", "internet", "dinosaur"]
verbs = ["jumps", "eats", "laughs", "runs"]
adjectives = ["fuzzy", "giant", "angry", "invisible"]
popular_phrases = ["to infinity and beyond", "just do it", "why so serious", "winter is coming"]

templates = [
    "The {adjectives} {nouns} {verbs} over the {nouns}",
    "{popular_phrases}! The {nouns} {verbs} like it's {adjectives}",
    "When the {nouns} {verbs}, {adjectives} things happen"
]

# Creating an instance of MyMemeGenerator
meme_generator = MyMemeGenerator(nouns, verbs, adjectives, popular_phrases, templates)

# Generating a meme phrase
print(meme_generator.generate_meme())

Full Source Code: Simple Meme Phrase Generator in Python

Click Here for the full code
import random

class MyMemeGenerator:
    def __init__ (self, nouns:list[str], verbs:list[str], adjectives:list[str], popular_phrases:list[str], templates:list[str],):
        """The templates should have {nouns} {verbs} {adjectives} {popular_phrases} in them to be replaced"""
        self.word_bank = {
            "nouns": nouns,
            "verbs": verbs,
            "adjectives": adjectives,
            "popular_phrases": popular_phrases
        }
        self.templates = [templates]

    def generate_meme(self) -> str:
        phrase = str(random.choice(self.templates)).split(' ')

        for index, word in enumerate(phrase):
            if "{nouns}" in word:
                replace_with = random.choice(self.word_bank['nouns'])
                phrase[index] = replace_with

            elif "{verbs}" in word:
                replace_with = random.choice(self.word_bank['verbs'])
                phrase[index] = replace_with

            elif "{adjectives}" in word:
                replace_with = random.choice(self.word_bank['adjectives'])
                phrase[index] = replace_with

            elif "{popular_phrases}" in word:
                replace_with = random.choice(self.word_bank['popular_phrases'])
                phrase[index] = replace_with

        return ' '.join(phrase)
            

# Example usage:
nouns = ["cat", "pineapple", "internet", "dinosaur"]
verbs = ["jumps", "eats", "laughs", "runs"]
adjectives = ["fuzzy", "giant", "angry", "invisible"]
popular_phrases = ["to infinity and beyond", "just do it", "why so serious", "winter is coming"]

templates = [
    "The {adjectives} {nouns} {verbs} over the {nouns}",
    "{popular_phrases}! The {nouns} {verbs} like it's {adjectives}",
    "When the {nouns} {verbs}, {adjectives} things happen"
]

# Creating an instance of MyMemeGenerator
meme_generator = MyMemeGenerator(nouns, verbs, adjectives, popular_phrases, templates)

# Generating a meme phrase
print(meme_generator.generate_meme())

Leave a Reply