Created
June 28, 2016 22:18
-
-
Save vaaas/ca1e6dc143b01033ed26fc578a03bc52 to your computer and use it in GitHub Desktop.
Markov chain based name generator. Takes the name of a text file with names, delimited by spaces as an argument.
This file contains 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 sys | |
import random | |
def process (text, length): | |
for line in text.split("\n"): | |
if len(line) < length+1: continue | |
for i in range(len(line) - length): | |
yield tuple(line[i:i+length+1]) | |
def parse (text, length=2): | |
db = {} | |
for unit in process(text, length): | |
key = unit[:length] | |
val = unit[length] | |
if key in db: | |
db[key].append(val) | |
else: | |
db[key] = [val] | |
return db | |
def generate (db, length): | |
key = random.choice(list(db.keys())) | |
lst = list(key) | |
length -= len(key) | |
while length > 0: | |
if not key in db: | |
break | |
new = random.choice(db[key]) | |
lst.append(new) | |
key = key[1:] + (new,) | |
length -= 1 | |
return "".join(lst) | |
def main (): | |
text = open(sys.argv[1],"r").read() | |
db = parse(text, 3) | |
for i in range(10): print(generate(db, 5)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment