str_processing.py 644 B

123456789101112131415161718192021222324252627282930
  1. import re
  2. from qwen_agent.utils.utils import has_chinese_chars
  3. def rm_newlines(text):
  4. if text.endswith('-\n'):
  5. text = text[:-2]
  6. return text.strip()
  7. rep_c = ' '
  8. if has_chinese_chars(text):
  9. rep_c = ''
  10. text = re.sub(r'(?<=[^\.。::\d])\n', rep_c, text)
  11. return text.strip()
  12. def rm_cid(text):
  13. text = re.sub(r'\(cid:\d+\)', '', text)
  14. return text
  15. def rm_hexadecimal(text):
  16. text = re.sub(r'[0-9A-Fa-f]{21,}', '', text)
  17. return text
  18. def rm_continuous_placeholders(text):
  19. text = re.sub(r'[.\- —。_*]{7,}', '\t', text)
  20. text = re.sub(r'\n{3,}', '\n\n', text)
  21. return text