Fork of https://github.com/alokprasad/fastspeech_squeezewave to also fix denoising in squeezewave
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.

64 lines
2.1 KiB

  1. """ from https://github.com/keithito/tacotron """
  2. import re
  3. valid_symbols = [
  4. 'AA', 'AA0', 'AA1', 'AA2', 'AE', 'AE0', 'AE1', 'AE2', 'AH', 'AH0', 'AH1', 'AH2',
  5. 'AO', 'AO0', 'AO1', 'AO2', 'AW', 'AW0', 'AW1', 'AW2', 'AY', 'AY0', 'AY1', 'AY2',
  6. 'B', 'CH', 'D', 'DH', 'EH', 'EH0', 'EH1', 'EH2', 'ER', 'ER0', 'ER1', 'ER2', 'EY',
  7. 'EY0', 'EY1', 'EY2', 'F', 'G', 'HH', 'IH', 'IH0', 'IH1', 'IH2', 'IY', 'IY0', 'IY1',
  8. 'IY2', 'JH', 'K', 'L', 'M', 'N', 'NG', 'OW', 'OW0', 'OW1', 'OW2', 'OY', 'OY0',
  9. 'OY1', 'OY2', 'P', 'R', 'S', 'SH', 'T', 'TH', 'UH', 'UH0', 'UH1', 'UH2', 'UW',
  10. 'UW0', 'UW1', 'UW2', 'V', 'W', 'Y', 'Z', 'ZH'
  11. ]
  12. _valid_symbol_set = set(valid_symbols)
  13. class CMUDict:
  14. '''Thin wrapper around CMUDict data. http://www.speech.cs.cmu.edu/cgi-bin/cmudict'''
  15. def __init__(self, file_or_path, keep_ambiguous=True):
  16. if isinstance(file_or_path, str):
  17. with open(file_or_path, encoding='latin-1') as f:
  18. entries = _parse_cmudict(f)
  19. else:
  20. entries = _parse_cmudict(file_or_path)
  21. if not keep_ambiguous:
  22. entries = {word: pron for word,
  23. pron in entries.items() if len(pron) == 1}
  24. self._entries = entries
  25. def __len__(self):
  26. return len(self._entries)
  27. def lookup(self, word):
  28. '''Returns list of ARPAbet pronunciations of the given word.'''
  29. return self._entries.get(word.upper())
  30. _alt_re = re.compile(r'\([0-9]+\)')
  31. def _parse_cmudict(file):
  32. cmudict = {}
  33. for line in file:
  34. if len(line) and (line[0] >= 'A' and line[0] <= 'Z' or line[0] == "'"):
  35. parts = line.split(' ')
  36. word = re.sub(_alt_re, '', parts[0])
  37. pronunciation = _get_pronunciation(parts[1])
  38. if pronunciation:
  39. if word in cmudict:
  40. cmudict[word].append(pronunciation)
  41. else:
  42. cmudict[word] = [pronunciation]
  43. return cmudict
  44. def _get_pronunciation(s):
  45. parts = s.strip().split(' ')
  46. for part in parts:
  47. if part not in _valid_symbol_set:
  48. return None
  49. return ' '.join(parts)