Skip to content

Instantly share code, notes, and snippets.

@dela3499
Last active March 4, 2016 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dela3499/7c0082132a975affc680 to your computer and use it in GitHub Desktop.
Save dela3499/7c0082132a975affc680 to your computer and use it in GitHub Desktop.
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:
print(x)
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