You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.4 KiB

  1. """ from https://github.com/keithito/tacotron """
  2. '''
  3. Cleaners are transformations that run over the input text at both training and eval time.
  4. Cleaners can be selected by passing a comma-delimited list of cleaner names as the "cleaners"
  5. hyperparameter. Some cleaners are English-specific. You'll typically want to use:
  6. 1. "english_cleaners" for English text
  7. 2. "transliteration_cleaners" for non-English text that can be transliterated to ASCII using
  8. the Unidecode library (https://pypi.python.org/pypi/Unidecode)
  9. 3. "basic_cleaners" if you do not want to transliterate (in this case, you should also update
  10. the symbols in symbols.py to match your data).
  11. '''
  12. import re
  13. from unidecode import unidecode
  14. from .numbers import normalize_numbers
  15. # Regular expression matching whitespace:
  16. _whitespace_re = re.compile(r'\s+')
  17. # List of (regular expression, replacement) pairs for abbreviations:
  18. _abbreviations = [(re.compile('\\b%s\\.' % x[0], re.IGNORECASE), x[1]) for x in [
  19. ('mrs', 'misess'),
  20. ('mr', 'mister'),
  21. ('dr', 'doctor'),
  22. ('st', 'saint'),
  23. ('co', 'company'),
  24. ('jr', 'junior'),
  25. ('maj', 'major'),
  26. ('gen', 'general'),
  27. ('drs', 'doctors'),
  28. ('rev', 'reverend'),
  29. ('lt', 'lieutenant'),
  30. ('hon', 'honorable'),
  31. ('sgt', 'sergeant'),
  32. ('capt', 'captain'),
  33. ('esq', 'esquire'),
  34. ('ltd', 'limited'),
  35. ('col', 'colonel'),
  36. ('ft', 'fort'),
  37. ]]
  38. def expand_abbreviations(text):
  39. for regex, replacement in _abbreviations:
  40. text = re.sub(regex, replacement, text)
  41. return text
  42. def expand_numbers(text):
  43. return normalize_numbers(text)
  44. def lowercase(text):
  45. return text.lower()
  46. def collapse_whitespace(text):
  47. return re.sub(_whitespace_re, ' ', text)
  48. def convert_to_ascii(text):
  49. return unidecode(text)
  50. def basic_cleaners(text):
  51. '''Basic pipeline that lowercases and collapses whitespace without transliteration.'''
  52. text = lowercase(text)
  53. text = collapse_whitespace(text)
  54. return text
  55. def transliteration_cleaners(text):
  56. '''Pipeline for non-English text that transliterates to ASCII.'''
  57. text = convert_to_ascii(text)
  58. text = lowercase(text)
  59. text = collapse_whitespace(text)
  60. return text
  61. def english_cleaners(text):
  62. '''Pipeline for English text, including number and abbreviation expansion.'''
  63. text = convert_to_ascii(text)
  64. text = lowercase(text)
  65. text = expand_numbers(text)
  66. text = expand_abbreviations(text)
  67. text = collapse_whitespace(text)
  68. return text