Skip to content

Commit 60cfbd6

Browse files
committed
Improved Chain.build() performance by 1/3.
1 parent 6f93edb commit 60cfbd6

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

markovify/chain.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,18 @@ def build(self, corpus, state_size):
7575
appears.
7676
"""
7777

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

8280
for run in corpus:
8381
items = ([BEGIN] * state_size) + run + [END]
8482
for i in range(len(run) + 1):
85-
state = tuple(items[i : i + state_size])
83+
state = model.setdefault(tuple(items[i : i + state_size]), {})
8684
follow = items[i + state_size]
87-
if state not in model:
88-
model[state] = {}
85+
try:
86+
state[follow] += 1
87+
except KeyError:
88+
state[follow] = 1
8989

90-
if follow not in model[state]:
91-
model[state][follow] = 0
92-
93-
model[state][follow] += 1
9490
return model
9591

9692
def precompute_begin_state(self):

0 commit comments

Comments
 (0)