Message Generator

As I mentioned in my last post, I was working on a more generic version of the generator which was driven by XML. This version is written in Python. I wrote in for Python 2.7 using the py-dom-xpath library. I have included the python code here as well as three example XML files: Cave Johnson’s Lemon rant, a quote from model Caprice, and my take on the old Steve Jackson Games “Message form the Illuminated Master” generator.

Here is the code and the XML. Enjoy and let me know what you thing. (I am working on a Powershell version and I am looking at creating a widget for WordPress, by the way.)

generator.py

# Message Generator
# Written by Taylor Kent
# Copyright 2012 by Michael Taylor Kent

# Get the libraries we need
import argparse
import random
import xml.dom.minidom
import xpath
import re

# Setup commandline argument Parser
parser = argparse.ArgumentParser(description="Open an XML file, generate a random messgae based on the contents of the XML file, and display it")
parser.add_argument('msgdatafile', metavar='InputXmlFile', nargs='?', type=argparse.FileType('r'), help='The XML file containing the message generation details')
parser.add_argument('count', metavar='Integer', nargs='?', type=int, help='The Number of random messages to generate (Optional)', default=1)
parser.add_argument('outfile', metavar='OutputFile', nargs='?', type=argparse.FileType('w'), help='The file to put the generated messages into (Optional)')
# Parse Commandline arguements
args = parser.parse_args()
# Open XML data file
msgdata = xml.dom.minidom.parse(args.msgdatafile)
i = 1
for i in range(args.count):	
	# Get a list of all the message patterns in the xml file
	xpathqry = '//messagedata/patterns/pattern'
	msgpatterns = xpath.find(xpathqry, msgdata)
	# Pick a message pattern
	msgpattern = msgpatterns[random.randint(0,(len(msgpatterns) - 1))]
	# Create message
	# Gather all the parts
	msgpatternname = str(msgpattern.getAttribute('name'))
	msgpatternvalue = str(msgpattern.getAttribute('value'))
	# Create Message
	# Create list of replacements
	msgparts = []
	# Check for Shared lists being used (if any)
	xpathqry = '//messagedata/patterns/pattern[@name=\'' + msgpatternname +  '\']/sharedlistsused/list'
	msgsharedlists = xpath.find(xpathqry, msgdata)
	if len(msgsharedlists) > 0:
		for msglist in msgsharedlists:
			msglistname = str(msglist.getAttribute('name')) 
			msglistregex = str(msglist.getAttribute('replacepattern'))
			# Get the shared list	
			xpathqry = '//messagedata/sharedlists/list[@name=\'' + msglistname + '\']/items/item'
			msgitems = xpath.find(xpathqry, msgdata)
			msgitemchoice = msgitems[random.randint(0,(len(msgitems) - 1))]
			msgitemvalue = str(msgitemchoice.getAttribute('value'))
			msgparts.append([msglistregex, msgitemvalue])
	# Check for Specific lists (if any)
	xpathqry = '//messagedata/patterns/pattern[@name=\'' + msgpatternname +  '\']/specificlists/list'
	msgspecificlists = xpath.find(xpathqry, msgdata)
	if len(msgspecificlists) > 0:
		for msglist in msgspecificlists:
			msglistname = str(msglist.getAttribute('name')) 
			msglistregex = str(msglist.getAttribute('replacepattern'))
			# Get list items
			xpathqry = '//messagedata/patterns/pattern[@name=\'' + msgpatternname +  '\']/specificlists/list[@name=\'' + msglistname + '\']/items/item'
			msgitems = xpath.find(xpathqry, msgdata)
			msgitemchoice = msgitems[random.randint(0,(len(msgitems) - 1))]
			msgitemvalue = str(msgitemchoice.getAttribute('value'))
			msgparts.append([msglistregex, msgitemvalue])
			# Get linked items (if any)
			msgcleanedvalue = re.sub("\'", "\\\\\'", msgitemvalue)
			xpathqry = '//messagedata/patterns/pattern[@name=\'' + msgpatternname +  '\']/specificlists/list[@name=\'' + msglistname + '\']/items/item[@value=\"' + msgitemvalue + '\"]/linkeditem'
			msglinkeditems = xpath.find(xpathqry, msgdata)
			if len(msglinkeditems) > 0:
				for msglinkeditem in msglinkeditems:
					msglinkeditemvalue = str(msglinkeditem.getAttribute("value"))
					msglinkeditemregex = str(msglinkeditem.getAttribute("replacepattern"))	
					msgparts.append([msglinkeditemregex, msglinkeditemvalue])
	# Actuall generate the message (stupid immutable strings in Python makes this harder than it needs to be)
	msg = []
	if len(msgparts) > 0:
		for msgpart in msgparts:
			if len(msg) == 0:
				msg.append(re.sub(msgpart[0],msgpart[1],msgpatternvalue))
			else:
				msg.append(re.sub(msgpart[0],msgpart[1],msg[len(msg) - 1]))
	else:
		msg.append(msgpatternvalue)
	# Get the complete message and display it
	msgfinal = msg[len(msg) - 1]
	print (msgfinal)
	print
	# write messages to the specified file, if it was supplied
	if args.outfile != None:
		args.outfile.write(msgfinal)
		args.outfile.write('\r\n\r\n')

lemons.xml

Original Quote:
“Alright, I’ve been thinking. When life gives you lemons, don’t make lemonade – make life take the lemons back! Get mad! I don’t want your damn lemons, what am I supposed to do with these? Demand to see life’s manager. Make life rue the day it thought it could give Cave Johnson lemons. Do you know who I am? I’m the man who’s gonna burn your house down! With the lemons. I’m going to to get my engineers to invent a combustible lemon that burns your house down!”

<messagedata>
	<sharedlists>
	</sharedlists>
	<patterns>
		<pattern name="lemons" value="Alright, I've been thinking. When rp1thing1rp1 gives you rp1thing2rp1, rp1thing3rp1. Make rp1thing1rp1 take the rp1thing2rp1 back! Get mad! I don't want your damn rp1thing2rp1! What am I supposed to do with these?! Demand to see rp1thing1rp1 manager! Make rp1thing1rp1 rue the day rp1pronoun1rp1 though it could give rp1namerp1 rp1thing2rp1! Do you know who I am? I'm rp1pronoun2rp1 who's gonna burn your rp1thing4rp1 down! With rp1thing2rp1! I'm going to get rp1thing5rp1 a combustible rp1thing6rp1 that burns your rp1thing4rp1 down!">
			<sharedlistsused>
			</sharedlistsused>
			<specificlists>
				<list  name="thing4"  replacepattern="rp1thing4rp1">
					<items>
						<item value="house"/>
						<item value="refrigerator"/>
						<item value="capitol building"/>
						<item value="studio"/>
						<item value="restaurant"/>
						<item value="candy factory"/>
						<item value="warehouse"/>
						<item value="computer"/>
					</items>
				</list>
				<list  name="thing5" replacepattern="rp1thing5rp1">
					<items>
						<item value="my engineers to invent"/>
						<item value="my friends to cook up"/>
						<item value="some rebel scum to steal"/>
						<item value="a dark mage to conjure up"/>
						<item value="some wombats to sniff out"/>
						<item value="an army of cockroaches to carry"/>
						<item value="a genius to think up"/>
						<item value="Lex Luthor to invert"/>
						<item value="a gang of Mad Scientists to build a time machine and go the future to find"/>
					</items>
				</list>
				<list  name="thing1" replacepattern="rp1thing1rp1">
					<items>
						<item value="life">
							<linkeditem value="it" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="John">
							<linkeditem value="he" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="the government">
							<linkeditem value="it" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="the King">
							<linkeditem value="he" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="the Chruch">
							<linkeditem value="it" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="the dark mage">
							<linkeditem value="he" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="the system">
							<linkeditem value="it" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="Russia">
							<linkeditem value="Russia" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="Cthulhu">
							<linkeditem value="the great dark lord" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="that supid dog">
							<linkeditem value="it" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="the devil">
							<linkeditem value="it" replacepattern="rp1pronoun1rp1"/>
						</item>
						<item value="Darth Vader">
							<linkeditem value="he" replacepattern="rp1pronoun1rp1"/>
						</item>
					</items>
				</list>
				<list  name="thing2" replacepattern="rp1thing2rp1">
					<items>
						<item value="lemons">
							<linkeditem value="don't make lemonade" replacepattern="rp1thing3rp1"/>
							<linkeditem value="lemon" replacepattern="rp1thing6rp1"/>
						</item>
						<item value="ice cubes">
							<linkeditem value="don't make a Tom Collins" replacepattern="rp1thing3rp1"/>
							<linkeditem value="ice cube" replacepattern="rp1thing6rp1"/>
						</item>
						<item value="taxes">
							<linkeditem value="don't file a tax return" replacepattern="rp1thing3rp1"/>
							<linkeditem value="tax" replacepattern="rp1thing6rp1"/>
						</item>
						<item value="bad music">
							<linkeditem value="don't just listen to it" replacepattern="rp1thing3rp1"/>
							<linkeditem value="song" replacepattern="rp1thing6rp1"/>
						</item>
						<item value="undercooked pizza">
							<linkeditem value="don't send it back" replacepattern="rp1thing3rp1"/>
							<linkeditem value="pizza" replacepattern="rp1thing6rp1"/>
						</item>
						<item value="mexican candy">
							<linkeditem value="don't eat it and puke" replacepattern="rp1thing3rp1"/>
							<linkeditem value="candy" replacepattern="rp1thing6rp1"/>
						</item>
						<item value="crappy software">
							<linkeditem value="don't just install it and post bitter forum post online about how it sucks" replacepattern="rp1thing3rp1"/>
							<linkeditem value="software" replacepattern="rp1thing6rp1"/>
						</item>
						<item value="a Yugo">
							<linkeditem value="don't drive it" replacepattern="rp1thing3rp1"/>
							<linkeditem value="Yugo" replacepattern="rp1thing6rp1"/>
						</item>
					</items>
				</list>
				<list  name="name" replacepattern="rp1namerp1">
					<items>
						<item value="Cave Johnson">
							<linkeditem value="the man" replacepattern="rp1pronoun2rp1"/>
						</item>
						<item value="Billy">
							<linkeditem value="the boy" replacepattern="rp1pronoun2rp1"/>
						</item>
						<item value="Sheela">
							<linkeditem value="the woman" replacepattern="rp1pronoun2rp1"/>
						</item>
						<item value="Merlin">
							<linkeditem value="the wizard" replacepattern="rp1pronoun2rp1"/>
						</item>
						<item value="Robin Hood">
							<linkeditem value="the hood" replacepattern="rp1pronoun2rp1"/>
						</item>
						<item value="that guy">
							<linkeditem value="the guy" replacepattern="rp1pronoun2rp1"/>
						</item>
						<item value="Richard Nixon">
							<linkeditem value="the zombie (and former president)" replacepattern="rp1pronoun2rp1"/>
						</item>
						<item value="JFK">
							<linkeditem value="the gunslinger" replacepattern="rp1pronoun2rp1"/>
						</item>
					</items>
				</list>
			</specificlists>
		</pattern>
	</patterns>
</messagedata>

mombasa.xml

Original Quote:
“I knocked someone over in Mombasa and he broke his leg. But I gave him 10 Pounds and he seemed really pleased.”

<messagedata>
	<sharedlists>
	</sharedlists>
    <patterns>
	<pattern name="mombasa" value="I knocked someone over in rp1placerp1 and rp1pronoun1rp1 broke rp1pronoun2rp1 rp1bodypartrp1. But I gave rp1pronoun3rp1 rp1compensationrp1 and rp1pronoun1rp1 rpt1endingrpt1.">
		<sharedlistsused>
		</sharedlistsused>
		<specificlists>
			<list  name="place" replacepattern="rp1placerp1">
				<items>
					<item value="Mombasa"/>
					<item value="Washington State"/>
					<item value="Walmart"/>
					<item value="Hell"/>
					<item value="Detroit"/>
					<item value="Las Vegas"/>
					<item value="line for concert tickets"/>
					<item value="my dream"/>
					<item value="the robot factory"/>
					<item value="Moombase Alpha"/>
					<item value="the Dungeon of Dreadmore"/>
				</items>
			</list>
			<list  name="bodypart" replacepattern="rp1bodypartrp1">
				<items>
					<item value="leg"/>
					<item value="arm"/>
					<item value="jaw"/>
					<item value="finger"/>
					<item value="foot"/>
					<item value="head"/>
					<item value="ankle"/>
					<item value="wrist"/>
					<item value="shoulder"/>
					<item value="back"/>
					<item value="tooth"/>
				</items>
			</list>
			<list  name="compensation" replacepattern="rp1compensationrp1">
				<items>
					<item value="10 pounds"/>
					<item value="37 cents"/>
					<item value="a lemon"/>
					<item value="someone elses business card"/>
					<item value="a hall pass"/>
					<item value="an apple"/>
					<item value="a 30 dollar bill"/>
					<item value="a happy meal"/>
					<item value="a ticket to Disney"/>
					<item value="a dime bag"/>
					<item value="3 pennies"/>
					<item value="a Alien artifact"/>
					<item value="a bible"/>
					<item value="my sweaty socks"/>
					<item value="a Lady Gaga CD"/>
					<item value="a radioactive bolt"/>
					<item value="an electric shock"/>
					<item value="a punch in the face"/>
					<item value="drug"/>
					<item value="a red plastic dinosaur"/>
					<item value="some pocket lint"/>
					<item value="a pizza"/>
					<item value="a jar of pickles"/>
					<item value="a ticket to last years superbowl"/>
					<item value="my car keys"/>
					<item value="a dress"/>
					<item value="some pants"/>
					<item value="a toy truck"/>
					<item value="a bowl of ice-cream"/>
					<item value="some mexican candy"/>
					<item value="a boot to the head"/>
				</items>
			</list>
			<list name="ending" replacepattern="rpt1endingrpt1">
				<items>
					<item value="sued me"/>
					<item value="pulled out a shotgun"/>
					<item value="gave me a Tickle Me Elmo"/>
					<item value="stole my wallet"/>
					<item value="ran off screaming"/>
					<item value="hit me in the face with a pie"/>
					<item value="threw a rock at me"/>
					<item value="shot me in the leg"/>
					<item value="seemed really please"/>
					<item value="returned the favor"/>
					<item value="put a curse on me"/>
					<item value="gave it back"/>
					<item value="smoked a dubie"/>
					<item value="had a heart attack"/>
					<item value="discovered the meaning of life (and didn't share it with me)"/>
					<item value="bought a roast-beef hoagie"/>
					<item value="whent to Taco Bell"/>
					<item value="ate a bug"/>
					<item value="told me about a great investment opportunity"/>
				</items>
			</list>
			<list name="pronoun" replacepattern="rp1pronoun1rp1">
				<items>
					<item value="he">
						<linkeditem value="his" replacepattern="rp1pronoun2rp1"/>
						<linkeditem value="him" replacepattern="rp1pronoun3rp1"/>
					</item>
					<item value="she">
						<linkeditem value="her" replacepattern="rp1pronoun2rp1"/>
						<linkeditem value="her" replacepattern="rp1pronoun3rp1"/>
					</item>
				</items>
			</list>
		</specificlists>
	</pattern>
    </patterns>
</messagedata>

illuminator.xml

Examples:
“The President writes Internal Security and the tasty woman

Cthulhu will be the house

George W. Bush smacks Robin Hood and the cold FNORD

Batman hates Captain Kirk, and The President smacks the pen”

<messagedata>
	<sharedlists>
		<list  name="adjective">
			<items>
				<item value="blue"/>
				<item value="hot"/>
				<item value="alien"/>
				<item value="magical"/>
				<item value="fast"/>
				<item value="hot"/>
				<item value="cold"/>
				<item value="cool"/>
				<item value="ferocious"/>
				<item value="frightening"/>
				<item value="small"/>
				<item value="huge"/>
				<item value="Japanese"/>
				<item value="old"/>
				<item value="smelly"/>
				<item value="fat"/>
				<item value="phat"/>
				<item value="angry"/>
				<item value="irate"/>
				<item value="mellow"/>
				<item value="blue-green"/>
				<item value="hyper"/>
				<item value="paranormal"/>
				<item value="super"/>
				<item value="supernatural"/>
				<item value="happy"/>
				<item value="scary"/>
				<item value="cute"/>
				<item value="watery"/>
				<item value="Y2K-ready"/>
				<item value="ready"/>
				<item value="tasty"/>
			</items>
		</list>
		<list  name="thing">
			<items>
				<item value="MP3"/>
				<item value="alien"/>
				<item value="car"/>
				<item value="book"/>
				<item value="(CENSORED)"/>
				<item value="(REDACTED)"/>
				<item value="calendar"/>
				<item value="helicoper"/>
				<item value="briefcase"/>
				<item value="phone"/>
				<item value="dinosaur"/>
				<item value="robot"/>
				<item value="android"/>
				<item value="lizard"/>
				<item value="ball"/>
				<item value="hat"/>
				<item value="submarine"/>
				<item value="computer"/>
				<item value="man"/>
				<item value="woman"/>
				<item value="spaceship"/>
				<item value="tank"/>
				<item value="pen"/>
				<item value="fluid"/>
				<item value="lake"/>
				<item value="house"/>
				<item value="TV"/>
				<item value="radio"/>
				<item value="warbot"/>
				<item value="crate"/>
				<item value="dog"/>
				<item value="cat"/>
				<item value="CEO"/>
				<item value="CTO"/>
				<item value="Yugo"/>
				<item value="horse"/>
				<item value="FNORD"/>
				<item value="button"/>
			</items>
		</list>
		<list name="action">
			<items>
				<item value="writes"/>
				<item value="discovers"/>
				<item value="torments"/>
				<item value="fights"/>
				<item value="smokes"/>
				<item value="drives"/>
				<item value="pilots"/>
				<item value="runs from"/>
				<item value="is"/>
				<item value="was"/>
				<item value="will be"/>
				<item value="hates"/>
				<item value="jumps on"/>
				<item value="infiltrates"/>
				<item value="adapts to"/>
				<item value="fills"/>
				<item value="live in"/>
				<item value="live on"/>
				<item value="swims in"/>
				<item value="runs from"/>
				<item value="bounces"/>
				<item value="cooks"/>
				<item value="kills"/>
				<item value="smacks"/>
			</items>
		</list>
		<list name="person">
			<items>
				<item value="Dick Cheney"/>
				<item value="Luke Skywalker"/>
				<item value="Cave Johnson"/>
				<item value="Merlin"/>
				<item value="King Arthur"/>
				<item value="Robin Hood"/>
				<item value="Bill Clinton"/>
				<item value="Bob"/>
				<item value="Killer Kane"/>
				<item value="Superman"/>
				<item value="Lex Luthor"/>
				<item value="Batman"/>
				<item value="George W. Bush"/>
				<item value="The President"/>
				<item value="Some guy"/>
				<item value="(CENSORED)"/>
				<item value="(REDACTED)"/>
				<item value="The Quarterback"/>
				<item value="Zeus"/>
				<item value="Mr Fixit"/>
				<item value="Conan"/>
				<item value="Cthulhu"/>
				<item value="GLaDOS"/>
				<item value="The Master Computer"/>
				<item value="HAL 9000"/>
				<item value="KGB"/>
				<item value="Internal Security"/>
				<item value="General Hagenkraut"/>
				<item value="Captain Kirk"/>
			</items>
		</list>
	</sharedlists>
	<patterns>
		<pattern name="illumination1" value="The rp1adj1rp1 rp1thingrp1 is rp1adj2rp1">
			<sharedlistsused>
				<list  name="adjective" replacepattern="rp1adj1rp1"/>
				<list  name="thing" replacepattern="rp1thingrp1"/>
				<list  name="adjective" replacepattern="rp1adj2rp1"/>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>
		<pattern name="illumination2" value="The rp1adj1rp1 rp1thingrp1 is rp1adj1rp1">
			<sharedlistsused>
				<list  name="adjective" replacepattern="rp1adj1rp1"/>
				<list  name="thing" replacepattern="rp1thingrp1"/>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>
		<pattern name="illumination3" value="rp1personrp1 rp1actionrp1 the rp1thingrp1">
			<sharedlistsused>
				<list  name="person" replacepattern="rp1personrp1"/>
				<list  name="action" replacepattern="rp1actionrp1"/>
				<list  name="thing" replacepattern="rp1thingrp1"/>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>
		<pattern name="illumination4" value="rp1person1rp1 rp1actionrp1 rp1person2rp1 and the rp1adjrp1 rp1thingrp1">
			<sharedlistsused>
				<list  name="person" replacepattern="rp1person1rp1"/>
				<list  name="action" replacepattern="rp1actionrp1"/>
				<list  name="person" replacepattern="rp1person2rp1"/>
				<list  name="adjective" replacepattern="rp1adjrp1"/>
				<list  name="thing" replacepattern="rp1thingrp1"/>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>-->
		<pattern name="illumination5" value="rp1person1rp1 rp1actionrp1 rp1person2rp1 and the rp1thingrp1">
			<sharedlistsused>
				<list  name="person" replacepattern="rp1person1rp1"/>
				<list  name="action" replacepattern="rp1actionrp1"/>
				<list  name="person" replacepattern="rp1person2rp1"/>
				<list  name="thing" replacepattern="rp1thingrp1"/>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>
		<pattern name="illumination6" value="rp1person1rp1 rp1actionrp1 rp1person2rp1 and rp1person2rp1's rp1thingrp1">
			<sharedlistsused>
				<list  name="person" replacepattern="rp1person1rp1"/>
				<list  name="action" replacepattern="rp1actionrp1"/>
				<list  name="person" replacepattern="rp1person2rp1"/>
				<list  name="thing" replacepattern="rp1thingrp1"/>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>
		<pattern name="illumination7" value="rp1person1rp1 rp1action1rp1 rp1person2rp1, and rp1person3rp1 rp1action2rp1 the rp1thingrp1">
			<sharedlistsused>
				<list  name="person" replacepattern="rp1person1rp1"/>
				<list  name="action" replacepattern="rp1action1rp1"/>
				<list  name="person" replacepattern="rp1person2rp1"/>
				<list  name="person" replacepattern="rp1person3rp1"/>
				<list  name="action" replacepattern="rp1action2rp1"/>
				<list  name="thing" replacepattern="rp1thingrp1"/>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>
		<pattern name="illumination8" value="FNORD">
			<sharedlistsused>
			</sharedlistsused>
			<specificlists>
			</specificlists>
		</pattern>
	</patterns>
</messagedata>

Enjoy. Please give credit where credit is due if you use the code. I would also be interested in seeing any patterns you come up with.

Fun with PowerShell

I have been trying to come up with the best way to start this blog. I have debated writing a post about why I am doing this blog, what it will be about, Resistentialism, me, etc. In the end I decided to share a share a stupid little scripting project I have been playing around with to help set the serious tone I believe this blog deserves.

I’ve been getting back into scripting lately since changing jobs. I have not needed todo all that much scripting in the past few years because I was in more of leadership role. Now I get to write scripts again, I am remember how much fun it is.

Back in the day, I stumbled upon a little script/program called the “Illuminator” that Steve Jackson Games was using to generate random, er, I mean coded, message from the “illuminated masters” of the Illuminati. I build something like it use a data file back then using VB 6.0 that I gave to SJG. (It was more or less useless to them considering that they are a Mac/Linux shop or at least were.) And now I use it as a way to learn various parts of new scripting languages when I need to. (I learn best “hands on”, and having a nice repeatable and expandable project to use for that makes things easier.) The project itself general requires using arrays, random numbers, files (writing to and reading from), and user input. (This example does not read from any files, but I am working on an expanded version that will. I will share that when it is complete. I also plan to write a Python version and possibly a Lua version as well.)

In the past, I would just generate random silly messages, but with the viral nature of Cave Johnson’s “Rue the day” speech from Portal 2, I decided to use that is the base message framework. If you have not played Portal 2 or hear of Cave Johnson, watch is video, thing will make a little more sense afterward:

Okay, with all that being said here is my silly little PowerShell “Rue the day” speech/message generator: (It has been testing with PowerShell 2 on Windows 7 64-bit)

##############################################################
# Name ruetheday.ps1
# Author: Taylor Kent
# Version: 1.0
# Description:
# 
# This script creates a random version on Cave Johnson's Rue The Day speech from Portal 2
# and either outputs it to the screen or a specifed file
# 
# You can add more things to the arrays used to generate the new version of the speech
# by modifying the code below
#
#
# Usage:
#
# ruetheday.ps1  [-number ] [-outfile ]
# 
# Usage Notes:
#
# Both parameters are optional
#
# History:
#
# 1.0 - Intial Creation
#
##############################################################

##############################################################
# Functions
##############################################################
# A standard way of displaying error text
function DisplayError
{
	param ([string]$errmsg, [string]$usageinfo)

	if ($usageinfo -ne "")
	{
$msg =@"
********************
ERROR: $errmsg
$usageinfo
********************
"@
	}
	else
	{
$msg =@"
********************
ERROR: $errmsg
********************
"@
	}
	Write-Host $msg -ForegroundColor red
}
# Check to see if a value is numeric
function isNumeric ($x) {
    $x2 = 0
    $isNum = [System.Int32]::TryParse($x, [ref]$x2)
    return $isNum
}
##############################################################
# Main
##############################################################
# Create Usage Info Constant
$USAGEINFO = @"
 ruetheday.ps1  [-number ] [-outfile ]
"@
# Check for commandline parameters
$quit = $false
$number = 1
$outfile = $null
if ($args.length -gt 0)
{
	for ($i = 0; $i -lt $args.Length; $i++)
	{
		switch ($args[$i])
		{
			"-?"
			{
				$quit = $true
				Write-Host "How to use:"
				Write-Host
				Write-Host $USAGEINFO
				Write-Host
			}
			"-help"
			{
				$quit = $true
				Write-Host "How to use:"
				Write-Host
				Write-Host $USAGEINFO
				Write-Host
			}
			"-number"
			{
				$i++
				if ((isNumeric $args[$i]) -eq $false)
				{
					$errmsg = $args[$i] + " is not a number and will be ignored"
					DisplayError $errmsg, $usageinfo
				}
				else
				{
					$number = [int]$args[$i]
				}
			}
			"-outfile"
			{
				$i++
				if ((test-path $args[$i] -IsValid) -eq $false)
				{
					$errmsg = $args[$i] + " is not a vaild path and will be ignored"
					DisplayError $errmsg, $usageinfo				
				}
				else
				{
					$outfile = $args[$i]
				}
			}
			default
			{
				$errmsg = $args[$i] + " is not vaild and will be ignored"
				DisplayError $errmsg, $usageinfo
			}
		}
	}
}
if ($quit -eq $false)
{
	for ($j=1; $j -le $number; $j++)
	{
		# Lists
		$thing1list = "life","John","the government","the king","the church","a dark mage","the system","Russia","Cthulhu","that stupid dog","the Devil","Darth Vader"
		$thing2list = "lemons","ice","taxes","bad music","undercooked pizza","mexican candy","a broken CD","crappy software"
		$thing3list = "don't make lemonade","don't make ice cubes","don't file a tax return", "don't just listen to it", "don't send it back", "don't just eat it and puke", "don't just eat it", "don't just install it and live with it"
		$thing4list = "house","refigerator","capitol building","studio","restaurant","candy factory","warehouse","computer"
		$thing5list = "engineers", "friends", "rebels","mages","wombat","army of cockroaches","Bucky Johnson","squid","Mad Scientists"
		$namelist = "Cave Johnson","Billy","Sheela","Merlin","Robin Hood","that guy","Richard Nixion"
		$pronounlist = "man","guy","woman","wizard","outlaw","guy","former president"
		
		# Get a random number generator
		$rand = New-Object System.Random
		
		# Pick stuff
		$thing1 = $thing1list[([int](($thing1list.Length - 1) * $rand.NextDouble()))]
		
		# $thing2 and $thing3 are linked
		$thing2number = [int](($thing2list.Length - 1) * $rand.NextDouble())

		$thing2 = $thing2list[$thing2number]
		$thing3 = $thing3list[$thing2number]

		$thing4 = $thing4list[([int](($thing4list.Length - 1) * $rand.NextDouble()))]

		# $name and $pronoun are linked
		$namenumber = [int](($namelist.Length - 1) * $rand.NextDouble())

		$name = $namelist[$namenumber]
		$pronoun = $pronounlist[$namenumber]

		$thing5 = $thing5list[([int](($thing5list.Length - 1) * $rand.NextDouble()))]

		# Generate Speech
$message = @"
Alright, I've been thinking. When $thing1 gives you $thing2, $thing3. Make $thing1 take the $thing2 back! Get mad! I don't want your damn $thing2! What am I supposed to do with these?! Demand to see $thing1 manager! Make $thing1 rue the day it though it could give $name $thing2! Do you know who I am? I'm the $pronoun who's gonna burn your $thing4 down! With $thing2! I'm going to get my $thing5 to invent a combustible $thing2 that burns your house down!
"@
		
		# If a file was supplied write the speech to it
		if ($outfile -ne $null)
		{
			$message | Out-File $outfile -Append
			"" | Out-File $outfile -Append
		}
		# Write the speech to the host
		Write-Host
		Write-Host $message
	}
	Write-Host

	$rand = $null
}

Here is a samples of it’s output:

Alright, I’ve been thinking. When Russia gives you undercooked pizza, don’t send it back. Make Russia take the undercooked pizza back! Get mad! I don’t want your damn undercooked pizza! What am I supposed to do with these?! Demand to see Russia manager! Make Russia rue the day it though it could give Merlin undercooked pizza! Do you know who I am? I’m the wizard who’s gonna burn your capitol building down! With undercooked pizza! I’m going to get my mages to invent a combustible undercooked pizza that burns your house down!

Got have fun with it. I look forward to your comments.