-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmudict.py
47 lines (33 loc) · 1.24 KB
/
cmudict.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
# -*- coding: utf-8 -*-
import codecs
from collections import defaultdict
from collections import Counter
class CMUDict(object):
def __init__(self, filename):
# load CMU dict
with codecs.open(filename, encoding='utf-8') as fin:
lines = (line for line in fin if line.strip() and not line.startswith(';;;'))
lines = (line.strip().lower().split(maxsplit=1) for line in lines)
# merge phonemes for the same word
self.data = defaultdict(list)
for word, phone in lines:
if word.endswith(('(1)', '(2)', '(3)')):
original_word = word[:-3]
self.data[original_word].append(phone)
else:
self.data[word].append(phone)
def words(self):
return self.data.keys()
def symbols(self):
freqs = Counter()
for _, phones_list in self.data.items():
for phones in phones_list:
freqs.update(phones.split())
return freqs
def __getitem__(self, item):
return self.data[item]
def align(self):
"""one-to-one alignment"""
# multiple letters map to one phoneme
# one letter map to multiple phonemes
pass