tts_preprocessor.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import re
  2. from num2words import num2words
  3. alphabet_map = {
  4. "A": " Ei ",
  5. "B": " Bee ",
  6. "C": " See ",
  7. "D": " Dee ",
  8. "E": " II ",
  9. "F": " Eff ",
  10. "G": " Jee ",
  11. "H": " Eich ",
  12. "I": " Eye ",
  13. "J": " Jay ",
  14. "K": " Kay ",
  15. "L": " El ",
  16. "M": " Emm ",
  17. "N": " Enn ",
  18. "O": " Ohh ",
  19. "P": " Pii ",
  20. "Q": " Queue ",
  21. "R": " Are ",
  22. "S": " Ess ",
  23. "T": " Tee ",
  24. "U": " You ",
  25. "V": " Vii ",
  26. "W": " Double You ",
  27. "X": " Ex ",
  28. "Y": " Why ",
  29. "Z": "Zed" # Zed is weird, as I (da3dsoul) am American, but most of the voice models sound British, so it matches
  30. }
  31. def preprocess(string):
  32. string = remove_surrounded_chars(string)
  33. string = string.replace('"', '')
  34. string = string.replace('“', '')
  35. string = string.replace('\n', ' ')
  36. string = remove_commas(string)
  37. string = hyphen_range_to(string)
  38. string = num_to_words(string)
  39. string = string.strip()
  40. # TODO Try to use a ML predictor to expand abbreviations. It's hard, dependent on context, and whether to actually
  41. # try to say the abbreviation or spell it out as I've done below is not agreed upon
  42. # For now, expand abbreviations to pronunciations
  43. string = replace_abbreviations(string)
  44. return string
  45. def replace_abbreviations(string):
  46. pattern = re.compile(r'[\s("\'\[<][A-Z]{2,4}[\s,.?!)"\'\]>]')
  47. result = string
  48. while True:
  49. match = pattern.search(result)
  50. if match is None:
  51. break
  52. start = match.start()
  53. end = match.end()
  54. result = result[0:start] + replace_abbreviation(result[start:end]) + result[end:len(result)]
  55. return result
  56. def replace_abbreviation(string):
  57. result = ""
  58. for char in string:
  59. result = match_mapping(char, result)
  60. return result
  61. def match_mapping(char, result):
  62. for mapping in alphabet_map.keys():
  63. if char == mapping:
  64. return result + alphabet_map[char]
  65. return result + char
  66. def remove_surrounded_chars(string):
  67. # this expression matches to 'as few symbols as possible (0 upwards) between any asterisks' OR
  68. # 'as few symbols as possible (0 upwards) between an asterisk and the end of the string'
  69. return re.sub(r'\*[^*]*?(\*|$)', '', string)
  70. def hyphen_range_to(text):
  71. pattern = re.compile(r'(\d+)[-–](\d+)')
  72. result = pattern.sub(lambda x: x.group(1) + ' to ' + x.group(2), text)
  73. return result
  74. def num_to_words(text):
  75. pattern = re.compile(r'\d+')
  76. result = pattern.sub(lambda x: num2words(int(x.group())), text)
  77. return result
  78. def remove_commas(text):
  79. import re
  80. pattern = re.compile(r'(\d),(\d)')
  81. result = pattern.sub(r'\1\2', text)
  82. return result
  83. def __main__(args):
  84. print(preprocess(args[1]))
  85. if __name__ == "__main__":
  86. import sys
  87. __main__(sys.argv)