-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-dict
executable file
·117 lines (99 loc) · 3.33 KB
/
generate-dict
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
#!/usr/bin/python
import os, sys
import subprocess
from voxutils.dictionaries import lookup_words
PHONE_MAP_DIR = "phone-maps"
DICT_DIR = "dict"
def vocab_gen(fn):
f = open(fn)
for line in f:
for word in line.split():
yield word.upper()
f.close()
def load_conversion(conversion):
d = {}
f = open(os.path.join(PHONE_MAP_DIR, conversion))
for line in f:
line = line.strip()
if line[:3] == "###" or line == '':
continue
try:
native, arpa = line.split(None, 1)
except ValueError:
native = line
arpa = ''
if native == '+SPACE+':
native = ' '
d[native.decode("utf-8")] = arpa
tree = {}
for token, arpa in d.iteritems():
current = tree
for c in token:
current = current.setdefault(c, {})
current[''] = arpa
return tree
def raw_espeak(word):
call = ["espeak", "-q", "--ipa", word.encode('utf-8')]
#call = ["espeak", "-qx", word]
return subprocess.check_output(call).strip()
def dict_espeak(vocab):
convertor = load_conversion("ipa-to-cmudict")
for word in vocab:
word = word.decode("utf-8")
if word[:2] == '++': # '++UM++' -> '+UM+', etc special cases
print ("%s %s" % (word, word[1:-1])).encode('utf-8')
continue
pron = raw_espeak(word).decode("utf-8")
arpa = []
pos = 0
try:
while pos < len(pron):
d = convertor
while pos < len(pron) and pron[pos] in d:
d = d[pron[pos]]
pos += 1
if d.get('') is None:
raise TabError("missing phoneme at %s" % pos)
arpa.append(d[''])
print ("%s %s" % (word, ' '.join(arpa))).encode('utf-8')
except (UnicodeDecodeError, TabError), e:
print >> sys.stderr, e, word, pron, arpa
def dict_espeak(vocab):
convertor = load_conversion("ipa-to-cmudict")
for word in vocab:
word = word.decode("utf-8")
if word[:2] == '++': # '++UM++' -> '+UM+', etc special cases
print ("%s %s" % (word, word[1:-1])).encode('utf-8')
continue
pron = raw_espeak(word).decode("utf-8")
arpa = []
pos = 0
try:
while pos < len(pron):
d = convertor
while pos < len(pron) and pron[pos] in d:
d = d[pron[pos]]
pos += 1
if d.get('') is None:
raise TabError("missing phoneme at %s" % pos)
arpa.append(d[''])
print ("%s %s" % (word, ' '.join(arpa))).encode('utf-8')
except (UnicodeDecodeError, TabError), e:
print >> sys.stderr, e, word, pron, arpa
def lut_dict_factory(dictname):
def f(vocab):
lines, missing = lookup_words(vocab, dictname)
for line in lines:
print line.upper()
for line in missing:
print >> sys.stderr, line
return f
dict_cmu = lut_dict_factory('cmudict')
def main(dictname, vocab_files):
vocab = set()
for fn in vocab_files:
vocab.update(vocab_gen(fn))
f = globals().get("dict_" + dictname,
lut_dict_factory(dictname))
f(vocab)
main(sys.argv[1], sys.argv[2:])