error-align 0.1.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- error_align/__init__.py +7 -0
- error_align/backtrace_graph.py +258 -0
- error_align/baselines/__init__.py +10 -0
- error_align/baselines/optimal_word_alignment.py +111 -0
- error_align/baselines/power/normalize/__init__.py +3 -0
- error_align/baselines/power/normalize/contractions.py +152 -0
- error_align/baselines/power/normalize/normalize.py +295 -0
- error_align/baselines/power/normalize/numbers.py +289 -0
- error_align/baselines/power/power/__init__.py +3 -0
- error_align/baselines/power/power/aligner.py +467 -0
- error_align/baselines/power/power/levenshtein.py +576 -0
- error_align/baselines/power/power/phonemes.py +16 -0
- error_align/baselines/power/power/pronounce.py +85 -0
- error_align/baselines/power/power/punct.py +166 -0
- error_align/baselines/power/power/readers.py +48 -0
- error_align/baselines/power/power/writers.py +259 -0
- error_align/baselines/power_alignment.py +61 -0
- error_align/baselines/rapidfuzz_word_alignment.py +100 -0
- error_align/baselines/utils.py +93 -0
- error_align/edit_distance.py +144 -0
- error_align/error_align.py +495 -0
- error_align/func.py +38 -0
- error_align/utils.py +166 -0
- error_align-0.1.0b1.dist-info/METADATA +161 -0
- error_align-0.1.0b1.dist-info/RECORD +26 -0
- error_align-0.1.0b1.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
from __future__ import division
|
|
2
|
+
|
|
3
|
+
from error_align.baselines.power.power.levenshtein import AlignLabels, ExpandedAlignment, Levenshtein
|
|
4
|
+
from error_align.baselines.power.power.phonemes import Phonemes
|
|
5
|
+
from error_align.baselines.power.power.pronounce import PronouncerBase, PronouncerLex, PronouncerType
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TokType:
|
|
9
|
+
WordBoundary = 1
|
|
10
|
+
SyllableBoundary = 2
|
|
11
|
+
Phoneme = 3
|
|
12
|
+
Empty = 0
|
|
13
|
+
|
|
14
|
+
@staticmethod
|
|
15
|
+
def checkAnnotation(tok):
|
|
16
|
+
tt = TokType.Phoneme
|
|
17
|
+
if not tok:
|
|
18
|
+
tt = TokType.Empty
|
|
19
|
+
elif tok == '|':
|
|
20
|
+
tt = TokType.WordBoundary
|
|
21
|
+
elif tok == '#':
|
|
22
|
+
tt = TokType.SyllableBoundary
|
|
23
|
+
return tt
|
|
24
|
+
|
|
25
|
+
class CharToWordAligner:
|
|
26
|
+
def __init__(self, ref, hyp, lowercase=False):
|
|
27
|
+
self.refwords = ref.strip()
|
|
28
|
+
self.hypwords = hyp.strip()
|
|
29
|
+
self.ref = ref
|
|
30
|
+
self.hyp = hyp
|
|
31
|
+
self.lowercase = lowercase
|
|
32
|
+
self.char_align = None
|
|
33
|
+
self.word_align = None
|
|
34
|
+
|
|
35
|
+
def charAlign(self):
|
|
36
|
+
ref_chars = [x for x in self.ref] + [' ']
|
|
37
|
+
hyp_chars = [x for x in self.hyp] + [' ']
|
|
38
|
+
|
|
39
|
+
lev = Levenshtein.align(ref_chars, hyp_chars, lowercase=self.lowercase, reserve_list=set([' ']))
|
|
40
|
+
lev.editops()
|
|
41
|
+
self.char_align = lev.expandAlign()
|
|
42
|
+
return self.char_align
|
|
43
|
+
|
|
44
|
+
def charAlignToWordAlign(self):
|
|
45
|
+
if not self.char_align:
|
|
46
|
+
raise Exception("char_align is None")
|
|
47
|
+
|
|
48
|
+
ref_word_align = []
|
|
49
|
+
hyp_word_align = []
|
|
50
|
+
align_word = []
|
|
51
|
+
|
|
52
|
+
tmp_ref_word = []
|
|
53
|
+
tmp_hyp_word = []
|
|
54
|
+
|
|
55
|
+
for i in range(len(self.char_align.align)):
|
|
56
|
+
ref_char = self.char_align.s1[i]
|
|
57
|
+
hyp_char = self.char_align.s2[i]
|
|
58
|
+
align_char = self.char_align.align[i]
|
|
59
|
+
|
|
60
|
+
# check if both words are completed
|
|
61
|
+
# There are a few of ways this could happen:
|
|
62
|
+
if ((align_char == AlignLabels.correct and ref_char == ' ') or
|
|
63
|
+
(align_char == AlignLabels.deletion and ref_char == ' ') or
|
|
64
|
+
(align_char == AlignLabels.insertion and hyp_char == ' ')):
|
|
65
|
+
|
|
66
|
+
ref_word = ''.join(tmp_ref_word)
|
|
67
|
+
hyp_word = ''.join(tmp_hyp_word)
|
|
68
|
+
|
|
69
|
+
if ref_word or hyp_word:
|
|
70
|
+
ref_word_align.append(ref_word)
|
|
71
|
+
hyp_word_align.append(hyp_word)
|
|
72
|
+
tmp_ref_word = []
|
|
73
|
+
tmp_hyp_word = []
|
|
74
|
+
|
|
75
|
+
# Check align type
|
|
76
|
+
if ref_word and hyp_word:
|
|
77
|
+
if ref_word == hyp_word:
|
|
78
|
+
align_word.append(AlignLabels.correct)
|
|
79
|
+
else:
|
|
80
|
+
align_word.append(AlignLabels.substitution)
|
|
81
|
+
elif ref_word:
|
|
82
|
+
align_word.append(AlignLabels.deletion)
|
|
83
|
+
else:
|
|
84
|
+
align_word.append(AlignLabels.insertion)
|
|
85
|
+
continue
|
|
86
|
+
|
|
87
|
+
# Read current chars and check if one of the words is complete
|
|
88
|
+
if ref_char == ' ':
|
|
89
|
+
if len(tmp_ref_word) > 1:
|
|
90
|
+
# Probably a D
|
|
91
|
+
ref_word = ''.join(tmp_ref_word)
|
|
92
|
+
ref_word_align.append(ref_word)
|
|
93
|
+
hyp_word_align.append('')
|
|
94
|
+
tmp_ref_word = []
|
|
95
|
+
align_word.append(AlignLabels.deletion)
|
|
96
|
+
else:
|
|
97
|
+
tmp_ref_word.append(ref_char)
|
|
98
|
+
|
|
99
|
+
if hyp_char == ' ':
|
|
100
|
+
if len(tmp_hyp_word) > 1:
|
|
101
|
+
# Probably an I
|
|
102
|
+
hyp_word = ''.join(tmp_hyp_word)
|
|
103
|
+
ref_word_align.append('')
|
|
104
|
+
hyp_word_align.append(hyp_word)
|
|
105
|
+
tmp_hyp_word = []
|
|
106
|
+
align_word.append(AlignLabels.insertion)
|
|
107
|
+
else:
|
|
108
|
+
tmp_hyp_word.append(hyp_char)
|
|
109
|
+
|
|
110
|
+
self.word_align = ExpandedAlignment(ref_word_align, hyp_word_align, align_word, lowercase=self.lowercase)
|
|
111
|
+
return self.word_align
|
|
112
|
+
|
|
113
|
+
class PowerAligner:
|
|
114
|
+
# Exclusive tokens that can only align to themselves; not other members in this set.
|
|
115
|
+
reserve_list = set(['|', '#'])
|
|
116
|
+
|
|
117
|
+
# R-sounds
|
|
118
|
+
r_set = set.union(set('r'), Phonemes.r_vowels)
|
|
119
|
+
exclusive_sets = [Phonemes.vowels, Phonemes.consonants, r_set]
|
|
120
|
+
|
|
121
|
+
phoneDistPenalty = 0.25
|
|
122
|
+
phoneDistPenalt16ySet = set(['|'])
|
|
123
|
+
|
|
124
|
+
def __init__(self, ref, hyp, lowercase=False, verbose=False,
|
|
125
|
+
pronounce_type=PronouncerType.Lexicon,
|
|
126
|
+
lexicon=None,
|
|
127
|
+
word_align_weights=Levenshtein.wordAlignWeights):
|
|
128
|
+
if not ref:
|
|
129
|
+
raise Exception("No reference file.\nref: {0}\nhyp: {1}".format(ref, hyp))
|
|
130
|
+
|
|
131
|
+
if pronounce_type == PronouncerType.Lexicon:
|
|
132
|
+
self.pronouncer = PronouncerLex(lexicon)
|
|
133
|
+
else:
|
|
134
|
+
self.pronouncer = PronouncerBase()
|
|
135
|
+
|
|
136
|
+
self.ref = [x for x in ref.strip().split() if x]
|
|
137
|
+
self.hyp = [x for x in hyp.strip().split() if x]
|
|
138
|
+
self.refwords = ' '.join(self.ref)
|
|
139
|
+
self.hypwords = ' '.join(self.hyp)
|
|
140
|
+
|
|
141
|
+
self.lowercase = lowercase
|
|
142
|
+
self.verbose = verbose
|
|
143
|
+
|
|
144
|
+
# Perform word alignment
|
|
145
|
+
lev = Levenshtein.align(self.ref, self.hyp, lowercase=self.lowercase, weights=word_align_weights)
|
|
146
|
+
lev.editops()
|
|
147
|
+
self.wer_alignment = lev.expandAlign()
|
|
148
|
+
self.wer, self.wer_components = self.wer_alignment.error_rate()
|
|
149
|
+
|
|
150
|
+
# Used for POWER alignment
|
|
151
|
+
self.power_alignment = None
|
|
152
|
+
self.power = None
|
|
153
|
+
self.power_components = None
|
|
154
|
+
|
|
155
|
+
# Used to find potential error regions
|
|
156
|
+
self.split_regions = None
|
|
157
|
+
self.error_indexes = None
|
|
158
|
+
self.phonetic_alignments = None
|
|
159
|
+
self.phonetic_lev = None
|
|
160
|
+
|
|
161
|
+
def align(self):
|
|
162
|
+
# Find the error regions that may need to be realigned
|
|
163
|
+
self.split_regions, self.error_indexes = self.wer_alignment.split_error_regions()
|
|
164
|
+
self.phonetic_alignments = [None] * len(self.split_regions)
|
|
165
|
+
|
|
166
|
+
for error_index in self.error_indexes:
|
|
167
|
+
seg = self.split_regions[error_index]
|
|
168
|
+
ref_words = seg.s1_tokens()
|
|
169
|
+
hyp_words = seg.s2_tokens()
|
|
170
|
+
ref_phones = self.pronouncer.pronounce(ref_words)
|
|
171
|
+
hyp_phones = self.pronouncer.pronounce(hyp_words)
|
|
172
|
+
|
|
173
|
+
power_seg_alignment, self.phonetic_alignments[error_index] = PowerAligner.phoneAlignToWordAlign(ref_words, hyp_words,
|
|
174
|
+
ref_phones, hyp_phones)
|
|
175
|
+
|
|
176
|
+
# Replace the error region at the current index.
|
|
177
|
+
self.split_regions[error_index] = power_seg_alignment
|
|
178
|
+
|
|
179
|
+
# Merge the alignment segments back together.
|
|
180
|
+
self.power_alignment = ExpandedAlignment(self.split_regions[0].s1, self.split_regions[0].s2,
|
|
181
|
+
self.split_regions[0].align,
|
|
182
|
+
self.split_regions[0].s1_map, self.split_regions[0].s2_map, lowercase=self.lowercase)
|
|
183
|
+
for i in range(1, len(self.split_regions)):
|
|
184
|
+
self.power_alignment.append_alignment(self.split_regions[i])
|
|
185
|
+
|
|
186
|
+
# Get the alignment score
|
|
187
|
+
self.power, self.power_components = self.power_alignment.error_rate()
|
|
188
|
+
|
|
189
|
+
assert self.hypwords == self.power_alignment.s2_string(), "hyp mismatch:\n{0}\n{1}".format(self.hypwords, self.power_alignment.s2_string())
|
|
190
|
+
assert self.refwords == self.power_alignment.s1_string(), "ref mismatch:\n{0}\n{1}".format(self.refwords, self.power_alignment.s1_string())
|
|
191
|
+
|
|
192
|
+
# TODO: Make this simpler (and maybe recursive)
|
|
193
|
+
@classmethod
|
|
194
|
+
def phoneAlignToWordAlign(cls, ref_words, hyp_words, ref_phones, hyp_phones, break_on_syllables=True):
|
|
195
|
+
ref_word_span = (0, len(ref_words))
|
|
196
|
+
hyp_word_span = (0, len(hyp_words))
|
|
197
|
+
|
|
198
|
+
# Perform Levenshtein Alignment
|
|
199
|
+
lev = Levenshtein.align(ref=ref_phones,
|
|
200
|
+
hyp=hyp_phones,
|
|
201
|
+
reserve_list=PowerAligner.reserve_list,
|
|
202
|
+
exclusive_sets=PowerAligner.exclusive_sets,
|
|
203
|
+
weights=Levenshtein.wordAlignWeights) #,
|
|
204
|
+
#dist_penalty=PowerAligner.phoneDistPenalty, dist_penalty_set=Levenshtein.wordAlignWeights)
|
|
205
|
+
phone_align = lev.expandAlignCompact()
|
|
206
|
+
|
|
207
|
+
worklist = list()
|
|
208
|
+
worklist.append((ref_word_span, hyp_word_span, phone_align))
|
|
209
|
+
|
|
210
|
+
full_reference = list()
|
|
211
|
+
full_hypothesis = list()
|
|
212
|
+
full_alignment = list()
|
|
213
|
+
full_phone_align = list()
|
|
214
|
+
|
|
215
|
+
while worklist:
|
|
216
|
+
# Take the next set of sequence boundaries off the worklist
|
|
217
|
+
ref_word_span, hyp_word_span, phone_align = worklist.pop()
|
|
218
|
+
ref_word_index, ref_word_limit = ref_word_span
|
|
219
|
+
hyp_word_index, hyp_word_limit = hyp_word_span
|
|
220
|
+
|
|
221
|
+
# TODO: Currently only checking in the forward direction
|
|
222
|
+
ref_word_builder = [] # Temp storage of words in alignment span
|
|
223
|
+
hyp_word_builder = []
|
|
224
|
+
|
|
225
|
+
ref_word_iter = enumerate(ref_words[ref_word_span[0]:ref_word_span[1]]) # Iterates through the surface words
|
|
226
|
+
hyp_word_iter = enumerate(hyp_words[hyp_word_span[0]:hyp_word_span[1]])
|
|
227
|
+
|
|
228
|
+
ref_aligned = [] # Finalized alignments
|
|
229
|
+
hyp_aligned = []
|
|
230
|
+
alignment = [] # Finalized alignment labels
|
|
231
|
+
|
|
232
|
+
ref_extra_syllable_word_index = None # Used for marking words mapping to extra syllables in alignment.
|
|
233
|
+
hyp_extra_syllable_word_index = None
|
|
234
|
+
ref_syllable_count = 0
|
|
235
|
+
hyp_syllable_count = 0
|
|
236
|
+
|
|
237
|
+
ref_word_started = False # Indicates whether a word is already accounted for in the alignment when a phoneme is reached.
|
|
238
|
+
hyp_word_started = False
|
|
239
|
+
|
|
240
|
+
advance_worklist = False
|
|
241
|
+
commit_alignment = False
|
|
242
|
+
|
|
243
|
+
for i in range(len(phone_align.align)):
|
|
244
|
+
ref_type = TokType.checkAnnotation(phone_align.s1[i])
|
|
245
|
+
hyp_type = TokType.checkAnnotation(phone_align.s2[i])
|
|
246
|
+
|
|
247
|
+
# Check if word boundaries are reached, both on ref an hyp -- or the case where no more symbols can be read.
|
|
248
|
+
if (i == len(phone_align.align) - 1) or (ref_type == TokType.WordBoundary and ref_type == hyp_type):
|
|
249
|
+
align_tok = None
|
|
250
|
+
# Only write outputs if either the ref or the hyp has scanned some words.
|
|
251
|
+
if ref_word_builder:
|
|
252
|
+
if hyp_word_builder:
|
|
253
|
+
align_tok = AlignLabels.substitution if ref_word_builder != hyp_word_builder else AlignLabels.correct
|
|
254
|
+
else:
|
|
255
|
+
align_tok = AlignLabels.deletion
|
|
256
|
+
elif hyp_word_builder:
|
|
257
|
+
align_tok = AlignLabels.insertion
|
|
258
|
+
|
|
259
|
+
if align_tok:
|
|
260
|
+
# Add the remainder to the worklist
|
|
261
|
+
ref_word_span_next = (ref_word_index + len(ref_word_builder), ref_word_limit)
|
|
262
|
+
hyp_word_span_next = (hyp_word_index + len(hyp_word_builder), hyp_word_limit)
|
|
263
|
+
phone_align_next = phone_align.subsequence(i, phone_align.length(), preserve_index=False)
|
|
264
|
+
worklist.append((ref_word_span_next, hyp_word_span_next, phone_align_next))
|
|
265
|
+
|
|
266
|
+
# "Commit" the current alignment
|
|
267
|
+
if align_tok in (AlignLabels.correct, AlignLabels.substitution):
|
|
268
|
+
alignment.append(align_tok)
|
|
269
|
+
|
|
270
|
+
# Check for syllable conflicts
|
|
271
|
+
if not break_on_syllables or not ref_extra_syllable_word_index:
|
|
272
|
+
ref_aligned.append(' '.join(ref_word_builder))
|
|
273
|
+
ref_syllable_count = 0
|
|
274
|
+
hyp_syllable_count = 0
|
|
275
|
+
else:
|
|
276
|
+
ref_aligned.append(' '.join(ref_word_builder[0:ref_extra_syllable_word_index]))
|
|
277
|
+
# The remaining words are deletions
|
|
278
|
+
for word in ref_word_builder[ref_extra_syllable_word_index:]:
|
|
279
|
+
alignment.append(AlignLabels.deletion)
|
|
280
|
+
ref_aligned.append(word)
|
|
281
|
+
hyp_aligned.append('')
|
|
282
|
+
ref_syllable_count = 0
|
|
283
|
+
|
|
284
|
+
if not break_on_syllables or not hyp_extra_syllable_word_index:
|
|
285
|
+
hyp_aligned.append(' '.join(hyp_word_builder))
|
|
286
|
+
ref_syllable_count = 0
|
|
287
|
+
hyp_syllable_count = 0
|
|
288
|
+
else:
|
|
289
|
+
hyp_aligned.append(' '.join(hyp_word_builder[0:hyp_extra_syllable_word_index]))
|
|
290
|
+
# The remaining words are insertions
|
|
291
|
+
for word in hyp_word_builder[hyp_extra_syllable_word_index:]:
|
|
292
|
+
alignment.append(AlignLabels.insertion)
|
|
293
|
+
ref_aligned.append('')
|
|
294
|
+
hyp_aligned.append(word)
|
|
295
|
+
hyp_syllable_count = 0
|
|
296
|
+
|
|
297
|
+
if align_tok == AlignLabels.substitution:
|
|
298
|
+
# Check if you need to rework this alignment.
|
|
299
|
+
if len(ref_word_builder) != len(hyp_word_builder):
|
|
300
|
+
# Word count mismatch in the alignment span. Is there a possibility that we need to re-align this segment?
|
|
301
|
+
ref_word_span_curr = (ref_word_index, ref_word_index + len(ref_word_builder))
|
|
302
|
+
hyp_word_span_curr = (hyp_word_index, hyp_word_index + len(hyp_word_builder))
|
|
303
|
+
phone_align_curr = phone_align.subsequence(0, i+1, preserve_index=False)
|
|
304
|
+
|
|
305
|
+
lev = Levenshtein.align(
|
|
306
|
+
ref=phone_align_curr.s1_tokens(),
|
|
307
|
+
hyp=phone_align_curr.s2_tokens(),
|
|
308
|
+
reserve_list=PowerAligner.reserve_list,
|
|
309
|
+
exclusive_sets=PowerAligner.exclusive_sets,
|
|
310
|
+
weights=Levenshtein.wordAlignWeights) #,
|
|
311
|
+
#dist_penalty=PowerAligner.phoneDistPenalty, dist_penalty_set=Levenshtein.wordAlignWeights)
|
|
312
|
+
|
|
313
|
+
phone_align_adjusted = lev.expandAlignCompact()
|
|
314
|
+
|
|
315
|
+
if phone_align_curr.align != phone_align_adjusted.align:
|
|
316
|
+
# Looks like we need to redo the phone-to-word alignment.
|
|
317
|
+
worklist.append((ref_word_span_curr, hyp_word_span_curr, phone_align_adjusted))
|
|
318
|
+
else:
|
|
319
|
+
commit_alignment = True
|
|
320
|
+
else:
|
|
321
|
+
commit_alignment = True
|
|
322
|
+
|
|
323
|
+
elif align_tok == AlignLabels.deletion:
|
|
324
|
+
for word in ref_word_builder:
|
|
325
|
+
alignment.append(align_tok)
|
|
326
|
+
ref_aligned.append(word)
|
|
327
|
+
hyp_aligned.append('')
|
|
328
|
+
|
|
329
|
+
commit_alignment = True
|
|
330
|
+
ref_syllable_count = 0
|
|
331
|
+
|
|
332
|
+
elif align_tok == AlignLabels.insertion:
|
|
333
|
+
for word in hyp_word_builder:
|
|
334
|
+
alignment.append(align_tok)
|
|
335
|
+
ref_aligned.append('')
|
|
336
|
+
hyp_aligned.append(word)
|
|
337
|
+
|
|
338
|
+
commit_alignment = True
|
|
339
|
+
hyp_syllable_count = 0
|
|
340
|
+
|
|
341
|
+
if commit_alignment:
|
|
342
|
+
# Commit the alignment.
|
|
343
|
+
full_reference.extend(ref_aligned)
|
|
344
|
+
full_hypothesis.extend(hyp_aligned)
|
|
345
|
+
full_alignment.extend(alignment)
|
|
346
|
+
full_phone_align.append(phone_align.subsequence(0, i, preserve_index=False))
|
|
347
|
+
ref_aligned = []
|
|
348
|
+
hyp_aligned = []
|
|
349
|
+
alignment = []
|
|
350
|
+
break
|
|
351
|
+
|
|
352
|
+
# Add words if word boundaries are reached.
|
|
353
|
+
else:
|
|
354
|
+
if ref_type == TokType.WordBoundary:
|
|
355
|
+
ref_word_started = False
|
|
356
|
+
if hyp_type != TokType.WordBoundary and ref_word_builder and not hyp_word_builder:
|
|
357
|
+
# DELETION
|
|
358
|
+
# Ref word ended, but no hyp words have been added. Mark the current ref word(s) in the span as deletion errors.
|
|
359
|
+
# TODO: Dedupe this logic
|
|
360
|
+
for word in ref_word_builder:
|
|
361
|
+
alignment.append(AlignLabels.deletion)
|
|
362
|
+
ref_aligned.append(word)
|
|
363
|
+
hyp_aligned.append('')
|
|
364
|
+
ref_syllable_count = 0
|
|
365
|
+
|
|
366
|
+
# Commit the alignment.
|
|
367
|
+
full_reference.extend(ref_aligned)
|
|
368
|
+
full_hypothesis.extend(hyp_aligned)
|
|
369
|
+
full_alignment.extend(alignment)
|
|
370
|
+
full_phone_align.append(phone_align.subsequence(0, i, preserve_index=False))
|
|
371
|
+
|
|
372
|
+
# Add the remainder to the worklist
|
|
373
|
+
ref_word_span_next = (ref_word_index + len(ref_word_builder), ref_word_limit)
|
|
374
|
+
hyp_word_span_next = (hyp_word_index + len(hyp_word_builder), hyp_word_limit)
|
|
375
|
+
lev = Levenshtein.align(
|
|
376
|
+
ref=[x for x in phone_align.s1[i:] if x],
|
|
377
|
+
hyp=[x for x in phone_align.s2 if x],
|
|
378
|
+
reserve_list=PowerAligner.reserve_list,
|
|
379
|
+
exclusive_sets=PowerAligner.exclusive_sets,
|
|
380
|
+
weights=Levenshtein.wordAlignWeights) #,
|
|
381
|
+
#dist_penalty=PowerAligner.phoneDistPenalty, dist_penalty_set=Levenshtein.wordAlignWeights)
|
|
382
|
+
phone_align_next = lev.expandAlignCompact()
|
|
383
|
+
|
|
384
|
+
worklist.append((ref_word_span_next, hyp_word_span_next, phone_align_next))
|
|
385
|
+
break
|
|
386
|
+
elif ref_type == TokType.Phoneme and not ref_word_started:
|
|
387
|
+
ref_word_started = True
|
|
388
|
+
try:
|
|
389
|
+
ref_word_item = ref_word_iter.__next__()
|
|
390
|
+
ref_word_builder.append(ref_word_item[1])
|
|
391
|
+
except StopIteration:
|
|
392
|
+
pass
|
|
393
|
+
|
|
394
|
+
if hyp_type == TokType.WordBoundary:
|
|
395
|
+
hyp_word_started = False
|
|
396
|
+
if ref_type != TokType.WordBoundary and hyp_word_builder and not ref_word_builder:
|
|
397
|
+
# INSERTION
|
|
398
|
+
# Hyp word ended, but no ref words have been added. Mark the current hyp word(s) in the span as insertion errors.
|
|
399
|
+
# TODO: Dedupe this logic
|
|
400
|
+
for word in hyp_word_builder:
|
|
401
|
+
alignment.append(AlignLabels.insertion)
|
|
402
|
+
ref_aligned.append('')
|
|
403
|
+
hyp_aligned.append(word)
|
|
404
|
+
hyp_syllable_count = 0
|
|
405
|
+
|
|
406
|
+
# Commit the alignment.
|
|
407
|
+
full_reference.extend(ref_aligned)
|
|
408
|
+
full_hypothesis.extend(hyp_aligned)
|
|
409
|
+
full_alignment.extend(alignment)
|
|
410
|
+
full_phone_align.append(phone_align.subsequence(0, i, preserve_index=False))
|
|
411
|
+
|
|
412
|
+
# Add the remainder to the worklist
|
|
413
|
+
ref_word_span_next = (ref_word_index + len(ref_word_builder), ref_word_limit)
|
|
414
|
+
hyp_word_span_next = (hyp_word_index + len(hyp_word_builder), hyp_word_limit)
|
|
415
|
+
lev = Levenshtein.align(
|
|
416
|
+
ref=[x for x in phone_align.s1 if x],
|
|
417
|
+
hyp=[x for x in phone_align.s2[i:] if x],
|
|
418
|
+
reserve_list=PowerAligner.reserve_list,
|
|
419
|
+
exclusive_sets=PowerAligner.exclusive_sets,
|
|
420
|
+
weights=Levenshtein.wordAlignWeights) #,
|
|
421
|
+
#dist_penalty=PowerAligner.phoneDistPenalty, dist_penalty_set=Levenshtein.wordAlignWeights)
|
|
422
|
+
phone_align_next = lev.expandAlignCompact()
|
|
423
|
+
|
|
424
|
+
worklist.append((ref_word_span_next, hyp_word_span_next, phone_align_next))
|
|
425
|
+
break
|
|
426
|
+
elif hyp_type == TokType.Phoneme and not hyp_word_started:
|
|
427
|
+
hyp_word_started = True
|
|
428
|
+
try:
|
|
429
|
+
hyp_word_item = hyp_word_iter.__next__()
|
|
430
|
+
hyp_word_builder.append(hyp_word_item[1])
|
|
431
|
+
except StopIteration:
|
|
432
|
+
pass
|
|
433
|
+
|
|
434
|
+
# Check for syllable mismatches
|
|
435
|
+
if ref_type == TokType.SyllableBoundary:
|
|
436
|
+
ref_syllable_count += 1
|
|
437
|
+
if hyp_type == TokType.SyllableBoundary:
|
|
438
|
+
hyp_syllable_count += 1
|
|
439
|
+
|
|
440
|
+
if (ref_type == TokType.SyllableBoundary == hyp_type or ref_syllable_count == hyp_syllable_count):
|
|
441
|
+
# No syllable conflicts here!
|
|
442
|
+
ref_extra_syllable_word_index = None
|
|
443
|
+
hyp_extra_syllable_word_index = None
|
|
444
|
+
elif (ref_type == TokType.SyllableBoundary and
|
|
445
|
+
not ref_extra_syllable_word_index and
|
|
446
|
+
TokType.checkAnnotation(phone_align.s2[i - 1]) == TokType.WordBoundary):
|
|
447
|
+
# Extra syllable in hypothesis. We only care if the syllable immediately follows a word boundary.
|
|
448
|
+
# This is because this indicates that a new word is being formed, which may likely be an insertion in hyp.
|
|
449
|
+
ref_extra_syllable_word_index = len(ref_word_builder) - 1
|
|
450
|
+
# print ref_word_builder
|
|
451
|
+
# print 'Syllable/word mismatch at', i
|
|
452
|
+
# print 'Extra hyp word:', ref_word_builder[ref_extra_syllable_word_index]
|
|
453
|
+
elif (hyp_type == TokType.SyllableBoundary and
|
|
454
|
+
not hyp_extra_syllable_word_index and
|
|
455
|
+
TokType.checkAnnotation(phone_align.s2[i - 1]) == TokType.WordBoundary):
|
|
456
|
+
# This time there's an extra syllable in the ref, corresponding to a new ref word.
|
|
457
|
+
hyp_extra_syllable_word_index = len(hyp_word_builder) - 1
|
|
458
|
+
# print hyp_word_builder
|
|
459
|
+
# print 'Syllable/word mismatch at', i
|
|
460
|
+
# print 'Extra ref word:', hyp_word_builder[hyp_extra_syllable_word_index]
|
|
461
|
+
# Concatenate all phoneme alignments
|
|
462
|
+
fp_align = full_phone_align[0]
|
|
463
|
+
for expand_align in full_phone_align[1:]:
|
|
464
|
+
fp_align.append_alignment(expand_align)
|
|
465
|
+
|
|
466
|
+
return ExpandedAlignment(full_reference, full_hypothesis, full_alignment), fp_align
|
|
467
|
+
|