Skip to content

Instantly share code, notes, and snippets.

@pganti
Created August 30, 2018 03:37
Show Gist options
  • Save pganti/5083361360fe3fdfab6016e93240e501 to your computer and use it in GitHub Desktop.
Save pganti/5083361360fe3fdfab6016e93240e501 to your computer and use it in GitHub Desktop.
markov chain
from collections import defaultdict, Counter
import random
import sys
# This is the length of the "state" the current character is predicted from.
# For Markov chains with memory, this is the "order" of the chain. For n-grams,
# n is STATE_LEN+1 since it includes the predicted character as well.
STATE_LEN = 4
data = sys.stdin.read()
model = defaultdict(Counter)
print('Learning model...')
for i in range(len(data) - STATE_LEN):
state = data[i:i + STATE_LEN]
next = data[i + STATE_LEN]
model[state][next] += 1
print('Sampling...')
state = random.choice(list(model))
out = list(state)
for i in range(400):
out.extend(random.choices(list(model[state]), model[state].values()))
state = state[1:] + out[-1]
print(''.join(out))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment