code: def splitText(text):
      
      segments = []
      remaining_text = __class__.cleanSpaces(text)
  
      while len(remaining_text) > __class__.MAX_SEGMENT_SIZE:
        cur_text = remaining_text[:__class__.MAX_SEGMENT_SIZE]
  
        # try to split at punctuation
        split_idx = __class__.findLastCharIndexMatching(cur_text,
                                                        # https://en.wikipedia.org/wiki/Unicode_character_property#General_Category
                                                        lambda x: unicodedata.category(x) in ("Ps", "Pe", "Pi", "Pf", "Po"))
        if split_idx is None:
          # try to split at whitespace
          split_idx = __class__.findLastCharIndexMatching(cur_text,
                                                          lambda x: unicodedata.category(x).startswith("Z"))
        if split_idx is None:
          # try to split at anything not a letter or number
          split_idx = __class__.findLastCharIndexMatching(cur_text,
                                                          lambda x: not (unicodedata.category(x)[0] in ("L", "N")))
        if split_idx is None:
          # split at the last char
          split_idx = __class__.MAX_SEGMENT_SIZE - 1
  
        new_segment = cur_text[:split_idx + 1].rstrip()
        segments.append(new_segment)
        remaining_text = remaining_text[split_idx + 1:].lstrip(string.whitespace + string.punctuation)
  
      if remaining_text:
        segments.append(remaining_text)
  
      return segments
docs: Split text into sub segments of size not bigger than MAX_SEGMENT_SIZE.