Ada Poetry Generator

By Russell Barnes. Posted

Ada Lovelace unveils the Analytical Engine in Scratch! This early computer looks a bit primitive, but can generate random poems

In this Scratch project, the user first chats to Ada, before clicking on her computer to generate a random poem. To achieve this, we’ll be creating and using lists, found in the Variables block category, containing words of a certain type: verbs, nouns, adjectives, and adverbs. We’ll then select randomly from these lists to create the poem, which should be different each time. They can be quite amusing.

The full article and many more Scratch tutorials can be found in our Scratch Essentials book

 Find many more Scratch projects in our Essentials book!

STEP-01 Prepare your artwork

After deleting the cat sprite as usual, you need to import the sprites and backdrop. Since they’re not in the Scratch 1.4 library, you can download them. As the Poetry backdrop is so simple – just a grey stripe at the bottom of a white canvas – you could paint it yourself, or just use ours by importing it from the folder where you’ve stored the downloaded graphics for this project. The same goes for the Banner sprite. Otherwise, import each sprite as usual, by clicking the star/folder icon above the Sprite List.

STEP-02 Ada says hello

First, we’ll get our Ada sprite to interact with the user via speech bubbles and text input when clicked, using the say and ask commands. Open the Ada sprite’s Scripts tab and then type in the code from Listing 1 below. Note that you’ll first need to create a name variable: select the Variables block category from the top-left, then click ‘Make a variable’, ‘For this sprite only’, and enter ‘name’ in the text field. You should untick the name block to stop it showing on the stage. We can now set name to answer (the user’s text input), and then add it into Ada’s response by using the join Operator block. Make sure you put a space after ‘Hi’ to avoid it being joined together with the name. After this, we add a block to get Ada to tell the user to click the computer.

 Listing 1

STEP-03 Computer beeps

Click the Computer sprite and select its Scripts tab. This is where we’ll add the workings of our poetry generator. To start with, type in the code from Listing 2 below. After a block to say ‘Here is your poem’ and the user’s name, we’ll use a Sound block to make our computer beep. Our Computer sprite already has the sound for this, or you can record/import a new one in its Sounds tab. We also add a repeat loop with two turn blocks to make the computer shake.

 Listing 2

STEP-04 Create word lists

You can’t make a poem without words. We’ll store ours in four lists: verbs, adverbs, nouns, and adjectives. Create each of these in Variables by clicking the ‘Make a list’ button, then ‘For this sprite only’, and typing its name. It will then appear on the stage: to add words to it, click the ‘+’ icon and type them in, one by one. When done, untick this list block to make it vanish from the stage. We used the following words for our lists:

Adjectives: happy, tired, hungry
Adverbs: loudly, silently, endlessly
Nouns: sea, moon, tree
Verbs: laugh, dance, burp

STEP-05 Poetry in motion

Now we have our word lists, we can use them to generate a random poem each time the computer is clicked by the user. Join the code from Listing 3 below to the bottom of your existing script for the Computer sprite. It comprises four say blocks, each of which includes an item of Variables block; this should have ‘any’ selected from its drop-down menu, to make a random selection from the list. Test the project out a few times to check that it works properly and generates random poems.

 Listing 3

STEP-06 Taking it further

While we’ve only created short lists for this example, you could add lots more words to them for greater variation in the random poems created by the computer. More, and differently constructed, say blocks can also be added to make poems longer. If you’re not keen on blank verse, why not create lists of rhyming words?

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.