Skip to main content

Kaldi alignment methods wrapped into Python

Project description

kaldialign

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
}

For alignment and edit distance, 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.

Bootstrapping method to extract WER 95% confidence intervals

boostrap_wer_ci(ref, hyp, hyp2=None) - obtain the 95% confidence intervals for WER using Bisani and Ney boostrapping method.

from kaldialign import bootstrap_wer_ci

ref = [
    ("a", "b", "c"),
    ("d", "e", "f"),
]
hyp = [
    ("a", "b", "d"),
    ("e", "f", "f"),
]
ans = bootstrap_wer_ci(ref, hyp)
assert ans["wer"] == 0.4989
assert ans["ci95"] == 0.2312
assert ans["ci95min"] == 0.2678
assert ans["ci95max"] == 0.7301

It also supports providing hypotheses from system 1 and system 2 to compute the probability of S2 improving over S1:

from kaldialign import bootstrap_wer_ci

ref = [
    ("a", "b", "c"),
    ("d", "e", "f"),
]
hyp = [
    ("a", "b", "d"),
    ("e", "f", "f"),
]
hyp2 = [
    ("a", "b", "c"),
    ("e", "e", "f"),
]
ans = bootstrap_wer_ci(ref, hyp, hyp2)

s = ans["system1"]
assert s["wer"] == 0.4989
assert s["ci95"] == 0.2312
assert s["ci95min"] == 0.2678
assert s["ci95max"] == 0.7301

s = ans["system2"]
assert s["wer"] == 0.1656
assert s["ci95"] == 0.2312
assert s["ci95min"] == -0.0656
assert s["ci95max"] == 0.3968

assert ans["p_s2_improv_over_s1"] == 1.0

Motivation

The need for this arised from the fact that practically all implementations of the Levenshtein distance have slight differences, making it impossible to use a different scoring tool than Kaldi and get the same error rate results. This package copies code from Kaldi directly and wraps it using pybind11, avoiding the issue altogether.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

kaldialign-0.9.1-cp312-cp312-win_amd64.whl (70.8 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

kaldialign-0.9.1-cp312-cp312-win32.whl (62.3 kB view hashes)

Uploaded CPython 3.12 Windows x86

kaldialign-0.9.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (92.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

kaldialign-0.9.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (87.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

kaldialign-0.9.1-cp312-cp312-macosx_10_9_universal2.whl (115.2 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

kaldialign-0.9.1-cp311-cp311-win_amd64.whl (70.1 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

kaldialign-0.9.1-cp311-cp311-win32.whl (61.8 kB view hashes)

Uploaded CPython 3.11 Windows x86

kaldialign-0.9.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

kaldialign-0.9.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (87.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

kaldialign-0.9.1-cp311-cp311-macosx_10_9_universal2.whl (113.3 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

kaldialign-0.9.1-cp310-cp310-win_amd64.whl (70.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

kaldialign-0.9.1-cp310-cp310-win32.whl (61.7 kB view hashes)

Uploaded CPython 3.10 Windows x86

kaldialign-0.9.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

kaldialign-0.9.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (87.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

kaldialign-0.9.1-cp310-cp310-macosx_10_9_universal2.whl (113.3 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

kaldialign-0.9.1-cp39-cp39-win_amd64.whl (70.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

kaldialign-0.9.1-cp39-cp39-win32.whl (62.0 kB view hashes)

Uploaded CPython 3.9 Windows x86

kaldialign-0.9.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (92.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

kaldialign-0.9.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (87.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

kaldialign-0.9.1-cp39-cp39-macosx_10_9_universal2.whl (113.6 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

kaldialign-0.9.1-cp38-cp38-win_amd64.whl (70.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

kaldialign-0.9.1-cp38-cp38-win32.whl (61.7 kB view hashes)

Uploaded CPython 3.8 Windows x86

kaldialign-0.9.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

kaldialign-0.9.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (87.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

kaldialign-0.9.1-cp38-cp38-macosx_10_9_universal2.whl (113.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

kaldialign-0.9.1-cp37-cp37m-win_amd64.whl (70.7 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

kaldialign-0.9.1-cp37-cp37m-win32.whl (62.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

kaldialign-0.9.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (93.7 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

kaldialign-0.9.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (89.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page