forked from MontrealCorpusTools/Montreal-Forced-Aligner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
877 lines (752 loc) · 23.4 KB
/
helper.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
"""
Helper functions
================
"""
from __future__ import annotations
import dataclasses
import functools
import itertools
import json
import sys
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
import ansiwrap
import numpy
from colorama import Fore, Style
if TYPE_CHECKING:
from montreal_forced_aligner.abc import CorpusMappingType, MetaDict, ScpType
from montreal_forced_aligner.dictionary.pronunciation import Word
from montreal_forced_aligner.textgrid import CtmInterval
__all__ = [
"TerminalPrinter",
"comma_join",
"make_safe",
"make_scp_safe",
"save_scp",
"load_scp",
"load_scp_safe",
"score",
"edit_distance",
"output_mapping",
"parse_old_features",
"compare_labels",
"overlap_scoring",
"align_phones",
]
def parse_old_features(config: MetaDict) -> MetaDict:
"""
Backwards compatibility function to parse old feature configuration blocks
Parameters
----------
config: dict[str, Any]
Configuration parameters
Returns
-------
dict[str, Any]
Up to date versions of feature blocks
"""
feature_key_remapping = {
"type": "feature_type",
"deltas": "uses_deltas",
}
skip_keys = ["lda", "fmllr"]
if "features" in config:
for key in skip_keys:
if key in config["features"]:
del config["features"][key]
for key, new_key in feature_key_remapping.items():
if key in config["features"]:
config["features"][new_key] = config["features"][key]
del config["features"][key]
else:
for key in skip_keys:
if key in config:
del config[key]
for key, new_key in feature_key_remapping.items():
if key in config:
config[new_key] = config[key]
del config[key]
return config
class TerminalPrinter:
"""
Helper class to output colorized text
Attributes
----------
colors: dict[str, str]
Mapping of color names to terminal codes in colorama (or empty strings
if the global terminal_colors flag is set to False)
"""
def __init__(self):
from .config import load_global_config
c = load_global_config()
self.colors = {}
self.colors["bright"] = ""
self.colors["green"] = ""
self.colors["red"] = ""
self.colors["blue"] = ""
self.colors["cyan"] = ""
self.colors["yellow"] = ""
self.colors["reset"] = ""
self.colors["normal"] = ""
self.width = c["terminal_width"]
self.indent_level = 0
self.indent_size = 2
if c["terminal_colors"]:
self.colors["bright"] = Style.BRIGHT
self.colors["green"] = Fore.GREEN
self.colors["red"] = Fore.RED
self.colors["blue"] = Fore.BLUE
self.colors["cyan"] = Fore.CYAN
self.colors["yellow"] = Fore.YELLOW
self.colors["reset"] = Style.RESET_ALL
self.colors["normal"] = Style.NORMAL
def error_text(self, text: Any) -> str:
"""
Highlight text as an error
Parameters
----------
text: Any
Text to highlight
Returns
-------
str
Highlighted text
"""
return self.colorize(text, "red")
def emphasized_text(self, text: Any) -> str:
"""
Highlight text as emphasis
Parameters
----------
text: Any
Text to highlight
Returns
-------
str
Highlighted text
"""
return self.colorize(text, "bright")
def pass_text(self, text: Any) -> str:
"""
Highlight text as good
Parameters
----------
text: Any
Text to highlight
Returns
-------
str
Highlighted text
"""
return self.colorize(text, "green")
def warning_text(self, text: Any) -> str:
"""
Highlight text as a warning
Parameters
----------
text: Any
Text to highlight
Returns
-------
str
Highlighted text
"""
return self.colorize(text, "yellow")
@property
def indent_string(self) -> str:
"""Indent string to use in formatting the output messages"""
return " " * self.indent_size * self.indent_level
def print_header(self, header: str) -> None:
"""
Print a section header
Parameters
----------
header: str
Section header string
"""
self.indent_level = 0
print()
underline = "*" * len(header)
print(self.colorize(underline, "bright"))
print(self.colorize(header, "bright"))
print(self.colorize(underline, "bright"))
print()
self.indent_level += 1
def print_sub_header(self, header: str) -> None:
"""
Print a subsection header
Parameters
----------
header: str
Subsection header string
"""
underline = "=" * len(header)
print(self.indent_string + self.colorize(header, "bright"))
print(self.indent_string + self.colorize(underline, "bright"))
print()
self.indent_level += 1
def print_end_section(self) -> None:
"""Mark the end of a section"""
self.indent_level -= 1
print()
def format_info_lines(self, lines: Union[list[str], str]) -> List[str]:
"""
Format lines
Parameters
----------
lines: Union[list[str], str
Lines to format
Returns
-------
str
Formatted string
"""
if isinstance(lines, str):
lines = [lines]
for i, line in enumerate(lines):
lines[i] = ansiwrap.fill(
line,
initial_indent=self.indent_string,
subsequent_indent=" " * self.indent_size * (self.indent_level + 1),
width=self.width,
break_on_hyphens=False,
break_long_words=False,
drop_whitespace=False,
)
return lines
def print_info_lines(self, lines: Union[list[str], str]) -> None:
"""
Print formatted information lines
Parameters
----------
lines: Union[list[str], str
Lines to format
"""
if isinstance(lines, str):
lines = [lines]
lines = self.format_info_lines(lines)
for line in lines:
print(line)
def print_green_stat(self, stat: Any, text: str) -> None:
"""
Print a statistic in green
Parameters
----------
stat: Any
Statistic to print
text: str
Other text to follow statistic
"""
print(self.indent_string + f"{self.colorize(stat, 'green')} {text}")
def print_yellow_stat(self, stat, text) -> None:
"""
Print a statistic in yellow
Parameters
----------
stat: Any
Statistic to print
text: str
Other text to follow statistic
"""
print(self.indent_string + f"{self.colorize(stat, 'yellow')} {text}")
def print_red_stat(self, stat, text) -> None:
"""
Print a statistic in red
Parameters
----------
stat: Any
Statistic to print
text: str
Other text to follow statistic
"""
print(self.indent_string + f"{self.colorize(stat, 'red')} {text}")
def colorize(self, text: Any, color: str) -> str:
"""
Colorize a string
Parameters
----------
text: Any
Text to colorize
color: str
Colorama code or empty string to wrap the text
Returns
-------
str
Colorized string
"""
return f"{self.colors[color]}{text}{self.colors['reset']}"
def print_block(self, block: dict, starting_level: int = 1) -> None:
"""
Print a configuration block
Parameters
----------
block: dict
Configuration options to output
starting_level: int
Starting indentation level
"""
for k, v in block.items():
value_color = None
key_color = None
value = ""
if isinstance(k, tuple):
k, key_color = k
if isinstance(v, tuple):
value, value_color = v
elif not isinstance(v, dict):
value = v
self.print_information_line(k, value, key_color, value_color, starting_level)
if isinstance(v, dict):
self.print_block(v, starting_level=starting_level + 1)
print()
def print_config(self, configuration: MetaDict) -> None:
"""
Pretty print a configuration
Parameters
----------
configuration: dict[str, Any]
Configuration to print
"""
for k, v in configuration.items():
if "name" in v:
name = v["name"]
name_color = None
if isinstance(name, tuple):
name, name_color = name
self.print_information_line(k, name, value_color=name_color, level=0)
if "data" in v:
self.print_block(v["data"])
def print_information_line(
self,
key: str,
value: Any,
key_color: Optional[str] = None,
value_color: Optional[str] = None,
level: int = 1,
) -> None:
"""
Pretty print a given configuration line
Parameters
----------
key: str
Configuration key
value: Any
Configuration value
key_color: str
Key color
value_color: str
Value color
level: int
Indentation level
"""
if key_color is None:
key_color = "bright"
if value_color is None:
value_color = "yellow"
if isinstance(value, bool):
if value:
value_color = "green"
else:
value_color = "red"
if isinstance(value, (list, tuple, set)):
value = comma_join([self.colorize(x, value_color) for x in sorted(value)])
else:
value = self.colorize(str(value), value_color)
indent = (" " * level) + "-"
subsequent_indent = " " * (level + 1)
if key:
key = f" {key}:"
subsequent_indent += " " * (len(key))
print(
ansiwrap.fill(
f"{self.colorize(key, key_color)} {value}",
width=self.width,
initial_indent=indent,
subsequent_indent=subsequent_indent,
)
)
def comma_join(sequence: List[Any]) -> str:
"""
Helper function to combine a list into a human-readable expression with commas and a
final "and" separator
Parameters
----------
sequence: list[Any]
Items to join together into a list
Returns
-------
str
Joined items in list format
"""
if len(sequence) < 3:
return " and ".join(sequence)
return f"{', '.join(sequence[:-1])}, and {sequence[-1]}"
def make_safe(element: Any) -> str:
"""
Helper function to make an element a string
Parameters
----------
element: Any
Element to recursively turn into a string
Returns
-------
str
All elements combined into a string
"""
if isinstance(element, list):
return " ".join(map(make_safe, element))
return str(element)
def make_scp_safe(string: str) -> str:
"""
Helper function to make a string safe for saving in Kaldi scp files. They use space as a delimiter, so
any spaces in the string will be converted to "_MFASPACE_" to preserve them
Parameters
----------
string: str
Text to escape
Returns
-------
str
Escaped text
"""
return string.replace(" ", "_MFASPACE_")
def load_scp_safe(string: str) -> str:
"""
Helper function to load previously made safe text. All instances of "_MFASPACE_" will be converted to a
regular space character
Parameters
----------
string: str
String to convert
Returns
-------
str
Converted string
"""
return string.replace("_MFASPACE_", " ")
def output_mapping(mapping: CorpusMappingType, path: str, skip_safe: bool = False) -> None:
"""
Helper function to save mapping information (i.e., utt2spk) in Kaldi scp format
CorpusMappingType is either a dictionary of key to value for
one-to-one mapping case and a dictionary of key to list of values for one-to-many case.
See Also
--------
:func:`~montreal_forced_aligner.helper.save_scp`
For another function that saves SCPs from lists
Parameters
----------
mapping: CorpusMappingType
Mapping to output
path: str
Path to save mapping
skip_safe: bool, optional
Flag for whether to skip over making a string safe
"""
if not mapping:
return
with open(path, "w", encoding="utf8") as f:
for k in sorted(mapping.keys()):
v = mapping[k]
if isinstance(v, (list, set, tuple)):
v = " ".join(map(str, v))
elif not skip_safe:
v = make_scp_safe(v)
f.write(f"{make_scp_safe(k)} {v}\n")
def save_scp(
scp: ScpType, path: str, sort: Optional[bool] = True, multiline: Optional[bool] = False
) -> None:
"""
Helper function to save an arbitrary SCP.
ScpType is either a list of tuples (str, str) for one-to-one mapping files or
a list of tuples (str, list) for one-to-many mappings.
See Also
--------
:kaldi_docs:`io#io_sec_scp_details`
For more information on the SCP format
Parameters
----------
scp: ScpType
SCP to save
path: str
File path
sort: bool, optional
Flag for whether the output file should be sorted
multiline: bool, optional
Flag for whether the SCP contains multiline data (i.e., utterance FSTs)
"""
if sys.platform == "win32":
newline = ""
else:
newline = None
if not scp:
return
with open(path, "w", encoding="utf8", newline=newline) as f:
if sort:
scp = sorted(scp)
for line in scp:
if multiline:
f.write(f"{make_safe(line[0])}\n{make_safe(line[1])}\n")
else:
f.write(f"{' '.join(map(make_safe, line))}\n")
def load_scp(path: str, data_type: Optional[Type] = str) -> CorpusMappingType:
"""
Load a Kaldi script file (.scp)
Scp files in Kaldi can either be one-to-one or one-to-many, with the first element separated by
whitespace as the key and the remaining whitespace-delimited elements the values.
Returns a dictionary of key to value for
one-to-one mapping case and a dictionary of key to list of values for one-to-many case.
See Also
--------
:kaldi_docs:`io#io_sec_scp_details`
For more information on the SCP format
Parameters
----------
path : str
Path to Kaldi script file
data_type : type
Type to coerce the data to
Returns
-------
CorpusMappingType
Dictionary where the keys are the first column and the values are all
other columns in the scp file
"""
scp = {}
with open(path, "r", encoding="utf8") as f:
for line in f:
line = line.strip()
if line == "":
continue
line_list = line.split()
key = load_scp_safe(line_list.pop(0))
if len(line_list) == 1:
value = data_type(line_list[0])
if isinstance(value, str):
value = load_scp_safe(value)
else:
value = [data_type(x) for x in line_list if x not in ["[", "]"]]
scp[key] = value
return scp
def edit_distance(x: List[str], y: List[str]) -> int:
"""
Compute edit distance between two sets of labels
See Also
--------
`https://gist.github.com/kylebgorman/8034009 <https://gist.github.com/kylebgorman/8034009>`_
For a more expressive version of this function
Parameters
----------
x: list[str]
First sequence to compare
y: list[str]
Second sequence to compare
Returns
-------
int
Edit distance
"""
idim = len(x) + 1
jdim = len(y) + 1
table = numpy.zeros((idim, jdim), dtype=numpy.uint8)
table[1:, 0] = 1
table[0, 1:] = 1
for i in range(1, idim):
for j in range(1, jdim):
if x[i - 1] == y[j - 1]:
table[i][j] = table[i - 1][j - 1]
else:
c1 = table[i - 1][j]
c2 = table[i][j - 1]
c3 = table[i - 1][j - 1]
table[i][j] = min(c1, c2, c3) + 1
return int(table[-1][-1])
def score_g2p(gold: Word, hypo: Word) -> Tuple[int, int]:
"""
Computes sufficient statistics for LER calculation.
Parameters
----------
gold: Labels
The reference labels
hypo: Labels
The hypothesized labels
multiple_hypotheses: bool
Flag for whether the hypotheses contain multiple
Returns
-------
int
Edit distance
int
Length of the gold labels
"""
for h in hypo.pronunciations:
if h in gold.pronunciations:
return 0, len(h)
edits = 100000
best_length = 100000
for (g, h) in itertools.product(gold.pronunciations, hypo.pronunciations):
e = edit_distance(g.pronunciation, h.pronunciation)
if e < edits:
edits = e
best_length = len(g)
if not edits:
best_length = len(g)
break
return edits, best_length
def score(gold: List[str], hypo: List[str]) -> Tuple[int, int]:
"""
Computes sufficient statistics for LER calculation.
Parameters
----------
gold: list[str]
The reference labels
hypo: list[str]
The hypothesized labels
multiple_hypotheses: bool
Flag for whether the hypotheses contain multiple
Returns
-------
int
Edit distance
int
Length of the gold labels
"""
edits = edit_distance(gold, hypo)
return edits, len(gold)
def compare_labels(
ref: str, test: str, silence_phone: str, mapping: Optional[Dict[str, str]] = None
) -> int:
"""
Parameters
----------
ref: str
test: str
mapping: Optional[dict[str, str]]
Returns
-------
int
0 if labels match or they're in mapping, 2 otherwise
"""
if ref == test:
return 0
if ref == silence_phone or test == silence_phone:
return 10
if mapping is not None and test in mapping:
if isinstance(mapping[test], str):
if mapping[test] == ref:
return 0
elif ref in mapping[test]:
return 0
ref = ref.lower()
test = test.lower()
if ref == test:
return 0
return 2
def overlap_scoring(
first_element: CtmInterval,
second_element: CtmInterval,
silence_phone: str,
mapping: Optional[Dict[str, str]] = None,
) -> float:
r"""
Method to calculate overlap scoring
.. math::
Score = -(\lvert begin_{1} - begin_{2} \rvert + \lvert end_{1} - end_{2} \rvert + \begin{cases}
0, & if label_{1} = label_{2} \\
2, & otherwise
\end{cases})
See Also
--------
`Blog post <https://memcauliffe.com/update-on-montreal-forced-aligner-performance.html>`_
For a detailed example that using this metric
Parameters
----------
first_element: :class:`~montreal_forced_aligner.data.CtmInterval`
First CTM interval to compare
second_element: :class:`~montreal_forced_aligner.data.CtmInterval`
Second CTM interval
mapping: Optional[dict[str, str]]
Optional mapping of phones to treat as matches even if they have different symbols
Returns
-------
float
Score calculated as the negative sum of the absolute different in begin timestamps, absolute difference in end
timestamps and the label score
"""
begin_diff = abs(first_element.begin - second_element.begin)
end_diff = abs(first_element.end - second_element.end)
label_diff = compare_labels(first_element.label, second_element.label, silence_phone, mapping)
return -1 * (begin_diff + end_diff + label_diff)
class EnhancedJSONEncoder(json.JSONEncoder):
"""JSON serialization"""
def default(self, o):
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
if isinstance(o, set):
return list(o)
return dataclasses.asdict(o)
def jsonl_encoder(obj):
return json.dumps(obj, cls=EnhancedJSONEncoder)
def align_phones(
ref: List[CtmInterval],
test: List[CtmInterval],
silence_phone: str,
custom_mapping: Optional[Dict[str, str]] = None,
) -> Tuple[float, float]:
"""
Align phones based on how much they overlap and their phone label, with the ability to specify a custom mapping for
different phone labels to be scored as if they're the same phone
Parameters
----------
ref: list[:class:`~montreal_forced_aligner.data.CtmInterval`]
List of CTM intervals as reference
test: list[:class:`~montreal_forced_aligner.data.CtmInterval`]
List of CTM intervals to compare to reference
silence_phones: set[str]
Set of silence phones (these are ignored in the final calculation)
custom_mapping: dict[str, str], optional
Mapping of phones to treat as matches even if they have different symbols
Returns
-------
float
Score based on the average amount of overlap in phone intervals
float
Phone error rate
"""
from Bio import pairwise2
if custom_mapping is None:
score_func = functools.partial(overlap_scoring, silence_phone=silence_phone)
else:
score_func = functools.partial(
overlap_scoring, silence_phone=silence_phone, mapping=custom_mapping
)
alignments = pairwise2.align.globalcs(
ref, test, score_func, -5, -5, gap_char=["-"], one_alignment_only=True
)
overlap_count = 0
overlap_sum = 0
num_insertions = 0
num_deletions = 0
num_substitutions = 0
for a in alignments:
for i, sa in enumerate(a.seqA):
sb = a.seqB[i]
if sa == "-":
if sb.label != silence_phone:
num_insertions += 1
else:
continue
elif sb == "-":
if sa.label != silence_phone:
num_deletions += 1
else:
continue
else:
overlap_sum += abs(sa.begin - sb.begin) + abs(sa.end - sb.end)
overlap_count += 1
if compare_labels(sa.label, sb.label, silence_phone, mapping=custom_mapping) > 0:
num_substitutions += 1
if overlap_count:
score = overlap_sum / overlap_count
else:
score = None
phone_error_rate = (num_insertions + num_deletions + (2 * num_substitutions)) / len(ref)
return score, phone_error_rate