From d6d826f5798bd1dd9cb4e86707bbf171fc2981e5 Mon Sep 17 00:00:00 2001 From: briennakh Date: Wed, 20 May 2020 13:36:02 -0500 Subject: [PATCH 1/2] Add min_words optional argument to make_sentence, to complement max_words --- markovify/text.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/markovify/text.py b/markovify/text.py index 3cca0ee..16a6231 100644 --- a/markovify/text.py +++ b/markovify/text.py @@ -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) @@ -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): From a43ad84c6cf2d35add0c2294da924a620a379228 Mon Sep 17 00:00:00 2001 From: briennakh Date: Mon, 15 Jun 2020 01:55:17 -0500 Subject: [PATCH 2/2] Add test --- test/test_basic.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_basic.py b/test/test_basic.py index cd8c238..1beca47 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -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())