/
predict_length.py
190 lines (143 loc) · 5.84 KB
/
predict_length.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
import logging
from pathlib import Path
from pprint import pformat
from typing import Tuple, Optional, List
from keras.models import Model, load_model
from keras.layers import GRU, Dense, Input
from nltk.corpus import swadesh
import numpy as np
from sklearn.model_selection import train_test_split
import util
class LengthModelWrapper(util.ModelWrapper):
def __init__(self, model: Model, pronunciations, letter_ids):
self.model = model
self.pronunciations = pronunciations
self.letter_ids = letter_ids
max_len = util.get_max_length(pronunciations.values())
self.max_len = max_len
def predict(self, words, round_preds=False):
"""
Accepts an iterable of words,
converts them to one-hot letter ID representations,
and returns an array of predicted lengths.
:param words: sequence, iterable
An iterable of strings whose phonemic lengths should be predicted.
:param round_preds: (optional) bool
If True, predictions will be rounded to the nearest integer.
:return: array[float]
An array of predicted phonemic length as floating points
"""
xs = words_to_ids(words, self.letter_ids, self.max_len)
preds = self.model.predict(xs)
return preds
def words_to_ids(words, letter_ids, maxlen):
X = np.zeros((len(words), maxlen, len(letter_ids)),
dtype=bool)
for n, word in enumerate(words):
for i, letter in enumerate(word):
letter_id = letter_ids[letter]
X[n,i,letter_id] = True
return X
def prepare_data(pronunciations, letter_ids):
logging.info('preparing the data...')
max_length = util.get_max_length(pronunciations)
X = np.zeros((len(pronunciations), max_length, len(letter_ids)),
dtype=bool)
y = np.zeros(len(pronunciations), dtype=int)
for n, word in enumerate(pronunciations):
for i, letter in enumerate(word):
letter_id = letter_ids[letter]
X[n, i, letter_id] = True
y[n] = len(pronunciations[word])
return X, y
def train_model(X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33,
random_state=21)
word_input = Input(shape=(X.shape[1:]))
x = GRU(256, input_shape=(X.shape[1:]), return_sequences=True)(word_input)
x = GRU(256)(x)
predictions = Dense(1)(x)
model = Model(input=word_input, output=predictions)
model.compile(optimizer='rmsprop', loss='mse',
metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=4, nb_epoch=3,
validation_data=(X_test, y_test))
return model
def get_model(fp, X=None, y=None) -> Model:
logging.info('getting the model...')
if Path(fp).exists():
model = load_model(fp)
else:
if X is None or y is None:
raise ValueError("You must provide training data if model doesn't exist")
model = train_model(X, y)
model.save(fp)
return model
def test_accuracy(words, model_wrapper: LengthModelWrapper) -> Tuple[float, List]:
correct = 0
errors = []
preds = model_wrapper.predict(words)
pronunciations = model_wrapper.pronunciations
for word, pred in zip(words, preds):
prediction = pred[0]
predicted_length = round(prediction)
real_length = len(pronunciations[word])
if predicted_length == real_length:
correct += 1
else:
errors.append((word, prediction, real_length))
accuracy = correct / len(words)
return accuracy, errors
def test_swadesh(model, lang) -> Tuple[Optional[float], Optional[List]]:
swadesh_langs = set(swadesh.fileids())
if lang in swadesh_langs:
logging.info('Testing model on Swadesh list for {}...'.format(lang))
# some entries in the swadesh list have multiple words
# because they include contextual definitions
# so we need to only take the first word
words = swadesh.words(fileids=lang)
words = [word.split()[0].casefold() for word in words]
accuracy, errors = test_accuracy(words, model)
else:
logging.error('No Swadesh corpus for "{}"'.format(lang))
accuracy = None
errors = None
return accuracy, errors
def get_wrapped_model(data_path=None):
"""
Loads or builds a model, then wraps it in a ``LengthModelWrapper``.
:param data_path: str, Path
The path to the JSON file with words mapped to pronunciations.
:return: LengthModelWrapper
Model wrapped in a LengthModelWrapper.
"""
if not data_path:
data_path = Path('pronunciations_en.json')
prons = util.load_data(data_path)
letter_ids = util.make_sequence_ids(prons)
# phoneme_ids = util.make_sequence_ids(prons.values())
X, y = prepare_data(prons, letter_ids)
model = get_model('model_lengths.h5', X, y)
model_wrapper = LengthModelWrapper(model, prons, letter_ids)
return model_wrapper
def main():
data_path = Path('pronunciations_en.json')
logging.basicConfig(level=logging.INFO)
model_wrapper = get_wrapped_model(data_path)
lang = data_path.stem.rsplit('_')[-1]
accuracy, errors = test_swadesh(model_wrapper, lang)
logging.info('Swadesh accuracy: {:.2%}'.format(accuracy))
logging.info('errors:\n{}'.format(pformat(errors)))
pronunciations = model_wrapper.pronunciations
while True:
word = input('> ')
pred = model_wrapper.predict([word])[0][0]
rounded_pred = round(pred)
print('predicted length: {} ({})'.format(rounded_pred, pred))
try:
real_length = len(pronunciations[word])
print('real length: {}'.format(real_length))
except KeyError:
print('"{}" is not the dictionary'.format(word))
if __name__ == '__main__':
main()