Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions markovify/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,18 @@ def build(self, corpus, state_size):
appears.
"""

# Using a DefaultDict here would be a lot more convenient, however the memory
# usage is far higher.
model = {}

for run in corpus:
items = ([BEGIN] * state_size) + run + [END]
for i in range(len(run) + 1):
state = tuple(items[i : i + state_size])
state = model.setdefault(tuple(items[i : i + state_size]), {})
follow = items[i + state_size]
if state not in model:
model[state] = {}
try:
state[follow] += 1
except KeyError:
state[follow] = 0

if follow not in model[state]:
model[state][follow] = 0

model[state][follow] += 1
return model

def precompute_begin_state(self):
Expand Down
Loading