Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import sys, codecs, random, string, re
- # just a simple story generator
- # let me know what you do with it
- # tweet -> @eigenbom
- # expands a term, using a grammar and a world (a dictionary of concrete things)
- def expand(grammar, world, start_term):
- m = re.search('[.*]', start_term)
- e2 = start_term
- def repl(matchobj):
- m = matchobj.group(1)
- attrib = (matchobj.group(0)[0]=="$")
- matched = ((not attrib) and m in grammar) or (attrib and m in world)
- if not matched:
- # not yet matched .. so hold off?
- return matchobj.group(0)
- if attrib: e = world[m]
- else: e = grammar[m]
- replacement = None
- if type(e) is list:
- replacement = random.choice(e)
- elif hasattr(e, '__call__'): # if function
- replacement = e(grammar, world, e2)
- else:
- replacement = e
- # if replacement is concrete, add it to the world ...
- if not attrib:
- world[m] = replacement
- # pprint(world)
- return replacement
- while True:
- # then expand
- e1 = re.sub('\[(\w*)\]', repl, e2)
- e1 = re.sub('\$(\w*)', repl, e1)
- if e1 == e2: break
- else: e2 = e1
- return e2
- # shorthand
- def o(str):
- return [s for s in str.split(",")]
- def generate_story():
- grammar = dict(
- raised_by_2 = o("wolves,donkeys,goats"),
- raised_by = o("[raised_by_2],violent [raised_by_2],happy [raised_by_2]"),
- raise_location = o("forest,lake,ruins,marsh,castle,swamp"),
- loc_adj = o("dark,pleasant,never-ending,smelly"),
- raise_location_desc = o("[raise_location],[loc_adj] [raise_location]"),
- raised_where = [
- " in a [raise_location_desc]",
- " near a [raise_location_desc]",
- " nowhere near a [raise_location_desc]",
- None],
- raised = [
- "was raised by [raised_by][raised_where]",
- "grew up around [raised_by][raised_where]"],
- major_skill = o("dance,socialise,paint"),
- minor_skill = o("knit,sleep,criticise"),
- growing_up_2 = [
- " and [minor_skill]",
- ", but $he was more interested in how to [minor_skill]",
- " and, to a lesser extent, how to [minor_skill]",
- ", but ate a lot of $fav_food",
- " and eat $fav_food",
- None
- ],
- growing_up = [
- "The $raised_by_2 taught [him] how to [major_skill][growing_up_2]",
- "$he learnt how to [major_skill][growing_up_2]",
- "Early on, $he took a fascination to [major_skill][growing_up_2]"
- ],
- enemy = o("a dragon,a ghost,a troll,a battletoad"),
- enemy_action = [
- " and stole [his] $fav_food.",
- ", demanding that $he $major_skill.",
- " and smiled.",
- ],
- event = ["[enemy] appeared[enemy_action]", "nothing much happened"],
- event_start = ["One day, ", "Once, while wandering in the $raise_location, ", "Minding [his] own business, suddenly ", None],
- story = "$he [raised]. [growing_up]. [event_start][event].",
- # helpers
- him = lambda g,w,_: "him" if w["he"]=="he" else "her",
- his = lambda g,w,_: "his" if w["he"]=="he" else "her",
- )
- world = {}
- # add a few details
- world["he"] = "he" if random.random()>0.5 else "she"
- world["fav_food"] = random.choice(["spaghetti","weetbix","sandwich","pies"])
- history = expand(grammar, world, "[story]")
- sentences = [s.strip() for s in history.split(".")]
- sentences = [s.capitalize() + "." for s in sentences if len(s)>0]
- history = " ".join(sentences).strip()
- return history
- if __name__=="__main__":
- print(generate_story())
Advertisement
Add Comment
Please, Sign In to add comment