Skip to content

Commit

Permalink
Add min_words optional argument to make_sentence, to complement max_w…
Browse files Browse the repository at this point in the history
…ords
  • Loading branch information
brienna committed May 20, 2020
1 parent 3f3bfd9 commit d6d826f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions markovify/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,15 @@ def make_sentence(self, init_state=None, **kwargs):
If `test_output` is set as False then the `test_sentence_output` check
will be skipped.
If `max_words` is specified, the word count for the sentence will be
evaluated against the provided limit.
If `max_words` or `min_words` are specified, the word count for the sentence will be
evaluated against the provided limit(s).
"""
tries = kwargs.get('tries', DEFAULT_TRIES)
mor = kwargs.get('max_overlap_ratio', DEFAULT_MAX_OVERLAP_RATIO)
mot = kwargs.get('max_overlap_total', DEFAULT_MAX_OVERLAP_TOTAL)
test_output = kwargs.get('test_output', True)
max_words = kwargs.get('max_words', None)
min_words = kwargs.get('min_words', None)

if init_state != None:
prefix = list(init_state)
Expand All @@ -211,7 +212,7 @@ def make_sentence(self, init_state=None, **kwargs):

for _ in range(tries):
words = prefix + self.chain.walk(init_state)
if max_words != None and len(words) > max_words:
if max_words != None and len(words) > max_words or len(words) < min_words:
continue
if test_output and hasattr(self, "rejoined_text"):
if self.test_sentence_output(words, mor, mot):
Expand Down

0 comments on commit d6d826f

Please sign in to comment.