-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstr2ipa.py
executable file
·212 lines (184 loc) · 6.78 KB
/
str2ipa.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""
./str2ipa.py --mapping_file data/mapping/letter-to-phone-mapping.<lang> \
--in_vocab data/vocab/vocab.<lang> \
--out_pron_dict data/pron-dict/pron-dict.<lang> \
./str2ipa.py --mapping_file data/mapping/letter-to-phone-mapping.ro \
--in_vocab data/vocab/vocab.ro \
--out_pron_dict data/pron-dict/pron-dict.ro
"""
import argparse
import itertools
import collections
import unicodedata
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--mapping_file', default='./letter-to-phone-mapping.fr')
parser.add_argument('--in_vocab')
parser.add_argument('--out_pron_dict')
parser.add_argument('--debug_match', action='store_true')
parser.add_argument('--ignore_errors', action='store_true')
args = parser.parse_args()
EMPTY_CHAR = "Ø"
class IpaRule(object):
def __init__(self, rule_line_num: int, match_str : str, consume_mask : list, replace_rules : list):
self.rule_line_num = rule_line_num
self.match_str = match_str
self.consume_mask = consume_mask
self.replace_rules = replace_rules
def TryMatch(self, s: str, mask: list):
"""Tries to match the rule given the mask.
Returns True iff matched.
Each element in |mask| is either boolean or list.
If it is False - this position was not consumed yet.
If it is True - this position was consumend, but does not provide output of its own.
If it is a list - the list contains all possible output strings starting at this position.
"""
matched = False
pos = 0
while True:
pos = s.find(self.match_str, pos)
if pos == -1:
break
if not self.CheckMaskAtPos(mask, pos):
pos += 1
else:
matched = True
appended_output = False
for i, consume in enumerate(self.consume_mask):
if consume:
if not appended_output:
mask[pos+i] = (self, self.replace_rules)
appended_output = True
else:
mask[pos+i] = True
return matched
def CheckMaskAtPos(self, mask : list, pos : int):
for i, consume in enumerate(self.consume_mask):
# mask[pos+i] can be a bool or a list.
if consume and mask[pos+i] is not False:
return False
return True
def __repr__(self):
bitmask = []
for b in self.consume_mask:
bitmask.append("T" if b else "F")
return "{self.match_str}/{self.rule_line_num}/{bitmask}".format(self=self, bitmask="".join(bitmask))
class IpaMapper(object):
def __init__(self, mapping_filename):
self.defined_vars = {}
self.rules = []
self.LoadMapping(mapping_filename)
def LoadMapping(self, mapping_filename):
for line_num, line in enumerate(open(mapping_filename)):
line_num += 1
try:
# Strip the comments and whitespace
pos = line.find("#")
if pos != -1:
line = line[:pos]
line = line.strip()
if len(line) == 0:
continue
# Parse matcher set definitions
if line.startswith("def "):
self.ParseDef(line[4:])
continue
match_rule, replace_rules = line.split(" ||| ")
self.AppendRules(line_num, match_rule, replace_rules.split(" "))
except:
print("Error in rules file line:", line_num)
raise
def AppendRules(self, line_num, match_rule, replace_rules):
replace_rules = [x.replace(EMPTY_CHAR, "") for x in replace_rules]
rule_list = []
consume_mask = []
while len(match_rule) > 0:
if match_rule[0] == "{":
pos = match_rule.find("}")
assert pos>0, (match_rule, pos)
var_name = match_rule[1:pos]
rule_list.append(self.defined_vars[var_name])
consume_mask.append(False)
match_rule = match_rule[pos+1:]
else:
rule_list.append([match_rule[0]])
consume_mask.append(True)
match_rule = match_rule[1:]
for p_tuple in itertools.product(*rule_list):
rule_str = "".join(p_tuple)
rule = IpaRule(line_num, rule_str, consume_mask, replace_rules)
self.CheckRules(rule)
self.rules.append(rule)
def CheckRules(self, rule_to_check):
s = rule_to_check.match_str
for prev_rule in self.rules:
mask = [False] * len(s)
if prev_rule.TryMatch(s, mask):
print("Error: rule {} is consumed by earlier rule {}".format(rule_to_check, prev_rule))
def ParseDef(self, line):
name, set_str = line.split("=")
name = name.strip()
set_str = set_str.strip()
self.defined_vars[name] = list(set_str)
print("Defined rule name:", name, "values:", set_str)
def ConvertWord(self, s, debug_match=False, ignore_errors=False):
"""Given a word (single words only) returns a list of all pronunciations (no space between phones)."""
assert("$" not in s)
assert("^" not in s)
s = "^" + s + "$"
mask = [False]*len(s)
for rule in self.rules:
# TryMatch updates the mask.
while rule.TryMatch(s, mask):
pass
# Mark start and end chars as consumed.
mask[0] = True
mask[-1] = True
if False in mask and not ignore_errors:
print("No rules matched for: {}, mask: {}".format(s, mask))
out_str_lists = []
for elem in mask:
if type(elem) == tuple:
out_str_lists.append(elem[1])
out_strs = []
for p_tuple in itertools.product(*out_str_lists):
if len(p_tuple) > 0:
out_strs.append("".join(p_tuple))
if debug_match:
print("Matching word:", s)
print(mask)
print("Result:", out_strs)
return out_strs
def IpaSplit(word, ignore_errors=False):
result = []
for i, c in enumerate(word):
category = unicodedata.category(c)
if category == "Mn":
assert len(result) > 0, "Unexpected accent before letter for char: {} in word: {}, len(word): {}".format(c, word, len(word))
result[-1] = result[-1] + c
else:
if category not in {"Ll", "Lu", "Lm", "Pd", "Lo"}:
assert ignore_errors, "Unexpected unicode category for char: {} in word: {}".format(c, word)
result.append(c)
return result
def main():
ipa_mapper = IpaMapper(args.mapping_file)
phone_dict = {}
for line in open(args.in_vocab):
if " ||| " in line:
surface, word = line.strip().split(" ||| ")
else:
# Only take the first token from each line.
word = line.split()[0]
surface = word
if word not in phone_dict:
out_pron_list = ipa_mapper.ConvertWord(word, debug_match=args.debug_match, ignore_errors=args.ignore_errors)
if out_pron_list:
phone_dict[surface] = set(out_pron_list)
with open(args.out_pron_dict, "w") as out_file:
for word in sorted(phone_dict):
for pron in sorted(phone_dict[word]):
out_file.write("{} ||| {}\n".format(word, " ".join(IpaSplit(pron, ignore_errors=False))))
if __name__ == '__main__':
main()