Kaldi alignment methods wrapped into Python
Maintained. Last release 6 months ago, commits in 2 of the last 26 weeks. Weaker on commit activity.
pip install kaldialignkaldialign
A small package that exposes edit distance computation functions from Kaldi. It uses the original Kaldi code and wraps it using pybind11.
Installation
conda install -c kaldialign kaldialign
or
pip install --verbose kaldialign
or
pip install --verbose -U git+https://github.com/pzelasko/kaldialign.git
or
git clone https://github.com/pzelasko/kaldialign.git
cd kaldialign
python3 -m pip install --verbose .
Examples
Alignment
align(ref, hyp, epsilon) - used to obtain the alignment between two string sequences. epsilon should be a null symbol (indicating deletion/insertion) that doesn't exist in either sequence.
from kaldialign import align
EPS = '*'
a = ['a', 'b', 'c']
b = ['a', 's', 'x', 'c']
ali = align(a, b, EPS)
assert ali == [('a', 'a'), ('b', 's'), (EPS, 'x'), ('c', 'c')]
Edit distance
edit_distance(ref, hyp) - used to obtain the total edit distance, as well as the number of insertions, deletions and substitutions.
from kaldialign import edit_distance
a = ['a', 'b', 'c']
b = ['a', 's', 'x', 'c']
results = edit_distance(a, b)
assert results == {
'ins': 1,
'del': 0,
'sub': 1,
'total': 2
}
Batch error rate
batch_error_rate(refs, hyps) - used to obtain corpus-level error counts and error rate for a batch of reference/hypothesis sequence pairs. It computes error rate as the sum of all insertions, deletions, and substitutions divided by the total number of reference symbols.
from kaldialign import batch_error_rate
refs = [
("a", "b", "c"),
("d", "e"),
]
hyps = [
("a", "x", "c", "y"),
("d",),
]
results = batch_error_rate(refs, hyps)
assert results == {
"ins": 1,
"del": 1,
"sub": 1,
"total": 3,
"ref_len": 5,
"err_rate": 0.6,
}
For alignment, edit distance, and batch error rate, you can pass sclite_mode=True to compute
WER or alignments based on SCLITE style weights, i.e., insertion/deletion cost 3 and substitution cost 4.
Compound word matching
All functions accept merge_compounds=True to allow adjacent words in either sequence to be
concatenated (without separator) to match a single word in the other sequence at zero cost.
This is useful whenever there are inconsistencies within transcriptions,
or between training and testing conditions of a model evaluated with WER.