smartTruncate_python.py 2.6 KB

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