### Install Kaldialign Source: https://context7.com/pzelasko/kaldialign/llms.txt Install Kaldialign using pip, conda, or from source. ```bash pip install kaldialign ``` ```bash conda install -c kaldialign kaldialign ``` ```bash git clone https://github.com/pzelasko/kaldialign.git cd kaldialign python3 -m pip install --verbose . ``` -------------------------------- ### Install kaldialign from Source with Pip Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Clone the repository and install locally using pip. ```bash git clone https://github.com/pzelasko/kaldialign.git cd kaldialign python3 -m pip install --verbose . ``` -------------------------------- ### Install kaldialign from Git with Pip Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Install directly from the GitHub repository using pip. ```bash pip install --verbose -U git+https://github.com/pzelasko/kaldialign.git ``` -------------------------------- ### Install kaldialign with Pip Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Use this command to install the package using pip. ```bash pip install --verbose kaldialign ``` -------------------------------- ### Install kaldialign with Conda Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Use this command to install the package if you are using Conda. ```bash conda install -c kaldialign kaldialign ``` -------------------------------- ### Get Kaldialign Package Version Source: https://context7.com/pzelasko/kaldialign/llms.txt Access the installed version of the kaldialign library using the standard __version__ attribute. This is useful for verifying installation and compatibility. ```python import kaldialign print(kaldialign.__version__) # e.g. → "0.10.0" ``` ```python # Verify the package is correctly installed try: import kaldialign print("kaldialign version:", kaldialign.__version__) except ImportError: print("kaldialign not installed — run: pip install kaldialign") ``` -------------------------------- ### Project and Version Setup Source: https://github.com/pzelasko/kaldialign/blob/main/CMakeLists.txt Defines the project name and sets the version for the Kaldialign package. This version is used for package metadata. ```cmake project(kaldialign CXX) # Please remember to also change line 3 of ./scripts/conda/kaldialign/meta.yaml. # setup.py uses KALDIALIGN_VERSION for the Python package metadata. set(KALDIALIGN_VERSION "0.10.0") ``` -------------------------------- ### Install Python Bindings Source: https://github.com/pzelasko/kaldialign/blob/main/CMakeLists.txt Installs the compiled Python binding target to the parent directory, making it available for import in Python. ```cmake install(TARGETS _kaldialign DESTINATION ../ ) ``` -------------------------------- ### __version__ Source: https://context7.com/pzelasko/kaldialign/llms.txt Accesses the installed version of the Kaldialign package. ```APIDOC ## __version__ ### Description Kaldialign exposes its installed version via the standard `__version__` attribute, resolved from package metadata at import time. ### Request Example ```python import kaldialign print(kaldialign.__version__) ``` ### Response - **__version__** (str): The installed version string of the kaldialign package. ### Response Example ``` "0.10.0" ``` ``` -------------------------------- ### Enforce Out-of-Source Builds Source: https://github.com/pzelasko/kaldialign/blob/main/CMakeLists.txt This snippet prevents in-source builds, guiding users to create a separate build directory for a cleaner build process. ```cmake cmake_minimum_required(VERSION 3.15 FATAL_ERROR) if("x${CMAKE_SOURCE_DIR}" STREQUAL "x${CMAKE_BINARY_DIR}") message(FATAL_ERROR "\nIn-source build is not a good practice. Please use: mkdir build cd build cmake .. to build this project" ) endif() ``` -------------------------------- ### Bootstrap WER CI with two hypotheses Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Supports providing hypotheses from two systems to compute the probability of system 2 improving over system 1. Also accepts `merge_compounds=True`. ```python 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 ``` -------------------------------- ### Bootstrap WER CI calculation Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Obtain 95% confidence intervals for WER using the Bisani and Ney bootstrapping method. Accepts lists of tuples for reference and hypothesis. ```python 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 ``` -------------------------------- ### Compute WER Confidence Intervals with Bootstrap Source: https://context7.com/pzelasko/kaldialign/llms.txt Estimates WER and its 95% confidence interval using the bootstrap method. Use for single system evaluation or two-system comparison to determine if one system is statistically better than another. ```python from kaldialign import bootstrap_wer_ci # Single system: estimate WER + 95% CI refs = [ ("the", "cat", "sat"), ("hello", "world", "foo"), ] hyps = [ ("the", "cat", "mat"), # 1 error / 3 words ("hello", "earth", "foo"), # 1 error / 3 words ] ans = bootstrap_wer_ci(refs, hyps, replications=10000, seed=42) # ans == { # "wer": , # "ci95": , # "ci95min": , # "ci95max": , # } print(f"WER: {ans['wer']:.3f} ± {ans['ci95']:.3f}") # e.g. → "WER: 0.333 ± 0.272" ``` ```python # Two-system comparison: which ASR model is better? hyps2 = [ ("the", "cat", "sat"), # perfect ("hello", "world", "foo"), # perfect ] ans2 = bootstrap_wer_ci(refs, hyps, hyps2, replications=10000, seed=0) # ans2 == { # "system1": {"wer": ..., "ci95": ..., "ci95min": ..., "ci95max": ...}, # "system2": {"wer": ..., "ci95": ..., "ci95min": ..., "ci95max": ...}, # "p_s2_improv_over_s1": 1.0, # probability system2 is better (0.0–1.0) # } s1 = ans2["system1"] s2 = ans2["system2"] print(f"System1 WER: {s1['wer']:.3f}, System2 WER: {s2['wer']:.3f}") print(f"P(S2 better than S1): {ans2['p_s2_improv_over_s1']:.2f}") ``` ```python # Compound-aware bootstrapping refs_c = [("white", "paper"), ("hello", "world")] hyps_c = [("whitepaper",), ("helloworld",)] ans_c = bootstrap_wer_ci(refs_c, hyps_c, merge_compounds=True) # ans_c["wer"] ≈ 0.0 (all compound matches, zero errors) ``` -------------------------------- ### Include pybind11 Module Source: https://github.com/pzelasko/kaldialign/blob/main/CMakeLists.txt Appends the CMake module path and includes the pybind11 module, which is necessary for building Python bindings. ```cmake list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) set(PYBIND11_FINDPYTHON ON) include(pybind11) ``` -------------------------------- ### bootstrap_wer_ci Source: https://github.com/pzelasko/kaldialign/blob/main/README.md `bootstrap_wer_ci(ref, hyp, hyp2=None)` calculates the 95% confidence intervals for Word Error Rate (WER) using the Bisani and Ney bootstrapping method. It can optionally compare two hypotheses (`hyp` and `hyp2`) to assess the probability of the second system improving over the first. Supports `merge_compounds=True`. ```APIDOC ## bootstrap_wer_ci ### Description Computes 95% confidence intervals for Word Error Rate (WER) using bootstrapping. ### Parameters - **ref** (list[tuple[str]]) - The reference sequences. - **hyp** (list[tuple[str]]) - The first hypothesis sequences. - **hyp2** (list[tuple[str]], optional) - The second hypothesis sequences for comparison. Defaults to None. - **merge_compounds** (bool, optional) - If True, allows adjacent words to be concatenated. Defaults to False. ### Returns - dict - A dictionary containing WER, confidence interval ('ci95'), minimum ('ci95min'), maximum ('ci95max'), and optionally results for 'system1', 'system2', and 'p_s2_improv_over_s1' if hyp2 is provided. ``` -------------------------------- ### Set Default Build Type Source: https://github.com/pzelasko/kaldialign/blob/main/CMakeLists.txt Ensures a build type is set if not already specified by the user. Defaults to 'Release' for optimized builds. ```cmake if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() ``` -------------------------------- ### align Source: https://github.com/pzelasko/kaldialign/blob/main/README.md `align(ref, hyp, epsilon)` is 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. This function also supports `merge_compounds=True`. ```APIDOC ## align ### Description Computes the alignment between two sequences of strings. ### Parameters - **ref** (list[str]) - The reference sequence. - **hyp** (list[str]) - The hypothesis sequence. - **epsilon** (str) - A null symbol not present in ref or hyp. - **merge_compounds** (bool, optional) - If True, allows adjacent words to be concatenated. Defaults to False. ### Returns - list[tuple[str, str]] - A list of tuples representing the aligned sequences. ``` -------------------------------- ### Add Python Bindings Module Source: https://github.com/pzelasko/kaldialign/blob/main/CMakeLists.txt Uses pybind11 to add a C++ module for Python bindings, specifying the source files for the extension. ```cmake pybind11_add_module(_kaldialign ./extensions/kaldi_align.cpp ./extensions/kaldialign.cpp ) ``` -------------------------------- ### bootstrap_wer_ci Source: https://context7.com/pzelasko/kaldialign/llms.txt Computes 95% confidence intervals for WER using the bootstrap method. It can compare one or two hypothesis systems against reference sequences. ```APIDOC ## bootstrap_wer_ci(refs, hyps, hyps2=None, replications=10000, seed=0, merge_compounds=False) ### Description Computes 95% confidence intervals for WER using the bootstrap method of Bisani and Ney (2004), matching Kaldi's `compute-wer-bootci` implementation. Accepts a list of reference sequences and one or two hypothesis sequence lists. When two hypothesis lists are provided, also computes the probability that system 2 improves over system 1. ### Parameters - **refs** (list of tuples): A list of reference sequences, where each sequence is a tuple of strings (words). - **hyps** (list of tuples): A list of hypothesis sequences for the first system. - **hyps2** (list of tuples, optional): A list of hypothesis sequences for the second system. Defaults to None. - **replications** (int, optional): The number of bootstrap replications to perform. Defaults to 10000. - **seed** (int, optional): The random seed for reproducibility. Defaults to 0. - **merge_compounds** (bool, optional): If True, treats space-joined strings in references and hypotheses as single compound words. Defaults to False. ### Request Example ```python from kaldialign import bootstrap_wer_ci # Single system: estimate WER + 95% CI refs = [ ("the", "cat", "sat"), ("hello", "world", "foo"), ] hyps = [ ("the", "cat", "mat"), # 1 error / 3 words ("hello", "earth", "foo"), # 1 error / 3 words ] ans = bootstrap_wer_ci(refs, hyps, replications=10000, seed=42) # Two-system comparison: which ASR model is better? hyps2 = [ ("the", "cat", "sat"), # perfect ("hello", "world", "foo"), # perfect ] ans2 = bootstrap_wer_ci(refs, hyps, hyps2, replications=10000, seed=0) # Compound-aware bootstrapping refs_c = [("white", "paper"), ("hello", "world")] hyps_c = [("whitepaper",), ("helloworld",)] ans_c = bootstrap_wer_ci(refs_c, hyps_c, merge_compounds=True) ``` ### Response #### Success Response - **wer** (float): Mean WER estimate. - **ci95** (float): Half-width of the 95% confidence interval. - **ci95min** (float): Lower bound of the 95% confidence interval. - **ci95max** (float): Upper bound of the 95% confidence interval. - **system1** (dict): Statistics for the first system (if `hyps2` is provided). - **wer** (float): Mean WER estimate for system 1. - **ci95** (float): Half-width of the 95% confidence interval for system 1. - **ci95min** (float): Lower bound of the 95% confidence interval for system 1. - **ci95max** (float): Upper bound of the 95% confidence interval for system 1. - **system2** (dict): Statistics for the second system (if `hyps2` is provided). - **wer** (float): Mean WER estimate for system 2. - **ci95** (float): Half-width of the 95% confidence interval for system 2. - **ci95min** (float): Lower bound of the 95% confidence interval for system 2. - **ci95max** (float): Upper bound of the 95% confidence interval for system 2. - **p_s2_improv_over_s1** (float): Probability that system 2 is better than system 1 (0.0–1.0), only if `hyps2` is provided. ### Response Example ```json { "wer": 0.333, "ci95": 0.272, "ci95min": 0.061, "ci95max": 0.605 } ``` ```json { "system1": { "wer": 0.333, "ci95": 0.272, "ci95min": 0.061, "ci95max": 0.605 }, "system2": { "wer": 0.0, "ci95": 0.0, "ci95min": 0.0, "ci95max": 0.0 }, "p_s2_improv_over_s1": 1.0 } ``` ``` -------------------------------- ### align Source: https://context7.com/pzelasko/kaldialign/llms.txt Computes the optimal alignment between two sequences, returning a list of (ref_token, hyp_token) pairs. Uses an epsilon symbol to denote insertions or deletions and indicates substitutions by mismatched tokens. ```APIDOC ## align(ref, hyp, eps_symbol, sclite_mode=False, merge_compounds=False) ### Description Computes the optimal alignment between two sequences, returning a list of `(ref_token, hyp_token)` pairs. The `eps_symbol` (epsilon) is used to represent insertions (when it appears in the first position) or deletions (when it appears in the second position). Mismatched tokens indicate substitutions. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method Python Function Call ### Endpoint None ### Request Example ```python from kaldialign import align EPS = "" ref = ["the", "cat", "sat", "on", "the", "mat"] hyp = ["the", "cat", "sat", "on", "a", "mat"] alignment = align(ref, hyp, EPS) # alignment == [ # ("the", "the"), # ("cat", "cat"), # ("sat", "sat"), # ("on", "on"), # ("the", "a"), ← substitution # ("mat", "mat"), # ] ref2 = ["a", "b", "c"] hyp2 = ["a", "s", "x", "c"] ali2 = align(ref2, hyp2, EPS) # ali2 == [ # ("a", "a"), # ("b", "s"), ← substitution # (EPS, "x"), ← insertion (EPS in ref position) # ("c", "c"), # ] ref3 = ["a", "b"] hyp3 = ["b", "c"] ali3 = align(ref3, hyp3, EPS) # ali3 == [ # ("a", EPS), ← deletion (EPS in hyp position) # ("b", "b"), # (EPS, "c"), ← insertion # ] ``` ### Response #### Success Response - **alignment** (list of tuples) - A list where each tuple contains a reference token and a hypothesis token, representing the alignment. `eps_symbol` indicates insertions or deletions. ``` -------------------------------- ### Align two string sequences Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Obtain the alignment between two string sequences. Ensure EPS is a null symbol not present in either sequence. ```python 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')] ``` -------------------------------- ### Compute Sequence Alignment Source: https://context7.com/pzelasko/kaldialign/llms.txt Computes the optimal alignment between two sequences, returning pairs of tokens. Uses an epsilon symbol to denote insertions or deletions. Mismatches indicate substitutions. ```python from kaldialign import align EPS = "" ref = ["the", "cat", "sat", "on", "the", "mat"] hyp = ["the", "cat", "sat", "on", "a", "mat"] alignment = align(ref, hyp, EPS) # alignment == [ # ("the", "the"), # ("cat", "cat"), # ("sat", "sat"), # ("on", "on"), # ("the", "a"), ← substitution # ("mat", "mat"), # ] ``` ```python # Insertion example: hyp has an extra word ref2 = ["a", "b", "c"] hyp2 = ["a", "s", "x", "c"] ali2 = align(ref2, hyp2, EPS) # ali2 == [ # ("a", "a"), # ("b", "s"), ← substitution # (EPS, "x"), ← insertion (EPS in ref position) # ("c", "c"), # ] ``` ```python # Deletion example: hyp is missing words ref3 = ["a", "b"] hyp3 = ["b", "c"] ali3 = align(ref3, hyp3, EPS) # ali3 == [ # ("a", EPS), ← deletion (EPS in hyp position) # ("b", "b"), # (EPS, "c"), ← insertion # ] ``` -------------------------------- ### Compound word matching with edit_distance Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Enable `merge_compounds=True` to allow adjacent words to match single words at zero cost. This is useful for handling inconsistencies in transcriptions. ```python from kaldialign import edit_distance, align # "white paper" (2 words) matches "whitepaper" (1 word) with 0 errors ref = ["the", "white", "paper", "is", "good"] hyp = ["the", "whitepaper", "is", "good"] results = edit_distance(ref, hyp, merge_compounds=True) assert results["total"] == 0 # Works in both directions results = edit_distance(hyp, ref, merge_compounds=True) assert results["total"] == 0 ``` -------------------------------- ### Compound word matching with align Source: https://github.com/pzelasko/kaldialign/blob/main/README.md When `merge_compounds=True`, the `align` function shows compound matches as space-joined strings. ```python ali = align(ref, hyp, "*", merge_compounds=True) assert ali == [ ("the", "the"), ("white paper", "whitepaper"), ("is", "is"), ("good", "good") ] ``` -------------------------------- ### Edit distance with SCLITE mode Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Use `sclite_mode=True` to compute WER or alignments based on SCLITE style weights (insertion/deletion cost 3, substitution cost 4). ```python # 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. ``` -------------------------------- ### Calculate edit distance between two sequences Source: https://github.com/pzelasko/kaldialign/blob/main/README.md Compute the total edit distance, including insertions, deletions, and substitutions. ```python 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 } ``` -------------------------------- ### edit_distance Source: https://github.com/pzelasko/kaldialign/blob/main/README.md `edit_distance(ref, hyp)` computes the total edit distance, including insertions, deletions, and substitutions. It supports `sclite_mode=True` for WER-based scoring and `merge_compounds=True` for compound word matching. ```APIDOC ## edit_distance ### Description Computes the edit distance between two sequences of strings. ### Parameters - **ref** (list[str]) - The reference sequence. - **hyp** (list[str]) - The hypothesis sequence. - **sclite_mode** (bool, optional) - If True, uses SCLITE style weights. Defaults to False. - **merge_compounds** (bool, optional) - If True, allows adjacent words to be concatenated. Defaults to False. ### Returns - dict - A dictionary containing 'ins' (insertions), 'del' (deletions), 'sub' (substitutions), and 'total' edit distance. ``` -------------------------------- ### Compound Word Matching Source: https://context7.com/pzelasko/kaldialign/llms.txt Enables matching adjacent words in one sequence to a single concatenated word in the other sequence at zero cost. Useful for handling transcription variations like 'white paper' vs 'whitepaper'. ```python from kaldialign import edit_distance, align EPS = "*" # Two ref words match one hyp compound word → 0 errors ref = ["the", "white", "paper", "is", "good"] hyp = ["the", "whitepaper", "is", "good"] result = edit_distance(ref, hyp, merge_compounds=True) # result == { # "ins": 0, "del": 0, "sub": 0, # "total": 0, "ref_len": 5, "err_rate": 0.0 # } ``` ```python # Symmetric: also works ref→hyp result_rev = edit_distance(hyp, ref, merge_compounds=True) # result_rev["total"] == 0 ``` ```python # Three tokens collapse to one compound result_three = edit_distance(["a", "b", "c"], ["abc"], merge_compounds=True) # result_three["total"] == 0 ``` ```python # Mixed: compound match + real substitution ref_mixed = ["the", "white", "paper", "is", "here"] hyp_mixed = ["the", "whitepaper", "was", "here"] result_mixed = edit_distance(ref_mixed, hyp_mixed, merge_compounds=True) # result_mixed == { # "ins": 0, "del": 0, "sub": 1, ``` -------------------------------- ### edit_distance Source: https://context7.com/pzelasko/kaldialign/llms.txt Computes the edit distance between two sequences, returning a dictionary with counts of insertions, deletions, substitutions, total errors, reference length, and error rate. Supports SCLITE-compatible weights and compound word merging. ```APIDOC ## edit_distance(ref, hyp, sclite_mode=False, merge_compounds=False) ### Description Computes the edit distance between two sequences, returning a dict with counts of insertions, deletions, substitutions, the total error count, the reference length, and the error rate. Optionally uses SCLITE-style weights (ins/del cost 3, sub cost 4) for compatibility with the SCLITE scoring tool. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method Python Function Call ### Endpoint None ### Request Example ```python from kaldialign import edit_distance ref = ["hello", "world", "how", "are", "you"] hyp = ["hello", "word", "how", "is", "you"] result = edit_distance(ref, hyp) # result == { # "ins": 0, "del": 0, "sub": 2, # "total": 2, "ref_len": 5, "err_rate": 0.4 # } result_sclite = edit_distance(ref, hyp, sclite_mode=True) # result_sclite == { # "ins": 0, "del": 0, "sub": 2, # "total": 2, "ref_len": 5, "err_rate": 0.4 # } ``` ### Response #### Success Response - **ins** (int) - Count of insertions. - **del** (int) - Count of deletions. - **sub** (int) - Count of substitutions. - **total** (int) - Total edit distance. - **ref_len** (int) - Length of the reference sequence. - **err_rate** (float) - Error rate (total / ref_len). #### Response Example ```json { "ins": 0, "del": 0, "sub": 2, "total": 2, "ref_len": 5, "err_rate": 0.4 } ``` ``` -------------------------------- ### Compute Edit Distance Source: https://context7.com/pzelasko/kaldialign/llms.txt Calculates the edit distance between two sequences, returning counts of insertions, deletions, substitutions, total errors, reference length, and error rate. Supports SCLITE-compatible weights and works with various token types. ```python from kaldialign import edit_distance # Basic word-level edit distance ref = ["hello", "world", "how", "are", "you"] hyp = ["hello", "word", "how", "is", "you"] result = edit_distance(ref, hyp) # result == { # "ins": 0, "del": 0, "sub": 2, # "total": 2, "ref_len": 5, "err_rate": 0.4 # } ``` ```python # SCLITE-compatible mode (ins/del cost 3, sub cost 4) result_sclite = edit_distance(ref, hyp, sclite_mode=True) # result_sclite == { # "ins": 0, "del": 0, "sub": 2, # "total": 2, "ref_len": 5, "err_rate": 0.4 # } ``` ```python # Edge case: empty reference with hypotheses → err_rate = inf result_empty = edit_distance([], ["hello"]) # result_empty == { # "ins": 1, "del": 0, "sub": 0, # "total": 1, "ref_len": 0, "err_rate": float("inf") # } ``` ```python # Works with integer tokens too result_int = edit_distance([1, 2, 3], [1, 4, 3]) # result_int == { # "ins": 0, "del": 0, "sub": 1, # "total": 1, "ref_len": 3, "err_rate": 0.333... # } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.