Generate models from very large corpora
masterTo manage memory when working with massive datasets, use retain_original=False to prevent Markovify from keeping the entire text corpus in memory. For extremely large datasets, you can build the model incrementally by reading files/lines and combining them using markovify.combine().
# Option A: Don't retain the original text in memory
with open("path/to/my/huge/corpus.txt") as f:
text_model = markovify.Text(f, retain_original=False)
# Option B: Incremental building via combining
combined_model = None
for (dirpath, _, filenames) in os.walk("path/to/my/huge/corpus"):
for filename in filenames:
with open(os.path.join(dirpath, filename)) as f:
model = markovify.Text(f, retain_original=False)
if combined_model:
combined_model = markovify.combine(models=[combined_model, model])
else:
combined_model = model