Repository navigation
Expand file tree
/
Copy pathpronunciation.py
More file actions
183 lines (142 loc) · 4.49 KB
/
Copy pathpronunciation.py
File metadata and controls
183 lines (142 loc) · 4.49 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import re
from pathlib import Path
STUTTER_RE = re.compile(
r"\b(?P<prefixes>(?:[A-Za-z]{1,3}-)+)(?P<word>[A-Za-z]+(?:'[A-Za-z]+)?)\b"
)
STUTTER_PIECE_TO_ARPA = {
# Single consonant sounds
"b": "B",
"d": "D",
"f": "F",
"g": "G",
"h": "HH",
"j": "JH",
"k": "K",
"l": "L",
"m": "M",
"n": "N",
"p": "P",
"r": "R",
"s": "S",
"t": "T",
"v": "V",
"w": "W",
"y": "Y",
"z": "Z",
# Common consonant clusters / digraphs
"ch": "CH",
"sh": "SH",
"th": "TH",
"dh": "DH",
"ph": "F",
"wh": "W",
"ng": "NG",
# Optional clusters
"bl": "B L",
"br": "B R",
"cl": "K L",
"cr": "K R",
"dr": "D R",
"fl": "F L",
"fr": "F R",
"gl": "G L",
"gr": "G R",
"pl": "P L",
"pr": "P R",
"sk": "S K",
"sl": "S L",
"sm": "S M",
"sn": "S N",
"sp": "S P",
"st": "S T",
"sw": "S W",
"tr": "T R",
}
VOWELS = {"a", "e", "i", "o", "u"}
class PronunciationProcessor:
def __init__(self, cmu_dict_dir: str):
self.pronunciation_dict = self._load_pronunciation_dictionary(cmu_dict_dir)
def _load_pronunciation_dictionary(self, cmu_dict_dir: str) -> dict[str, str]:
dictionary_path = Path(cmu_dict_dir) / "merged.dict.txt"
pronunciations = {}
with open(dictionary_path, "r", encoding="utf-8") as file:
for line in reversed(file.read().splitlines()):
if not line.strip():
continue
word, arpa = line.split(" ", 1)
pronunciations[word.upper()] = arpa.strip()
return pronunciations
def apply(self, text: str, punctuation=r"!?,.;:'\"", eos_token: bool = True) -> str:
output = []
for raw_token in text.split(" "):
if not raw_token:
continue
start_chars, token, end_chars = self._split_edge_punctuation(
raw_token,
punctuation,
)
converted = self._token_to_arpa(token)
output.append(f"{start_chars}{converted}{end_chars}")
result = " ".join(output)
if eos_token and result and result[-1] != ";":
result += ";"
return result
def _split_edge_punctuation(
self,
token: str,
punctuation: str,
) -> tuple[str, str, str]:
start_chars = ""
end_chars = ""
while len(token) > 1 and token[-1] in punctuation:
end_chars = token[-1] + end_chars
token = token[:-1]
while len(token) > 1 and token[0] in punctuation:
start_chars += token[0]
token = token[1:]
return start_chars, token, end_chars
def _token_to_arpa(self, token: str) -> str:
stutter_arpa = self._stutter_to_arpa(token)
if stutter_arpa:
return stutter_arpa
word_arpa = self._word_to_arpa(token)
if word_arpa:
return self._wrap_arpa(word_arpa)
return token
def _word_to_arpa(self, word: str) -> str | None:
return self.pronunciation_dict.get(word.upper())
def _stutter_to_arpa(self, token: str) -> str | None:
match = STUTTER_RE.fullmatch(token)
if not match:
return None
word = match.group("word")
word_arpa = self._word_to_arpa(word)
if not word_arpa:
return None
prefix_parts = match.group("prefixes").rstrip("-").split("-")
arpa_parts = []
for part in prefix_parts:
part_arpa = self._stutter_piece_to_arpa(part, word_arpa)
if not part_arpa:
return None
# Makes "s-s-stutter" sound like "ss-ss-stutter",
# not "ess ess stutter".
if len(part) == 1:
part_arpa = f"{part_arpa} {part_arpa}"
arpa_parts.append(self._wrap_arpa(part_arpa))
arpa_parts.append(self._wrap_arpa(word_arpa))
return " ".join(arpa_parts)
def _stutter_piece_to_arpa(self, piece: str, word_arpa: str) -> str | None:
piece = piece.lower()
if piece in STUTTER_PIECE_TO_ARPA:
return STUTTER_PIECE_TO_ARPA[piece]
if piece in VOWELS:
return self._first_arpa_phone(word_arpa)
def _first_arpa_phone(self, word_arpa: str) -> str | None:
phones = word_arpa.split()
if not phones:
return None
return phones[0]
def _wrap_arpa(self, arpa: str) -> str:
return "{" + arpa + "}"