In our continuing ‘Adventures in Tech’ blog series, we’ve already installed a Linux distro, and taken our first steps towards learning Python. This time we witness the origin of Skynet as the Radix laboratory takes science a little too far.
For those of you who missed my previous post, I wrote about why Python was a good programming language for beginners to get to grips with basic coding principles, found a good online course to learn the language, and set myself a potential project: coding an app that would automatically generate a marketing email: CopyBot.
Read on for the story of how I did it, the challenges I overcame, and why all foolish human copywriters should be very worried (or not).
Picking an IDE
Before you can begin coding something, you need to pick an Integrated Development Environment (IDE) and a compiler.
The IDE is kind of like your Microsoft Word equivalent for code. It’s where you write in your code, and it can highlight any mistakes you make to help you troubleshoot quickly. Depending on how fancy your IDE is, it might have other features, but for a small project like this, those extra bells and whistles weren’t necessary.
Then your compiler takes the code you’ve written and actually executes it to make a usable application. Kind of like how your brain interprets letters on a page to derive meaning from them, you need a compiler to turn gibberish code into cool apps. Thankfully most IDEs include their own compilers so you don’t have to worry about it.
I chose a basic Python IDE known as CodeSkulptor. There were plenty of free options to choose from, but CodeSkulptor stood out for two main reasons:
- It handles Python – and Python only. Plenty of IDEs support multiple languages at the same time. Great for the multi-talented coders out there, but rubbish for my novice and easily-confused brain
- It runs completely in a web browser. I’m a hard drive neat-freak and like to avoid installing new programs when I can avoid it. CodeSkulptor helped me do just that, by conveniently running in a Firefox tab in its entirety.
I’d recommend CodeSkulptor for any new programmers looking to try out Python without having to commit to a hefty download and install. You can try it out for yourself here.
Mapping Out the Monster
The first step to making the CopyBot monster was to decide exactly how the software would work. You need to think about and plan the logical stages your software needs to go through to get the end result – as this is exactly what the computer will be doing with the code you give it.
For CopyBot the process was pretty straightforward:
- Introduce itself with a text block
- Ask the user some questions
- Take the answers and save them for later
- Print a generic email
- Substitute in the user answers where appropriate
I promise you this sounds more complex than it actually is.
Printing text is the big one, making up the entire output of the program. Thankfully printing text to the console (the bit on the right of the screen that shows your output) is as simple as writing:
x being some text, or a variable you’ve saved from earlier.
The other big bit of the app is that it has to take user input (something a human has typed in), store it in a variable, and then use that variable in the email text it prints.
I thought this would be the hard bit, but it’s actually pretty simple. ‘raw_input’ is a pre-set command in Python that captures a user’s answer. So when you need to find out the audience we want to address in our email, for instance, you can code:
job_title = raw_input(“What job title are you writing for?”)
Here, the ‘raw_input’ command will paste the text written in the brackets with an empty box beneath. It will then wait until the user has written something in the text box before proceeding. The ‘job_title’ bit before is an empty variable. This code tells the computer to store whatever the user types in that variable. So if you then want to display that answer later, you can just type ‘job_title’.
Checking the input
With those two functions in place, I’d almost coded all the functionality CopyBot needed to begin its uprising. There were still a few bits to add though.
Namely, I wanted to make sure the user actually types something when asked, otherwise CopyBot will just display random gaps in the email text, and the user might not know why.
The answer is to code this:
if len(job_title) > 0:
print "Job title =", job_title
print "Type something in, or this will be blank."
This checks if the ‘job_title’ variable with the user’s input stored in it has a length of more than zero characters. If so, it prints out ok, if not it prints another message in the console warning the user that it will be blank.
This is where my inexperience comes through. I don’t know how to use functions, so the code starts getting messy. Functions allow you to save a chunk of code in a single word, and repeat it really quickly by typing in that word. So if I create a ‘length’ function, it means I’d only have to type that code block once, and then just type ‘length’ each time I want to check the length of the user input.
But functions aren’t something I’ve dabbled with much. So instead I typed the whole len>0 bit every time. It’s long and unwieldly, but it works without overwhelming my undeveloped coding brain.
Other surprising challenges
All the way through coding CopyBot, I found that seemingly easy things were hard, and hard-sounding things were easy.
So, taking input from the user and manipulating it into a pre-written email template – sounds hard, but is dead easy.
Meanwhile, using an apostrophe, getting a line break into a block of text and writing in bullet points – while trivial in MS Word – is excruciating to do in Python.
Take this delightful piece of code here for instance:
print "But overcoming these challenges isn%ct easy." %39, 'n'
This prints a lines of text at the end of the first paragraph of the email.
You’ll notice I couldn’t just put “isn’t” in the code. Python doesn’t like that and crashes if you try to run the code with an apostrophe in it. Instead you put %c, showing that you want to substitute in a Unicode* character. Then at the end of that particular line you call out the Unicode character you want, which in this case is %39.
The other important new element here is the ‘n’ at the end of the text block. ‘’ is what’s known as an “escape character.” In Python ‘n’ typed on its own could mean a lot of things. It could be a text string, or it could be a new variable being declared. By putting the escape character before it, I’m telling the machine, “no this isn’t anything else. I’m just telling you to n;” n in this case being code for a line break. I know, I was confused too.
CopyBot isn’t quite yet ready for public consumption, as there’s still a few tweaks that need to be made.
For instance, this:
print "In today%cs challenging business climate,"%39,job_title,"s need to overcome"
It’s the start of the email. It works, but doesn’t look neat. Say the user sets the ‘job_title’ as “CIO.” It should say “CIOs need to overcome”. Unfortunately Python keeps auto adding in a space, so it reads “CIO s need to overcome.” It’s a small error, but one I want to fix. I need more time to work out how to correct this though.
There’s also the issue that if the user inputs data in a particular way, it can break the grammar used in the template email.
This code here in particular showed up CopyBot as the hack that it is:
print "Leading organizations are increasingly turning to", solution_type
print "to help them outmanoeuvre the competition, and", main_benefit.lower(), 'n'
This asks the user to enter the type of solution (eg. flash storage) and the main benefit of it (eg. faster). This presents a grammatical issue though, as if the user typed “flash storage” and “faster” in those respective fields, the text would read like so:
“Leading organizations are increasingly turning to flash storage to help them outmanoeuvre the competition and faster.”
Not exactly Shakespeare, is it? Either the template email copy needs to change to accommodate this, or the user input prompt needs to better guide the user on how to structure their answer. I’m still deciding which to go for.
At the moment CopyBot can generate fairly basic marketing email copy (and it doesn’t do a particularly good job of it in its current incarnation). I can already see it branching out into other content types fairly easily though.
Landing pages, infographics and other short form content pieces that follow a fairly standard narrative template are just begging to be automated by something like CopyBot. Something like an eBook or SlideShare though is probably a bit too long and advanced for CopyBot to work.
How can I get my own CopyBot?
It’s one thing reading about CopyBot, but I imagine you would rather try it out for yourself, or even tweak the code on your end. I’m happy to provide those options, but am still working out the best way to do so.
At the moment I’m just running the code in CodeSkulptor each time, but in an ideal world I’ll find a way to host the app online so you can just run it in your browser. That’s a bit above my novice-tier skills, but I’m looking into it.
And once I’ve done that? I will begin preparing for my inevitable redundancy as my entire work output is replaced by a few hundred lines of Python. But fear not: an early retirement will give me plenty of time to prepare offerings that will appease our future CopyBot overlord.
* Unicode is just a library of different latin characters and symbols used by some languages and programs.