utils.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. from __future__ import unicode_literals
  2. def truncate_sentence(text, max_chars, break_words=False, padding=0):
  3. """Truncates a sentence.
  4. :param max_chars: The maximum characters of truncated sentence.
  5. :param break_words: If you wish to truncate given sentence strictly even
  6. if it breaks a word, set it to ``True``. It defaults
  7. to ``False`` which means truncating given sentence
  8. shorter but never breaking words.
  9. :param padding: The padding size for truncating. It is usually used to
  10. keep spaces for some ending characters such as ``"..."``.
  11. :return: The truncated sentence.
  12. """
  13. if break_words:
  14. return text[:-abs(max_chars - len(text)) - padding]
  15. words = []
  16. for word in text.split():
  17. predicted_len = (
  18. sum(map(len, words)) + # length of words
  19. len(word) + # length of next word
  20. len(words) - 1 + # length of spaces
  21. padding)
  22. if predicted_len >= max_chars:
  23. break
  24. words.append(word)
  25. return ' '.join(words)