phones.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import re
  2. def parse(praat_output):
  3. word_lines = []
  4. phoneme_lines = []
  5. active_list = None
  6. for line in praat_output.split('\n'):
  7. if line == "Words:":
  8. active_list = word_lines
  9. continue
  10. if line == "Phonemes:":
  11. active_list = phoneme_lines
  12. continue
  13. active_list.append(line)
  14. data = {}
  15. pronunciation_dict = {}
  16. with open('cmudict.txt', 'r', encoding='iso-8859-1') as f:
  17. for line in f.readlines():
  18. cols = line.split()
  19. pronunciation_dict[cols[0]] = cols[1:]
  20. data['words'] = []
  21. for line in word_lines:
  22. cols = line.split('\t')
  23. data['words'].append({
  24. 'time' : float(cols[0]),
  25. 'word' : cols[1],
  26. 'expected' : (pronunciation_dict.get(cols[1].upper()) or [None]) + [None] * 10
  27. })
  28. data['phones'] = []
  29. word_index = -1
  30. phoneme_in_word_index = 0
  31. for line in phoneme_lines:
  32. cols = line.split('\t')
  33. if len(cols) < 3: continue
  34. time = float(cols[0]) if re.match(r'^-?\d+(?:\.\d+)?$', cols[0]) else None
  35. while (
  36. type(time) == float and
  37. word_index < len(data['words']) - 1 and
  38. time >= data['words'][word_index + 1]['time']
  39. ):
  40. word_index += 1
  41. phoneme_in_word_index = 0
  42. data['phones'].append({
  43. 'time': time,
  44. 'phoneme': cols[1],
  45. 'word_index': word_index,
  46. 'word': data['words'][word_index],
  47. 'word_time': data['words'][word_index]['time'],
  48. 'expected': data['words'][word_index]['expected'][phoneme_in_word_index],
  49. 'F': [None if '--' in f else float(f) for f in cols[2:]]
  50. })
  51. phoneme_in_word_index += 1
  52. return data