_cmudict.py 1.9 KB

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