error-align 0.1.0b1__tar.gz
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-0.1.0b1/PKG-INFO +161 -0
- error_align-0.1.0b1/README.md +119 -0
- error_align-0.1.0b1/pyproject.toml +95 -0
- error_align-0.1.0b1/src/error_align/__init__.py +7 -0
- error_align-0.1.0b1/src/error_align/backtrace_graph.py +258 -0
- error_align-0.1.0b1/src/error_align/baselines/__init__.py +10 -0
- error_align-0.1.0b1/src/error_align/baselines/optimal_word_alignment.py +111 -0
- error_align-0.1.0b1/src/error_align/baselines/power/normalize/__init__.py +3 -0
- error_align-0.1.0b1/src/error_align/baselines/power/normalize/contractions.py +152 -0
- error_align-0.1.0b1/src/error_align/baselines/power/normalize/normalize.py +295 -0
- error_align-0.1.0b1/src/error_align/baselines/power/normalize/numbers.py +289 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/__init__.py +3 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/aligner.py +467 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/levenshtein.py +576 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/phonemes.py +16 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/pronounce.py +85 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/punct.py +166 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/readers.py +48 -0
- error_align-0.1.0b1/src/error_align/baselines/power/power/writers.py +259 -0
- error_align-0.1.0b1/src/error_align/baselines/power_alignment.py +61 -0
- error_align-0.1.0b1/src/error_align/baselines/rapidfuzz_word_alignment.py +100 -0
- error_align-0.1.0b1/src/error_align/baselines/utils.py +93 -0
- error_align-0.1.0b1/src/error_align/edit_distance.py +144 -0
- error_align-0.1.0b1/src/error_align/error_align.py +495 -0
- error_align-0.1.0b1/src/error_align/func.py +38 -0
- error_align-0.1.0b1/src/error_align/utils.py +166 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: error-align
|
|
3
|
+
Version: 0.1.0b1
|
|
4
|
+
Summary: Text-to-text alignment algorithm for speech recognition error analysis.
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: speech-recognition,text-alignment,NLP,evaluation
|
|
7
|
+
Author: Lasse Borgholt
|
|
8
|
+
Author-email: lb@corti.ai
|
|
9
|
+
Requires-Python: >=3.11,<3.13
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
19
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
20
|
+
Provides-Extra: evaluation
|
|
21
|
+
Requires-Dist: backoff (>=2.2.1,<3.0.0) ; extra == "evaluation"
|
|
22
|
+
Requires-Dist: click (>=8.2.1,<9.0.0) ; extra == "evaluation"
|
|
23
|
+
Requires-Dist: datasets (>=3.3.2,<4.0.0) ; extra == "evaluation"
|
|
24
|
+
Requires-Dist: gitpython (>=3.1.45,<4.0.0) ; extra == "evaluation"
|
|
25
|
+
Requires-Dist: librosa (>=0.11.0,<0.12.0) ; extra == "evaluation"
|
|
26
|
+
Requires-Dist: matplotlib (>=3.10.6,<4.0.0) ; extra == "evaluation"
|
|
27
|
+
Requires-Dist: nemo-toolkit[all] (>=2.4.0,<3.0.0) ; extra == "evaluation"
|
|
28
|
+
Requires-Dist: num2words (>=0.5.14,<0.6.0) ; extra == "evaluation"
|
|
29
|
+
Requires-Dist: numpy (>=1.22,<2.0) ; extra == "evaluation"
|
|
30
|
+
Requires-Dist: pandas (>=2.3.1,<3.0.0) ; extra == "evaluation"
|
|
31
|
+
Requires-Dist: pyphen (>=0.17.2,<0.18.0) ; extra == "evaluation"
|
|
32
|
+
Requires-Dist: rapidfuzz (>=3.13.0,<4.0.0) ; extra == "evaluation"
|
|
33
|
+
Requires-Dist: regex (>=2024.11.6,<2025.0.0)
|
|
34
|
+
Requires-Dist: scipy (>=1.16.1,<2.0.0) ; extra == "evaluation"
|
|
35
|
+
Requires-Dist: soundfile (>=0.13.1,<0.14.0) ; extra == "evaluation"
|
|
36
|
+
Requires-Dist: textgrid (>=1.6.1,<2.0.0) ; extra == "evaluation"
|
|
37
|
+
Requires-Dist: tqdm (>=4.67.1,<5.0.0)
|
|
38
|
+
Requires-Dist: transformers (>=4.51.3,<5.0.0) ; extra == "evaluation"
|
|
39
|
+
Requires-Dist: unidecode (>=1.4.0,<2.0.0)
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
<p align="center">
|
|
43
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/logo.svg" alt="ErrorAlign Logo" width="85%"/>
|
|
44
|
+
</p>
|
|
45
|
+
|
|
46
|
+
<p align="center">
|
|
47
|
+
<img src="https://img.shields.io/badge/python-%203.11%20|%203.12-green" alt="Python Versions">
|
|
48
|
+
<img src="https://img.shields.io/codecov/c/github/borgholt/error-align/main.svg?style=flat-square" alt="Coverage">
|
|
49
|
+
<img src="https://github.com/borgholt/error-align/actions/workflows/lint.yml/badge.svg?branch=main" alt="Linting" style="margin-left:5px;">
|
|
50
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="License">
|
|
51
|
+
</p>
|
|
52
|
+
|
|
53
|
+
<br/>
|
|
54
|
+
|
|
55
|
+
**Text-to-text alignment algorithm for speech recognition error analysis.** ErrorAlign helps you dig deeper into your speech recognition projects by accurately aligning each word in a reference transcript with the model-generated transcript. Unlike traditional methods, such as Levenshtein-based alignment, it is not restricted to simple one-to-one alignment, but can map a single reference word to multiple words or subwords in the model output. This enables quick and reliable identification of error patterns in rare words, names, or domain-specific terms that matter most for your application.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
__Contents__ | [Installation](#installation) | [Quickstart](#quickstart) | [Work-in-Progress](#wip) | [Citation and Research](#citation) |
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
<a name="installation">
|
|
64
|
+
|
|
65
|
+
## Installation
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
pip install error-align
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
<a name="quickstart">
|
|
73
|
+
|
|
74
|
+
## Quickstart
|
|
75
|
+
```python
|
|
76
|
+
from error_align import error_align
|
|
77
|
+
|
|
78
|
+
ref = "Some things are worth noting!"
|
|
79
|
+
hyp = "Something worth nothing period?"
|
|
80
|
+
|
|
81
|
+
alignments = error_align(ref, hyp)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Resulting alignments:
|
|
85
|
+
```python
|
|
86
|
+
Alignment(SUBSTITUTE: "Some" -> "Some"-),
|
|
87
|
+
Alignment(SUBSTITUTE: "things" -> -"thing"),
|
|
88
|
+
Alignment(DELETE: "are"),
|
|
89
|
+
Alignment(MATCH: "worth" == "worth"),
|
|
90
|
+
Alignment(SUBSTITUTE: "noting" -> "nothing"),
|
|
91
|
+
Alignment(INSERT: "period")
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
<a name="wip">
|
|
95
|
+
|
|
96
|
+
## Work-in-Progress
|
|
97
|
+
|
|
98
|
+
- Optimization for longform text.
|
|
99
|
+
- Efficient word-level first-pass.
|
|
100
|
+
- C++ version with Python bindings.
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
<a name="citation">
|
|
105
|
+
|
|
106
|
+
## Citation and Research
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
@article{borgholt2021alignment,
|
|
110
|
+
title={A Text-To-Text Alignment Algorithm for Better Evaluation of Modern Speech Recognition Systems},
|
|
111
|
+
author={Borgholt, Lasse and Havtorn, Jakob and Igel, Christian and Maal{\o}e, Lars and Tan, Zheng-Hua},
|
|
112
|
+
journal={arXiv preprint arXiv:2509.24478},
|
|
113
|
+
year={2025}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
__To reproduce results from the paper:__
|
|
118
|
+
- Install with extra evaluation dependencies:
|
|
119
|
+
- `pip install error-align[evaluation]`
|
|
120
|
+
- Clone this repository:
|
|
121
|
+
- `git clone https://github.com/borgholt/error-align.git`
|
|
122
|
+
- Navigate to the evaluation directory:
|
|
123
|
+
- `cd error-align/evaluation`
|
|
124
|
+
- Transcribe a dataset for evaluation. For example:
|
|
125
|
+
- `python transcribe_dataset.py --model_name whisper --dataset_name commonvoice --language_code fr`
|
|
126
|
+
- Run evaluation script on the output file. For example:
|
|
127
|
+
- `python evaluate_dataset.py --transcript_file transcribed_data/whisper_commonvoice_test_fr.parquet`
|
|
128
|
+
|
|
129
|
+
__Notes:__
|
|
130
|
+
- To reproduce results on the `primock57` dataset, first run: `python prepare_primock57.py`.
|
|
131
|
+
- Use the `--help` flag to see all available options for `transcribe_dataset.py` and `evaluate_dataset.py`.
|
|
132
|
+
- All results reported in the paper are based on the test sets.
|
|
133
|
+
|
|
134
|
+
__Collaborators:__
|
|
135
|
+
|
|
136
|
+
<br/>
|
|
137
|
+
|
|
138
|
+
<div>
|
|
139
|
+
<a href="https://corti.ai">
|
|
140
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/corti.png" alt="Corti" height="75">
|
|
141
|
+
</a>
|
|
142
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
143
|
+
<a href="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/aau.png">
|
|
144
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/aau.png" alt="Aalborg University" height="75">
|
|
145
|
+
</a>
|
|
146
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
147
|
+
<a href="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/pcai.png">
|
|
148
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/pcai.png" alt="Pioneer Centre for Artificial Intelligence" height="75">
|
|
149
|
+
</a>
|
|
150
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
151
|
+
<a href="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/dtu.png">
|
|
152
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/dtu.png" alt="Technical University of Denmark" height="75">
|
|
153
|
+
</a>
|
|
154
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
155
|
+
<a href="https://www.ku.dk/">
|
|
156
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/ucph.png" alt="University of Copenhagen" height="75">
|
|
157
|
+
</a>
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/logo.svg" alt="ErrorAlign Logo" width="85%"/>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="https://img.shields.io/badge/python-%203.11%20|%203.12-green" alt="Python Versions">
|
|
7
|
+
<img src="https://img.shields.io/codecov/c/github/borgholt/error-align/main.svg?style=flat-square" alt="Coverage">
|
|
8
|
+
<img src="https://github.com/borgholt/error-align/actions/workflows/lint.yml/badge.svg?branch=main" alt="Linting" style="margin-left:5px;">
|
|
9
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="License">
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<br/>
|
|
13
|
+
|
|
14
|
+
**Text-to-text alignment algorithm for speech recognition error analysis.** ErrorAlign helps you dig deeper into your speech recognition projects by accurately aligning each word in a reference transcript with the model-generated transcript. Unlike traditional methods, such as Levenshtein-based alignment, it is not restricted to simple one-to-one alignment, but can map a single reference word to multiple words or subwords in the model output. This enables quick and reliable identification of error patterns in rare words, names, or domain-specific terms that matter most for your application.
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
__Contents__ | [Installation](#installation) | [Quickstart](#quickstart) | [Work-in-Progress](#wip) | [Citation and Research](#citation) |
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
<a name="installation">
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
pip install error-align
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
<a name="quickstart">
|
|
32
|
+
|
|
33
|
+
## Quickstart
|
|
34
|
+
```python
|
|
35
|
+
from error_align import error_align
|
|
36
|
+
|
|
37
|
+
ref = "Some things are worth noting!"
|
|
38
|
+
hyp = "Something worth nothing period?"
|
|
39
|
+
|
|
40
|
+
alignments = error_align(ref, hyp)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Resulting alignments:
|
|
44
|
+
```python
|
|
45
|
+
Alignment(SUBSTITUTE: "Some" -> "Some"-),
|
|
46
|
+
Alignment(SUBSTITUTE: "things" -> -"thing"),
|
|
47
|
+
Alignment(DELETE: "are"),
|
|
48
|
+
Alignment(MATCH: "worth" == "worth"),
|
|
49
|
+
Alignment(SUBSTITUTE: "noting" -> "nothing"),
|
|
50
|
+
Alignment(INSERT: "period")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
<a name="wip">
|
|
54
|
+
|
|
55
|
+
## Work-in-Progress
|
|
56
|
+
|
|
57
|
+
- Optimization for longform text.
|
|
58
|
+
- Efficient word-level first-pass.
|
|
59
|
+
- C++ version with Python bindings.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
<a name="citation">
|
|
64
|
+
|
|
65
|
+
## Citation and Research
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
@article{borgholt2021alignment,
|
|
69
|
+
title={A Text-To-Text Alignment Algorithm for Better Evaluation of Modern Speech Recognition Systems},
|
|
70
|
+
author={Borgholt, Lasse and Havtorn, Jakob and Igel, Christian and Maal{\o}e, Lars and Tan, Zheng-Hua},
|
|
71
|
+
journal={arXiv preprint arXiv:2509.24478},
|
|
72
|
+
year={2025}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
__To reproduce results from the paper:__
|
|
77
|
+
- Install with extra evaluation dependencies:
|
|
78
|
+
- `pip install error-align[evaluation]`
|
|
79
|
+
- Clone this repository:
|
|
80
|
+
- `git clone https://github.com/borgholt/error-align.git`
|
|
81
|
+
- Navigate to the evaluation directory:
|
|
82
|
+
- `cd error-align/evaluation`
|
|
83
|
+
- Transcribe a dataset for evaluation. For example:
|
|
84
|
+
- `python transcribe_dataset.py --model_name whisper --dataset_name commonvoice --language_code fr`
|
|
85
|
+
- Run evaluation script on the output file. For example:
|
|
86
|
+
- `python evaluate_dataset.py --transcript_file transcribed_data/whisper_commonvoice_test_fr.parquet`
|
|
87
|
+
|
|
88
|
+
__Notes:__
|
|
89
|
+
- To reproduce results on the `primock57` dataset, first run: `python prepare_primock57.py`.
|
|
90
|
+
- Use the `--help` flag to see all available options for `transcribe_dataset.py` and `evaluate_dataset.py`.
|
|
91
|
+
- All results reported in the paper are based on the test sets.
|
|
92
|
+
|
|
93
|
+
__Collaborators:__
|
|
94
|
+
|
|
95
|
+
<br/>
|
|
96
|
+
|
|
97
|
+
<div>
|
|
98
|
+
<a href="https://corti.ai">
|
|
99
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/corti.png" alt="Corti" height="75">
|
|
100
|
+
</a>
|
|
101
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
102
|
+
<a href="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/aau.png">
|
|
103
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/aau.png" alt="Aalborg University" height="75">
|
|
104
|
+
</a>
|
|
105
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
106
|
+
<a href="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/pcai.png">
|
|
107
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/pcai.png" alt="Pioneer Centre for Artificial Intelligence" height="75">
|
|
108
|
+
</a>
|
|
109
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
110
|
+
<a href="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/dtu.png">
|
|
111
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/dtu.png" alt="Technical University of Denmark" height="75">
|
|
112
|
+
</a>
|
|
113
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/empty.png" alt="" width="30">
|
|
114
|
+
<a href="https://www.ku.dk/">
|
|
115
|
+
<img src="https://raw.githubusercontent.com/borgholt/error-align/refs/heads/main/.github/assets/ucph.png" alt="University of Copenhagen" height="75">
|
|
116
|
+
</a>
|
|
117
|
+
</div>
|
|
118
|
+
|
|
119
|
+
---
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "error-align"
|
|
3
|
+
version = "0.1.0b1"
|
|
4
|
+
description = "Text-to-text alignment algorithm for speech recognition error analysis."
|
|
5
|
+
authors = ["Lasse Borgholt <lb@corti.ai>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
keywords = ["speech-recognition", "text-alignment", "NLP", "evaluation"]
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Programming Language :: Python :: 3",
|
|
10
|
+
"Programming Language :: Python :: 3.11",
|
|
11
|
+
"Programming Language :: Python :: 3.12",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Topic :: Software Development :: Libraries",
|
|
15
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
16
|
+
"Topic :: Text Processing :: Linguistic",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Intended Audience :: Science/Research",
|
|
19
|
+
]
|
|
20
|
+
readme = "README.md"
|
|
21
|
+
|
|
22
|
+
packages = [
|
|
23
|
+
{ include = 'error_align', from = 'src' },
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[tool.poetry.dependencies]
|
|
27
|
+
|
|
28
|
+
# Core dependencies
|
|
29
|
+
python = ">=3.11,<3.13"
|
|
30
|
+
regex = "^2024.11.6"
|
|
31
|
+
tqdm = "^4.67.1"
|
|
32
|
+
unidecode = "^1.4.0"
|
|
33
|
+
|
|
34
|
+
# Extra dependencies
|
|
35
|
+
rapidfuzz = { version = "^3.13.0", optional = true }
|
|
36
|
+
num2words = { version = "^0.5.14", optional = true }
|
|
37
|
+
datasets = { version = "^3.3.2", optional = true }
|
|
38
|
+
soundfile = { version = "^0.13.1", optional = true }
|
|
39
|
+
librosa = { version = "^0.11.0", optional = true }
|
|
40
|
+
click = { version = "^8.2.1", optional = true }
|
|
41
|
+
pandas = { version = "^2.3.1", optional = true }
|
|
42
|
+
scipy = { version = "^1.16.1", optional = true }
|
|
43
|
+
textgrid = { version = "^1.6.1", optional = true }
|
|
44
|
+
nemo-toolkit = {extras = ["all"], version = "^2.4.0", optional = true}
|
|
45
|
+
transformers = { version = "^4.51.3", optional = true }
|
|
46
|
+
backoff = { version = "^2.2.1", optional = true }
|
|
47
|
+
gitpython = { version = "^3.1.45", optional = true }
|
|
48
|
+
pyphen = { version = "^0.17.2", optional = true }
|
|
49
|
+
matplotlib = { version = "^3.10.6", optional = true }
|
|
50
|
+
numpy = { version = ">=1.22,<2.0", optional = true }
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
[tool.poetry.group.dev]
|
|
54
|
+
optional = true
|
|
55
|
+
|
|
56
|
+
[tool.poetry.group.dev.dependencies]
|
|
57
|
+
ipython = "9.3.0"
|
|
58
|
+
ruff = ">=0.1,<0.13"
|
|
59
|
+
pre-commit = "^4.2.0"
|
|
60
|
+
build = "^1.3.0"
|
|
61
|
+
twine = "^6.2.0"
|
|
62
|
+
|
|
63
|
+
[tool.poetry.group.test]
|
|
64
|
+
optional = true
|
|
65
|
+
|
|
66
|
+
[tool.poetry.group.test.dependencies]
|
|
67
|
+
pytest = ">=7.4.3,<9.0.0"
|
|
68
|
+
pytest-cov = "^6.1.1"
|
|
69
|
+
typeguard = "^4.4.4"
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
[tool.poetry.extras]
|
|
73
|
+
evaluation = [
|
|
74
|
+
"rapidfuzz",
|
|
75
|
+
"num2words",
|
|
76
|
+
"datasets",
|
|
77
|
+
"soundfile",
|
|
78
|
+
"librosa",
|
|
79
|
+
"click",
|
|
80
|
+
"pandas",
|
|
81
|
+
"scipy",
|
|
82
|
+
"textgrid",
|
|
83
|
+
"nemo-toolkit",
|
|
84
|
+
"transformers",
|
|
85
|
+
"backoff",
|
|
86
|
+
"gitpython",
|
|
87
|
+
"pyphen",
|
|
88
|
+
"matplotlib",
|
|
89
|
+
"numpy",
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
[build-system]
|
|
94
|
+
requires = ["poetry-core"]
|
|
95
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import random
|
|
2
|
+
from collections import Counter
|
|
3
|
+
|
|
4
|
+
from error_align.utils import OP_TYPE_COMBO_MAP, OpType
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Node:
|
|
8
|
+
"""Node in the backtrace graph corresponding to the index (i, j) in the backtrace matrix."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, hyp_idx, ref_idx) -> None:
|
|
11
|
+
"""Initialize the node at index (i, j)."""
|
|
12
|
+
self.hyp_idx = hyp_idx
|
|
13
|
+
self.ref_idx = ref_idx
|
|
14
|
+
self.children = {}
|
|
15
|
+
self.parents = {}
|
|
16
|
+
|
|
17
|
+
# Used to count the number of paths going through this node.
|
|
18
|
+
self._bwd_node_count = 0
|
|
19
|
+
self._fwd_node_count = 0
|
|
20
|
+
|
|
21
|
+
# Used to store the operations that lead to this node.
|
|
22
|
+
self._ingoing_edge_counts = {}
|
|
23
|
+
self._outgoing_edge_counts = {}
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def index(self):
|
|
27
|
+
return (self.hyp_idx, self.ref_idx)
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def offset_index(self):
|
|
31
|
+
"""Get the offset index of the node so indices match the hypothesis and reference strings.
|
|
32
|
+
|
|
33
|
+
Root will be at (-1, -1).
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
tuple[int, int]: The offset index of the node in the backtrace matrix.
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
return (self.hyp_idx - 1, self.ref_idx - 1)
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def number_of_paths(self):
|
|
43
|
+
return self._bwd_node_count * self._fwd_node_count
|
|
44
|
+
|
|
45
|
+
def number_of_ingoing_paths_via(self, op_type: OpType):
|
|
46
|
+
"""Get the number of paths going through this node via the given operation type.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
op_type (OpType): The operation type to check.
|
|
50
|
+
|
|
51
|
+
"""
|
|
52
|
+
if op_type not in self.parents:
|
|
53
|
+
return 0
|
|
54
|
+
return self._ingoing_edge_counts[op_type] * self.parents[op_type]._outgoing_edge_counts[op_type]
|
|
55
|
+
|
|
56
|
+
def number_of_outgoing_paths_via(self, op_type: OpType):
|
|
57
|
+
"""Get the number of paths going through this node via the given operation type.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
op_type (OpType): The operation type to check.
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
if op_type not in self.children:
|
|
64
|
+
return 0
|
|
65
|
+
return self._outgoing_edge_counts[op_type] * self.children[op_type]._ingoing_edge_counts[op_type]
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def is_terminal(self):
|
|
69
|
+
"""Check if the node is a terminal node (i.e., it has no children)."""
|
|
70
|
+
return len(self.children) == 0
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def is_root(self):
|
|
74
|
+
"""Check if the node is a root node (i.e., it has no parents)."""
|
|
75
|
+
return len(self.parents) == 0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class BacktraceGraph:
|
|
79
|
+
"""Backtrace alignment graph."""
|
|
80
|
+
|
|
81
|
+
def __init__(self, backtrace_matrix: list[list[int]]) -> None:
|
|
82
|
+
"""Create a graph from the backtrace matrix."""
|
|
83
|
+
self.hyp_dim = len(backtrace_matrix)
|
|
84
|
+
self.ref_dim = len(backtrace_matrix[0])
|
|
85
|
+
self.hyp_max_idx = self.hyp_dim - 1
|
|
86
|
+
self.ref_max_idx = self.ref_dim - 1
|
|
87
|
+
self.backtrace_matrix = backtrace_matrix
|
|
88
|
+
|
|
89
|
+
self._nodes = None
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def nodes(self) -> dict[tuple[int, int], Node]:
|
|
93
|
+
"""Get the nodes in the graph.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
dict: A dictionary of nodes indexed by their (hyp_idx, ref_idx).
|
|
97
|
+
|
|
98
|
+
"""
|
|
99
|
+
if self._nodes is None:
|
|
100
|
+
terminal_node = Node(self.hyp_max_idx, self.ref_max_idx)
|
|
101
|
+
self._nodes = {terminal_node.index: terminal_node}
|
|
102
|
+
|
|
103
|
+
# Traverse nodes in reverse topological order to add parents.
|
|
104
|
+
for index in self._iter_topological_order(reverse=True):
|
|
105
|
+
if index in self._nodes and index != (0, 0):
|
|
106
|
+
self._add_parents_from_backtrace(index)
|
|
107
|
+
|
|
108
|
+
# Sort nodes by their indices to ensure topological order.
|
|
109
|
+
self._nodes = dict(sorted(self._nodes.items(), key=lambda item: (item[0][0], item[0][1])))
|
|
110
|
+
self._set_path_and_node_counts() # TODO: Consider to do this lazily. Shouldn't be too expensive.
|
|
111
|
+
|
|
112
|
+
return self._nodes
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def number_of_paths(self):
|
|
116
|
+
"""Count the number of paths in the graph.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
int: The number of paths.
|
|
120
|
+
|
|
121
|
+
"""
|
|
122
|
+
return self.get_node(0, 0)._bwd_node_count
|
|
123
|
+
|
|
124
|
+
def get_node(self, hyp_idx, ref_idx):
|
|
125
|
+
"""Get the node at the given index.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
hyp_idx (int): Hyp/row index.
|
|
129
|
+
ref_idx (int): Ref/column index.
|
|
130
|
+
|
|
131
|
+
"""
|
|
132
|
+
return self.nodes[(hyp_idx, ref_idx)]
|
|
133
|
+
|
|
134
|
+
def get_node_set(self):
|
|
135
|
+
"""Get the set of all node indices in the graph.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
set: A set of all node indices.
|
|
139
|
+
|
|
140
|
+
"""
|
|
141
|
+
transitions = set()
|
|
142
|
+
for node in self.nodes.values():
|
|
143
|
+
transitions.add(node.offset_index)
|
|
144
|
+
return transitions
|
|
145
|
+
|
|
146
|
+
def get_path(self, sample=False):
|
|
147
|
+
"""Get a path through the graph.
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
sample (bool): If True, sample a path randomly based on the transition probabilities. Otherwise, return
|
|
151
|
+
the first path deterministically.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
list[Node]: A list of nodes representing the path.
|
|
155
|
+
|
|
156
|
+
"""
|
|
157
|
+
node = self.get_node(0, 0)
|
|
158
|
+
assert node.is_root, "The node at (-1, -1) was expected to be a root node."
|
|
159
|
+
|
|
160
|
+
path = []
|
|
161
|
+
while not node.is_terminal:
|
|
162
|
+
if sample:
|
|
163
|
+
op_type = random.choice(list(node.children.keys()))
|
|
164
|
+
else:
|
|
165
|
+
op_type = next(iter(node.children.keys()))
|
|
166
|
+
node = node.children[op_type]
|
|
167
|
+
path.append((op_type, node))
|
|
168
|
+
|
|
169
|
+
return path
|
|
170
|
+
|
|
171
|
+
def get_unambiguous_matches(self, ref):
|
|
172
|
+
"""Get word spans that are unambiguously matched (i.e., only one path in backtrace graph).
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
list[Node]: A list of nodes representing the unambiguous path.
|
|
176
|
+
|
|
177
|
+
"""
|
|
178
|
+
ref = "*" + ref # Index offset
|
|
179
|
+
mono_match_end_nodes = set()
|
|
180
|
+
ref_idxs = Counter()
|
|
181
|
+
hyp_idxs = Counter()
|
|
182
|
+
for (hyp_idx, ref_idx), node in self.nodes.items():
|
|
183
|
+
if OpType.MATCH in node.parents and ref[ref_idx] == "<":
|
|
184
|
+
_ref_idx, _hyp_idx = ref_idx + 1, hyp_idx + 1
|
|
185
|
+
while True:
|
|
186
|
+
if (_hyp_idx, _ref_idx) not in self.nodes:
|
|
187
|
+
break
|
|
188
|
+
if OpType.MATCH not in self.nodes[(_hyp_idx, _ref_idx)].parents:
|
|
189
|
+
break
|
|
190
|
+
if ref[_ref_idx] == ">":
|
|
191
|
+
end_index = (_hyp_idx, _ref_idx)
|
|
192
|
+
mono_match_end_nodes.add(end_index)
|
|
193
|
+
ref_idxs[_ref_idx] += 1
|
|
194
|
+
hyp_idxs[_hyp_idx] += 1
|
|
195
|
+
break
|
|
196
|
+
_ref_idx, _hyp_idx = _ref_idx + 1, _hyp_idx + 1
|
|
197
|
+
|
|
198
|
+
return {(h - 1, r - 1) for h, r in mono_match_end_nodes if hyp_idxs[h] == 1 and ref_idxs[r] == 1}
|
|
199
|
+
|
|
200
|
+
def _parent_index_from_op_type(self, hyp_idx, ref_idx, op_type):
|
|
201
|
+
"""Create a parent node based on the index of the current node and the operation type."""
|
|
202
|
+
hyp_idx = hyp_idx - 1 if op_type != OpType.DELETE else hyp_idx
|
|
203
|
+
ref_idx = ref_idx - 1 if op_type != OpType.INSERT else ref_idx
|
|
204
|
+
if (hyp_idx, ref_idx) not in self._nodes:
|
|
205
|
+
self._nodes[(hyp_idx, ref_idx)] = Node(hyp_idx, ref_idx)
|
|
206
|
+
return self._nodes[(hyp_idx, ref_idx)]
|
|
207
|
+
|
|
208
|
+
def _iter_topological_order(self, reverse=False):
|
|
209
|
+
"""Iterate through the nodes in topological order."""
|
|
210
|
+
if reversed:
|
|
211
|
+
for i in reversed(range(self.hyp_dim)):
|
|
212
|
+
for j in reversed(range(self.ref_dim)):
|
|
213
|
+
yield (i, j)
|
|
214
|
+
else:
|
|
215
|
+
for i in range(self.hyp_dim):
|
|
216
|
+
for j in range(self.ref_dim):
|
|
217
|
+
yield (i, j)
|
|
218
|
+
|
|
219
|
+
def _add_parents_from_backtrace(self, index):
|
|
220
|
+
"""Add parents to the node at the given index based on the backtrace matrix."""
|
|
221
|
+
node = self._nodes.get(index, None)
|
|
222
|
+
|
|
223
|
+
assert node is not None, f"Node at index {index} does not exist in the graph."
|
|
224
|
+
|
|
225
|
+
op_type_combo_code = self.backtrace_matrix[node.hyp_idx][node.ref_idx]
|
|
226
|
+
op_type_combo = OP_TYPE_COMBO_MAP[op_type_combo_code]
|
|
227
|
+
|
|
228
|
+
for op_type in op_type_combo:
|
|
229
|
+
parent_node = self._parent_index_from_op_type(*node.index, op_type)
|
|
230
|
+
node.parents[op_type] = parent_node
|
|
231
|
+
parent_node.children[op_type] = node
|
|
232
|
+
|
|
233
|
+
def _set_path_and_node_counts(self):
|
|
234
|
+
"""Count the number of paths going through any node in the graph using the forward-backward algorithm."""
|
|
235
|
+
ordered_nodes = list(self.nodes.values())
|
|
236
|
+
root_node = ordered_nodes[0]
|
|
237
|
+
terminal_node = ordered_nodes[-1]
|
|
238
|
+
assert root_node.is_root, "The first node must be a root node."
|
|
239
|
+
assert terminal_node.is_terminal, "The last node must be a terminal node."
|
|
240
|
+
|
|
241
|
+
# Forward pass
|
|
242
|
+
ordered_nodes[0]._fwd_node_count = 1
|
|
243
|
+
for node in ordered_nodes[1:]:
|
|
244
|
+
for op_type, parent in node.parents.items():
|
|
245
|
+
node._fwd_node_count += parent._fwd_node_count
|
|
246
|
+
node._ingoing_edge_counts[op_type] = parent._fwd_node_count
|
|
247
|
+
|
|
248
|
+
# Backward pass
|
|
249
|
+
ordered_nodes[-1]._bwd_node_count = 1
|
|
250
|
+
for node in reversed(ordered_nodes[:-1]):
|
|
251
|
+
for op_type, child in node.children.items():
|
|
252
|
+
node._bwd_node_count += child._bwd_node_count
|
|
253
|
+
node._outgoing_edge_counts[op_type] = child._bwd_node_count
|
|
254
|
+
|
|
255
|
+
# Validate that the number of forward and backward paths are equal
|
|
256
|
+
assert root_node._bwd_node_count == terminal_node._fwd_node_count, (
|
|
257
|
+
"The number of forward and backward paths must be equal."
|
|
258
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
try:
|
|
3
|
+
from error_align.baselines.optimal_word_alignment import OptimalWordAlign as OptimalWordAlign
|
|
4
|
+
from error_align.baselines.power_alignment import PowerAlign as PowerAlign
|
|
5
|
+
from error_align.baselines.rapidfuzz_word_alignment import RapidFuzzWordAlign as RapidFuzzWordAlign
|
|
6
|
+
except ImportError:
|
|
7
|
+
raise ImportError(
|
|
8
|
+
"Baselines module could not be imported. Please ensure all dependencies are installed. "
|
|
9
|
+
"You can install the evaluation dependencies using: pip install error-align[evaluation]"
|
|
10
|
+
)
|