-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculateProbability.py
More file actions
98 lines (77 loc) · 2.11 KB
/
Copy pathcalculateProbability.py
File metadata and controls
98 lines (77 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'''
calculate monophone and diphone probability from Buckeye corpus.
need one system argument: dir for buckeye corpus
'''
import os, sys
import pickle
from collections import defaultdict
def phone_conversion(phn):
if phn == "en":
return "n"
elif phn == "em":
return "m"
elif phn == "eng":
return "ng"
elif phn == "el":
return "l"
elif phn == "hh":
return "h"
elif phn == "nx":
return "n"
elif phn == "tq":
return "t"
else:
return phn
buckeye_path = sys.argv[1]
speaker_list = os.listdir(buckeye_path)
mono_dict = defaultdict(int)
di_dict = defaultdict(int)
for spk in speaker_list:
print("WORKING ON SPEAKER ", spk)
spk_path = os.path.join(buckeye_path, spk)
phone_files = [os.path.join(spk_path, phn) for phn in os.listdir(spk_path) if phn.endswith(".phones")]
for phone in phone_files:
with open(phone, "r") as f:
data = f.readlines()
for i in range(9, len(data)):
line = data[i].lstrip().rstrip()
items = line.split()
try:
phn = items[2]
except:
phn = "EMPTY"
phn = phn.replace(";", "")
if "+" in phn:
phn, _ = phn.split("+")
if (phn != "eng") and (len(phn) == 3) and (phn.islower()):
next_phn = phone_conversion(phn[-1])
phn = phone_conversion(phn[:2])
mono_dict[phn] += 1
mono_dict[next_phn] += 1
mono_dict["total_monophone"] += 2
diphone = "%s-%s" % (phn, next_phn)
di_dict[diphone] += 1
di_dict["total_diphone"] += 1
else:
phn = phone_conversion(phn)
mono_dict[phn] += 1
mono_dict["total_monophone"] += 1
if i + 1 < len(data):
# print(data[i+1])
next_line = data[i+1].lstrip().rstrip()
next_items = next_line.split()
try:
next_phn = phone_conversion(next_items[2])
except:
next_phn = "EMPTY"
next_phn = next_phn.replace(";", "")
diphone = "%s-%s" % (phn, next_phn)
di_dict[diphone] += 1
di_dict["total_diphone"] += 1
for k in sorted(list(mono_dict.keys())):
print(k, '\t', mono_dict[k])
prob_dict = {}
prob_dict["monophone"] = mono_dict
prob_dict["diphone"] = di_dict
with open('prob_dictionary_v2.pk', 'wb') as output:
pickle.dump(prob_dict, output)