Skip to content

Commit

Permalink
Merge PR #58, but with tweaks
Browse files Browse the repository at this point in the history
- Changes char_min to min_chars
- Also changes positional argument from char_limit to max_chars
- Makes length tests inclusive of thresholds, instead of exclusive
  • Loading branch information
jsvine committed Mar 24, 2017
2 parents c11601b + 0dd7e4c commit f922fa9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,6 @@ Many thanks to the following GitHub users for contributing code and/or ideas:
- [@eh11fx](https://github.com/eh11fx)
- [@ammgws](https://github.com/ammgws)
- [@OtakuMegane](https://github.com/OtakuMegane)
- [@tsunaminoai](https://github.com/tsunaminoai)

Developed at [BuzzFeed](https://www.buzzfeed.com).
11 changes: 6 additions & 5 deletions markovify/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,22 @@ def make_sentence(self, init_state=None, **kwargs):
return self.word_join(words)
return None

def make_short_sentence(self, char_limit, **kwargs):
def make_short_sentence(self, max_chars, min_chars=0, **kwargs):
"""
Tries making a sentence of no more than `char_limit` characters`,
passing **kwargs to `self.make_sentence`.
Tries making a sentence of no more than `max_chars` characters and optionally
no less than `min_chars` charcaters, passing **kwargs to `self.make_sentence`.
"""
tries = kwargs.get('tries', DEFAULT_TRIES)

for _ in range(tries):
sentence = self.make_sentence(**kwargs)
if sentence and len(sentence) < char_limit:
if sentence and len(sentence) <= max_chars and len(sentence) >= min_chars:
return sentence

def make_sentence_with_start(self, beginning, **kwargs):
"""
Tries making a sentence that begins with `beginning` string,
which should be a string of one or two words known to exist in the
which should be a string of one or two words known to exist in the
corpus. **kwargs are passed to `self.make_sentence`.
"""
split = self.word_split(beginning)
Expand Down
9 changes: 8 additions & 1 deletion test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ def test_make_sentence_with_start_three_words(self):
def test_short_sentence(self):
text_model = sherlock_model
sent = None
while sent == None:
while sent is None:
sent = text_model.make_short_sentence(45)
assert len(sent) < 45

def test_short_sentence_min_chars(self):
sent = None
while sent is None:
sent = sherlock_model.make_short_sentence(100, min_chars=50)
assert len(sent) < 100
assert len(sent) >= 50

def test_dont_test_output(self):
text_model = sherlock_model
sent = text_model.make_sentence(test_output=False)
Expand Down

0 comments on commit f922fa9

Please sign in to comment.