Created
February 22, 2015 08:00
-
-
Save lykkin/6c004246c6ba7b3b6ff5 to your computer and use it in GitHub Desktop.
dumb markov thing
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 random import random | |
class node: | |
name = '' # the token's value | |
followingStates = None # <next name>: <num of times seen> | |
numSeen = 0 # number of times token has been seen | |
def __init__(self, name): | |
self.name = name | |
self.numSeen = 1 | |
self.followingStates = {} | |
def incState(self, state): | |
if state in self.followingStates: | |
self.followingStates[state] += 1 | |
else: | |
self.followingStates[state] = 1 | |
def getNextState(self): | |
idx = self.numSeen * random() | |
for nextName, nextSeen in self.followingStates.iteritems(): | |
if idx <= nextSeen: | |
return nextName | |
idx -= nextSeen | |
def __str__(self): | |
return self.name | |
class markov: | |
lookup = {} # <node name>: <node instance> | |
currentNode = None | |
def incWord(self, word): | |
if word not in self.lookup: | |
self.lookup[word] = node(word) | |
if self.currentNode is not None: | |
self.currentNode.incState(word) | |
self.currentNode = self.lookup[word] | |
def produceChain(self, n): | |
outputNode = self.getStartNode() | |
res = [] | |
for x in range(n): | |
res += [str(outputNode)] | |
# try to get another state, return if you hit a terminal? | |
try: | |
outputNode = self.lookup[outputNode.getNextState()] | |
except: | |
return res | |
return res | |
def populateTable(self, stream): | |
for word in stream: | |
self.incWord(word) | |
self.currentNode = None | |
def getStartNode(self): | |
idx = int(random()*len(self.lookup)) | |
return self.lookup.values()[idx] | |
m = markov() | |
m.populateTable(['i', 'am', 'not', 'a', 'crook']) | |
print m.produceChain(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment