Skip to content

Instantly share code, notes, and snippets.

@dela3499 dela3499/
Last active Mar 4, 2016

What would you like to do?
Generate a tree of paths, and repeatedly travel random paths from root to leaf to generate strings.
import random
def lprint(xs):
for x in xs:
def repeat_and_deduplicate(f, args, n):
return list(set([f(*args) for _ in range(n)]))
# String -> Dict String String
def replace_string(s, replacements, max_depth = 20):
result = s
for i in range(max_depth):
for search, replace_options in replacements.items():
result = result.replace(search, random.choice(replace_options))
return result
replacements = \
{ '#place': [ 'my house', 'your house', '#country club']
, '#country club': [ 'Wright Park', 'the Oak Creek #OCplace']
, '#OCplace': [ 'bar', 'golf course', 'pool']
, '#preposition': [ 'to', 'toward', 'under', 'through', 'onto', 'into']
, '#verb': [ 'go', 'escape', 'travel']
initial_string = "We should #verb #preposition #place."
lprint(repeat_and_deduplicate(replace_string, [initial_string, replacements], 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.