Download runtime/asr_consensus.py from Hansda/tiny-dictation-en: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/Hansda/tiny-dictation-en/resolve/main/runtime/asr_consensus.py
- Command line
-
hf download hf://Hansda/tiny-dictation-en/runtime/asr_consensus.py
-
curl -L -o asr_consensus.py https://huggingface.co/Hansda/tiny-dictation-en/resolve/main/runtime/asr_consensus.py
1.2 kB
| 'Combine independently decoded transcripts with word alignment and weighted voting.' | |
| def aligned_words(anchor, alternative): | |
| """Align a transcript to anchor words and return votes for words and gaps.""" | |
| distance = [list(range(len(alternative) + 1))] | |
| for i, left in enumerate(anchor, 1): | |
| row = [i] | |
| for j, right in enumerate(alternative, 1): | |
| row.append(min( | |
| distance[-1][j] + 1, row[-1] + 1, | |
| distance[-1][j - 1] + (left != right), | |
| )) | |
| distance.append(row) | |
| words = [""] * len(anchor) | |
| gaps = [[] for _ in range(len(anchor) + 1)] | |
| i, j = len(anchor), len(alternative) | |
| while i or j: | |
| if i and j and distance[i][j] == distance[i - 1][j - 1] + (anchor[i - 1] != alternative[j - 1]): | |
| words[i - 1] = alternative[j - 1] | |
| i, j = i - 1, j - 1 | |
| elif i and distance[i][j] == distance[i - 1][j] + 1: | |
| i -= 1 | |
| else: | |
| j -= 1 | |
| gaps[i].append(alternative[j]) | |
| slots = [] | |
| for gap, word in zip(gaps, words): | |
| slots.extend((tuple(reversed(gap)), (word,) if word else ())) | |
| return tuple(slots + [tuple(reversed(gaps[-1]))]) | |