Skip to content

Instantly share code, notes, and snippets.

@csinchok
Created April 4, 2013 19:28
Show Gist options
  • Save csinchok/5313395 to your computer and use it in GitHub Desktop.
Save csinchok/5313395 to your computer and use it in GitHub Desktop.
Simple grammar generator
import random
class Announcement(object):
def __init__(self, article):
self.grammars['article'] = [article]
start = ['"%(article)s" is a %(hyperbole)s, %(name)s!']
grammars = {
'hyperbole': [
"Hot Potato",
"Screamin' Green Been",
"Deviled Egg"
],
'name': [
'folks',
'ladies and gents',
'bros'
]
}
def __getitem__(self, key):
if key in self.grammars:
return random.choice(self.grammars[key])
else:
raise KeyError
def __str__(self):
return random.choice(self.start) % self
announcement = Announcement("Some testing article")
print(announcement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment