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,93 @@
|
|
|
1
|
+
import unicodedata
|
|
2
|
+
|
|
3
|
+
import regex as re
|
|
4
|
+
from num2words import num2words
|
|
5
|
+
|
|
6
|
+
from error_align.utils import NUMERIC_TOKEN, basic_normalizer, basic_tokenizer
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def strip_accents(text: str) -> str:
|
|
10
|
+
"""Strip accents from the text."""
|
|
11
|
+
normalized = unicodedata.normalize("NFD", text)
|
|
12
|
+
return "".join(c for c in normalized if unicodedata.category(c) != "Mn")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def normalize_evaluation_segment(segment: str) -> str:
|
|
16
|
+
"""Normalize a segment by removing accents and converting to lowercase.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
segment (str): The segment to normalize.
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
str: The normalized segment.
|
|
23
|
+
"""
|
|
24
|
+
return re.sub(r"[^a-z0-9]", "", strip_accents(segment.lower()))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def convert_numbers_to_words(text: str, lang: str = "en") -> str:
|
|
28
|
+
"""Convert numeric tokens in the text to their word representation.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
text (str): The input text containing numeric tokens.
|
|
32
|
+
lang (str): The language to use for conversion (default is "en").
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
str: The text with numeric tokens converted to words.
|
|
36
|
+
"""
|
|
37
|
+
if bool(re.match(NUMERIC_TOKEN, text)):
|
|
38
|
+
if lang == "en":
|
|
39
|
+
# NOTE: num2words doesn't support thousands separators
|
|
40
|
+
text_ = text.replace(",", "")
|
|
41
|
+
else:
|
|
42
|
+
|
|
43
|
+
text_ = text.replace(".", "")
|
|
44
|
+
text_ = text_.replace(",", ".")
|
|
45
|
+
try:
|
|
46
|
+
return num2words(text_, lang=lang)
|
|
47
|
+
except Exception:
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
return text
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def clean_text(text: str, lang: str = "en") -> dict:
|
|
54
|
+
"""
|
|
55
|
+
Cleans the text by removing examples with empty transcriptions.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
example: The example to clean.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
A cleaned version of the example with empty transcriptions removed.
|
|
62
|
+
"""
|
|
63
|
+
# Remove all tags, e.g., <unk>.
|
|
64
|
+
text = re.sub(r"<[^>]+>", "", text)
|
|
65
|
+
|
|
66
|
+
# Re-contract apostrophes.
|
|
67
|
+
text = re.sub(r"(\w) '(\w)", r"\1'\2", text)
|
|
68
|
+
|
|
69
|
+
# Get normalized tokens.
|
|
70
|
+
normalized_tokens = [basic_normalizer(token.group()) for token in basic_tokenizer(text)]
|
|
71
|
+
|
|
72
|
+
# Convert numbers to words.
|
|
73
|
+
normalized_tokens = [convert_numbers_to_words(token, lang=lang) for token in normalized_tokens]
|
|
74
|
+
|
|
75
|
+
return " ".join(normalized_tokens)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def clean_example(example: dict, lang="en") -> dict:
|
|
79
|
+
"""
|
|
80
|
+
Cleans the example by removing examples with empty transcriptions.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
example: The example to clean.
|
|
84
|
+
lang: The language to use for cleaning.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
A cleaned version of the example with empty transcriptions removed.
|
|
88
|
+
"""
|
|
89
|
+
if "ref" in example:
|
|
90
|
+
example["ref"] = clean_text(example["ref"], lang=lang)
|
|
91
|
+
if "hyp" in example:
|
|
92
|
+
example["hyp"] = clean_text(example["hyp"], lang=lang)
|
|
93
|
+
return example
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
from error_align.utils import DELIMITERS, OP_TYPE_COMBO_MAP_INV, OpType
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _get_levenshtein_values(ref_token: str, hyp_token: str):
|
|
5
|
+
"""Compute the Levenshtein values for deletion, insertion, and diagonal (substitution or match).
|
|
6
|
+
|
|
7
|
+
Args:
|
|
8
|
+
ref_token (str): The reference token.
|
|
9
|
+
hyp_token (str): The hypothesis token.
|
|
10
|
+
|
|
11
|
+
Returns:
|
|
12
|
+
tuple: A tuple containing the deletion cost, insertion cost, and diagonal cost.
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
if hyp_token == ref_token:
|
|
16
|
+
diag_cost = 0
|
|
17
|
+
else:
|
|
18
|
+
diag_cost = 1
|
|
19
|
+
|
|
20
|
+
return 1, 1, diag_cost
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _get_error_align_values(ref_token: str, hyp_token: str):
|
|
24
|
+
"""Compute the error alignment values for deletion, insertion, and diagonal (substitution or match).
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
ref_token (str): The reference token.
|
|
28
|
+
hyp_token (str): The hypothesis token.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
tuple: A tuple containing the deletion cost, insertion cost, and diagonal cost.
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
if hyp_token == ref_token:
|
|
35
|
+
diag_cost = 0
|
|
36
|
+
elif hyp_token in DELIMITERS or ref_token in DELIMITERS:
|
|
37
|
+
diag_cost = 3 # NOTE: Will never be chosen as insert + delete (= 2) is equivalent and cheaper.
|
|
38
|
+
else:
|
|
39
|
+
diag_cost = 2
|
|
40
|
+
|
|
41
|
+
return 1, 1, diag_cost
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def compute_distance_matrix(
|
|
45
|
+
ref: str | list[str],
|
|
46
|
+
hyp: str | list[str],
|
|
47
|
+
score_func: callable,
|
|
48
|
+
backtrace: bool = False,
|
|
49
|
+
dtype: type = int,
|
|
50
|
+
):
|
|
51
|
+
"""Compute the edit distance score matrix between two sequences x (hyp) and y (ref)
|
|
52
|
+
using only pure Python lists.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
ref (str or list[str]): The reference sequence/transcript.
|
|
56
|
+
hyp (str or list[str]): The hypothesis sequence/transcript.
|
|
57
|
+
score_func (callable): A function that takes two tokens (ref_token, hyp_token) and returns
|
|
58
|
+
a tuple of (deletion_cost, insertion_cost, diagonal_cost).
|
|
59
|
+
backtrace (bool): Whether to compute the backtrace matrix.
|
|
60
|
+
dtype (type): The type to store scores (int, float, etc.).
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
list[list]: The score matrix.
|
|
64
|
+
list[list]: The backtrace matrix, if backtrace=True.
|
|
65
|
+
|
|
66
|
+
"""
|
|
67
|
+
hyp_dim, ref_dim = len(hyp) + 1, len(ref) + 1
|
|
68
|
+
|
|
69
|
+
# Create empty score matrix of zeros and initialize first row and column.
|
|
70
|
+
score_matrix = [[dtype(0) for _ in range(ref_dim)] for _ in range(hyp_dim)]
|
|
71
|
+
for j in range(ref_dim):
|
|
72
|
+
score_matrix[0][j] = dtype(j)
|
|
73
|
+
for i in range(hyp_dim):
|
|
74
|
+
score_matrix[i][0] = dtype(i)
|
|
75
|
+
|
|
76
|
+
# Create backtrace matrix and operation combination map and initialize first row and column.
|
|
77
|
+
# Each operation combination is dynamically assigned a unique integer.
|
|
78
|
+
if backtrace:
|
|
79
|
+
backtrace_matrix = [[0 for _ in range(ref_dim)] for _ in range(hyp_dim)]
|
|
80
|
+
backtrace_matrix[0][0] = OP_TYPE_COMBO_MAP_INV[(OpType.MATCH,)]
|
|
81
|
+
for j in range(1, ref_dim):
|
|
82
|
+
backtrace_matrix[0][j] = OP_TYPE_COMBO_MAP_INV[(OpType.DELETE,)]
|
|
83
|
+
for i in range(1, hyp_dim):
|
|
84
|
+
backtrace_matrix[i][0] = OP_TYPE_COMBO_MAP_INV[(OpType.INSERT,)]
|
|
85
|
+
|
|
86
|
+
# Fill in the score and backtrace matrix.
|
|
87
|
+
for j in range(1, ref_dim):
|
|
88
|
+
for i in range(1, hyp_dim):
|
|
89
|
+
ins_cost, del_cost, diag_cost = score_func(ref[j - 1], hyp[i - 1])
|
|
90
|
+
|
|
91
|
+
ins_val = score_matrix[i - 1][j] + ins_cost
|
|
92
|
+
del_val = score_matrix[i][j - 1] + del_cost
|
|
93
|
+
diag_val = score_matrix[i - 1][j - 1] + diag_cost
|
|
94
|
+
new_val = min(ins_val, del_val, diag_val)
|
|
95
|
+
score_matrix[i][j] = dtype(new_val)
|
|
96
|
+
|
|
97
|
+
# Track possible operations (note that the order of operations matters).
|
|
98
|
+
if backtrace:
|
|
99
|
+
pos_ops = tuple()
|
|
100
|
+
if diag_val == new_val and diag_cost == 0:
|
|
101
|
+
pos_ops += (OpType.MATCH,)
|
|
102
|
+
if ins_val == new_val:
|
|
103
|
+
pos_ops += (OpType.INSERT,)
|
|
104
|
+
if del_val == new_val:
|
|
105
|
+
pos_ops += (OpType.DELETE,)
|
|
106
|
+
if diag_val == new_val and diag_cost > 0:
|
|
107
|
+
pos_ops += (OpType.SUBSTITUTE,)
|
|
108
|
+
backtrace_matrix[i][j] = OP_TYPE_COMBO_MAP_INV[pos_ops]
|
|
109
|
+
|
|
110
|
+
if backtrace:
|
|
111
|
+
return score_matrix, backtrace_matrix
|
|
112
|
+
return score_matrix
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def compute_levenshtein_distance_matrix(ref: str | list[str], hyp: str | list[str], backtrace: bool = False):
|
|
116
|
+
"""Compute the Levenshtein distance matrix between two sequences.
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
ref (str): The reference sequence/transcript.
|
|
120
|
+
hyp (str): The hypothesis sequence/transcript.
|
|
121
|
+
backtrace (bool): Whether to compute the backtrace matrix.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
np.ndarray: The score matrix.
|
|
125
|
+
np.ndarray: The backtrace matrix, if backtrace=True.
|
|
126
|
+
|
|
127
|
+
"""
|
|
128
|
+
return compute_distance_matrix(ref, hyp, _get_levenshtein_values, backtrace)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def compute_error_align_distance_matrix(ref: str | list[str], hyp: str | list[str], backtrace: bool = False):
|
|
132
|
+
"""Compute the error alignment distance matrix between two sequences.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
ref (str): The reference sequence/transcript.
|
|
136
|
+
hyp (str): The hypothesis sequence/transcript.
|
|
137
|
+
backtrace (bool): Whether to compute the backtrace matrix.
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
np.ndarray: The score matrix.
|
|
141
|
+
np.ndarray: The backtrace matrix, if backtrace=True.
|
|
142
|
+
|
|
143
|
+
"""
|
|
144
|
+
return compute_distance_matrix(ref, hyp, _get_error_align_values, backtrace)
|