SpoonerismBot

SpoonerismBot is a python script for Twitter. If you send a message to @SpoonerismBot, it will reply with a spoonerized version.

@dinosaurs_rarr the lord is a shoving leopard

— SpoonerismBot (@SpoonerismBot) August 14, 2016

I wrote SpoonerismBot using python. It is available under an MIT licence.

Spoonerism

A Spoonerism is a play on words where the first sounds of two words in a phrase are swapped. For instance, "the Lord is a loving shepherd" becomes "the Lord is a shoving leopard", with the sound /l/ from "loving" swapped with the /ʃ/ ("sh") from "shepherd".

Given an input phrase, SpoonerismBot tries to generate a spoonerism phonetically. If that fails, it generates a lexical fallback.

Phonetic

In order to swap sounds, we first need a way to work out what sounds (also known as phonemes) make up each word. We also need a list of words, and I used the ENABLE2K scrabble word list.

I then ran each word through the text-to-speech software built into OS X. I used the NSSpeechSynthesizer.phonemesFromText_() method to get a representation of how the computer would pronounce the word. I stripped out any information that wasn't phonemic, and save the results in a dictionary.

When parsing a message, the script would look up how each word is pronounced. It would take a pair of words, find the first vowel sound in each and swaps the preceding sounds. It would do this for each possible pair of words in the message. I also told it how to work out the pronunciation of possessive forms (e.g. "doctor's") which weren't listed in the dictionary.

The script then tries to check whether the results of each swap are valid words. It does this by seeing if the new string of phonemes matches any entry in the dictionary. If there are several homophones (words that sound the same), it picks one at random. If more than one attempt resulted in a valid spoonerism, it picks one at random.

Lexical

If SpoonerismBot isn't able to find a spoonerism that sounds right for your words, it has a fallback option based on the letters used to spell the words.

It takes the two longest dictionary words in your message, looks for the first vowel in each and swaps the letters beforehand. The results don't always involve real words though, but it's a worst-case scenario.

Ongoing loop

The script for SpoonerismBot runs on a headless Raspberry Pi computer at home.

It runs on a loop every minute. It updates the profile description, checks for new messages, generates a reply to each, and then tweets it. It uses Python Twitter Tools to access Twitter's API from Python.

Sigmund Freudian Bot

I used the same phonetic dictionary to make another bot based on the idea of a Freudian slip. This is where you say something that isn't quite what you meant but reveals a subconscious thought. It is also based on the assumption that subconscious thoughts are generally rude.

When you send @freudianBot a message, it replaces sends you back the most similar-sounding message that can be generated by replacing one of the words with a swear word.

To make the list of swear words, I scraped results from noswearing.com and ran it through the same speech-to-text software. Some of these words are utterly vile and racist. I can only apologize.

Feel free to try it for yourself on Twitter