cleaners.py 2.6 KB

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