Created
August 30, 2018 03:37
-
-
Save pganti/5083361360fe3fdfab6016e93240e501 to your computer and use it in GitHub Desktop.
markov chain
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
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