Skip to content

Commit

Permalink
Merge a43ad84 into 3f3bfd9
Browse files Browse the repository at this point in the history
  • Loading branch information
brienna committed Jun 15, 2020
2 parents 3f3bfd9 + a43ad84 commit a346937
Show file tree
Hide file tree
Showing 2 changed files with 9 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
5 changes: 5 additions & 0 deletions test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ def test_max_words(self):
sent = text_model.make_sentence(max_words=0)
assert sent is None

def test_min_words(self):
text_model = self.sherlock_model
sent = text_model.make_sentence(min_words=5)
assert len(sent.split(' ')) >= 5

def test_newline_text(self):
with open(os.path.join(os.path.dirname(__file__), "texts/senate-bills.txt")) as f:
model = markovify.NewlineText(f.read())
Expand Down

0 comments on commit a346937

Please sign in to comment.