Last active
March 4, 2016 23:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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