smartTruncate_python_1.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. def smartTruncate(content, begin=0, length=100, addSuffix=False, suffix=' ...', addPrefix=False, prefix='... '):
  2. '''Truncate a string without cutting a sentence in the middle.
  3. Syntax: smartTruncate(content, begin=0, length=100, addSuffix=False, suffix=' ...', addPrefix=False, prefix='... ')
  4. Returns dictionary object with trimmed string 'result' and the index value where it ended 'trimmed_at_index'
  5. '''
  6. def finalize(string, wasChanged=False):
  7. 'This function adds the prefix and suffix and builds the return object'
  8. if wasChanged:
  9. #if a begin value was given, and prefix was requested, add prefix
  10. if begin != 0 and addPrefix is True:
  11. string = ''.join([prefix, string])
  12. #if suffix was requested, add suffix
  13. if addSuffix is True:
  14. string = ''.join([string, suffix])
  15. #if it was trimmed, include that in the result set
  16. if end_of_sentence:
  17. return {'result': string,'trimmed_at_index': end_of_sentence + 1}
  18. else:
  19. return {'result': string}
  20. #if it wasn't changed then just return the string
  21. else:
  22. return {'result': string}
  23. #cut beginning value from content
  24. if begin != 0:
  25. mod_content = content[begin:]
  26. wasChanged = True
  27. #make sure variable mod_content is created
  28. else:
  29. mod_content = content
  30. #if content.length is less than desired length then return the content unmodified
  31. if len(mod_content) <= length:
  32. return finalize(mod_content, wasChanged)
  33. #else if the content.length is longer than desired length then:
  34. else:
  35. #first, trim character count to desired length
  36. mod_content = content[0:length-1]
  37. #add a trailing space in case the final character is a period and we already ended on a perfect sentence boundary
  38. mod_content = ''.join([mod_content,' '])
  39. #second, find the last-most period or sentence termination including the period
  40. end_of_sentence = mod_content.rfind('. ')+1
  41. #third, trim to the last full sentence
  42. mod_content = content[0:end_of_sentence+1]
  43. #final, return the modified content
  44. return finalize(mod_content, wasChanged=True)