abbreviator_1.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. def abbreviate(self, input_sent) -> str:
  2. """
  3. Abbreviates an input string, prioritizing minimizing length of longest word.
  4. Given instance `target_length` smaller than possible with min token word length of 1,
  5. returns smallest possible output given max per-token length of 1.
  6. :param input_sent
  7. :return: str string of abbreviated words
  8. """
  9. current_length = len(input_sent)
  10. if current_length <= self.target_len:
  11. return input_sent
  12. words = {word: i for i, word in enumerate(input_sent.split())}
  13. # store result list for downstream in-place token replacement
  14. result = [word for word in words]
  15. while current_length > self.target_len:
  16. sorted_words = [word for word in sorted(words.keys(), key=len, reverse=True)]
  17. longest_word = sorted_words[0]
  18. if len(longest_word) == 1:
  19. return self._re_assemble(words, result)
  20. words[longest_word[:-1]] = words.pop(longest_word)
  21. current_length -= 1
  22. if current_length <= self.target_len:
  23. return self._re_assemble(words, result)
  24. for k, v in words.items():
  25. result[v] = k
  26. return ' '.join(result)