Skip to content

Commit

Permalink
Merge 0dae4b0 into 6968649
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSzymanski committed Sep 26, 2018
2 parents 6968649 + 0dae4b0 commit 7a45441
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions markovify/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, corpus, state_size, model=None):
self.state_size = state_size
self.model = model or self.build(corpus, self.state_size)
self.precompute_begin_state()
self.mem = {}

def build(self, corpus, state_size):
"""
Expand Down Expand Up @@ -90,8 +91,12 @@ def move(self, state):
choices = self.begin_choices
cumdist = self.begin_cumdist
else:
choices, weights = zip(*self.model[state].items())
cumdist = list(accumulate(weights))
try:
choices, cumdist = self.mem[state]
except:
choices, weights = zip(*self.model[state].items())
cumdist = list(accumulate(weights))
self.mem[state] = (choices, cumdist)
r = random.random() * cumdist[-1]
selection = choices[bisect.bisect(cumdist, r)]
return selection
Expand Down

0 comments on commit 7a45441

Please sign in to comment.