Metadata-Version: 2.4
Name: cawn-etalon
Version: 0.1.3
Summary: Where every word and every letter of a known text begins and ends in its sound. No model, no training: numpy only.
License: Apache-2.0
Project-URL: Homepage, https://cawnai.com/blog/etalon
Keywords: forced alignment,speech,Serbian,Croatian,subtitles
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: numpy
Requires-Dist: num2words<0.6,>=0.5.14
Provides-Extra: hf
Requires-Dist: huggingface_hub; extra == "hf"
Requires-Dist: pyarrow; extra == "hf"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: black; extra == "dev"
Dynamic: license-file

<p align="center">
  <img src="https://raw.githubusercontent.com/DeanChugall/etalon/main/assets/etalon_banner.png?v=0.1.3" alt="Etalon: where every word and every letter of a known text begins and ends in its sound" width="100%">
</p>

<h4 align="center">
  <a href="https://cawnai.com/blog/etalon" target="_blank">How it works</a> |
  <a href="https://huggingface.co/spaces/datatab/etalon-demo" target="_blank">Live demo</a> |
  <a href="https://pypi.org/project/cawn-etalon/" target="_blank">PyPI</a>
</h4>

<p align="center">
  <a href="https://pypi.org/project/cawn-etalon/" target="_blank"><img src="https://img.shields.io/pypi/v/cawn-etalon.svg?style=flat-square&color=d3a64e" alt="PyPI version"></a>
  <a href="https://pypi.org/project/cawn-etalon/" target="_blank"><img src="https://img.shields.io/badge/python-3.9%2B-3c4552?style=flat-square" alt="Python 3.9 or newer"></a>
  <a href="https://github.com/DeanChugall/etalon/blob/main/LICENSE"><img src="https://img.shields.io/badge/code-Apache%202.0-3c4552?style=flat-square" alt="Code licence: Apache 2.0"></a>
  <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank"><img src="https://img.shields.io/badge/bank-CC%20BY--SA%204.0-3c4552?style=flat-square" alt="Bank licence: CC BY-SA 4.0"></a>
  <img src="https://img.shields.io/badge/languages-Serbian%20%7C%20Croatian-69b8a4?style=flat-square" alt="Languages: Serbian, Croatian">
  <img src="https://img.shields.io/badge/needs-numpy%20only-69b8a4?style=flat-square" alt="Needs numpy only">
  <a href="https://huggingface.co/spaces/datatab/etalon-demo" target="_blank"><img src="https://img.shields.io/badge/demo-Hugging%20Face-d3a64e?style=flat-square" alt="Demo on Hugging Face"></a>
</p>

Etalon finds where every word and every letter of a known text begins and ends in a recording of
it (forced alignment). It uses no neural model and needs no training and no graphics card: the
whole method is a few hundred lines of numpy and a small bank of sounds that ships with the
package.

- Serbian and Croatian, measured. Bosnian and Montenegrin work the same way and are not measured yet.
- Between half a minute and two minutes of work on a desktop processor for an hour of sound.
- A recording and its plain text are enough; no times, no subtitles and no cutting are needed.
- Works on one recording, or on a whole dataset read as a stream from Hugging Face.
- From the command line, from Python, or on a page in the browser.

The method is described in [Etalon](https://cawnai.com/blog/etalon).

## Install

```
pip install cawn-etalon              # one recording at a time
pip install "cawn-etalon[hf]"        # also whole datasets (adds huggingface_hub and pyarrow)
```

The command and the Python module are both called `etalon`.

Python 3.9 or newer. A plain 16 kHz mono `.wav` is read directly; any other sound needs `ffmpeg`
on the path.

## Use

### In the browser

```
etalon ui
```

A page opens on this computer (nothing is sent anywhere). Drop a recording, paste its text, and
hear the result with the word that sounds lit. The same page lays whole datasets and makes banks
for other languages, and shows the log of everything it works on.

### Command line

```
etalon recording.wav text.txt                       # -> recording.srt, recording.json
etalon recording.mp3 text.txt --format srt,vtt,json,tsv,textgrid,html --out folder/name
etalon recording.wav subtitles.srt                  # only the text of the subtitles is used
```

`--format html` writes a page to hear the result. On it, red words are the tenth of the words
that fit their sound worst and yellow the next 15 %. These marks are relative: a tenth of the
words is always red, in the best result too. They say where to listen first, not that a word is
wrong.

### Python

```python
import etalon

result = etalon.align("recording.wav", "text.txt", lang="sr")
for word in result["words"]:
    print(word["word"], word["start"], word["end"])     # seconds
etalon.save(result, "name", ("srt", "json"))
```

Every word also has its `letters` (letter, start, end), a `mark` (higher fits better) and a
`doubt` (0, 1 or 2). `result["fit"]` and `result["match"]` say whether the text belongs to the sound.

### A whole dataset

Every row a sound and its text. A Hugging Face repository is read as a stream, so nothing is
downloaded whole; a folder of parquet files on disk works too.

```
etalon dataset OWNER/NAME                                # shows the columns it has
etalon dataset OWNER/NAME --audio_column audio --text_column text --limit 100 --html 20
etalon dataset OWNER/NAME --audio_column audio --text_column text --speaker_column speaker --push OWNER/NEW
```

The rows are written out as parquet with new columns: `words`, `letters`, `etalon_slide`,
`etalon_mark`, `etalon_ok`. `--html 20` adds a page to hear the first 20 rows. `--push` sends the
result to a new repository, which is made private; its licence is yours to set.

## Languages

| `--lang` | language | bank of sounds | measured |
|---|---|---|---|
| `sr` | Serbian | its own (Serbian parliament) | yes |
| `hr` | Croatian | the Serbian one | yes |
| `bs`, `me` | Bosnian, Montenegrin | the Serbian one | not yet |
| any other code | a language said the way it is written | made by you, once (below) | no |

A bank for another language is made once, from a dataset that already has the times of its
letters (a list of `{letter, start, end}` per row, from another aligner). After that Etalon lays
that language by itself:

```
etalon bank OWNER/NAME --lang sl --audio_column audio --letters_column letters --speaker_column speaker
etalon recording.wav text.txt --lang sl
```

The bank is kept in `~/.etalon/bank_sl.pkl`. With `--speaker_column` it is made in two rounds,
every speaker moved to a standard voice first, as the Serbian one was; without it in one round,
and it is blunter.

Making a bank cannot quietly go wrong:

- The code must be your own. `sr`, `hr`, `bs` and `me` come with Etalon and are refused: a bank
  made under one of them would never be used.
- A bank that is already there is not replaced unless you add `--overwrite`; the old one is then
  kept beside the new one as `bank_sl.pkl.bak`.
- Letter times that do not fit the sound (milliseconds instead of seconds, or counted from the
  start of the whole recording) stop the work before it has begun.
- When the bank is made it is tried on rows that were kept out of it, and you are told how many
  letter starts it finds within 50 ms of the dataset's own times. This says whether the bank was
  made from sound data. It does not say how right the dataset's times are: Etalon can only be as
  good as the aligner it learned from.

```
etalon banks                 # every bank on this computer: what it is for, how it was made, its check, its file
etalon banks remove sl       # take a bank you made away (it asks first; --yes does not)
```

The bank that comes with Etalon cannot be removed or replaced. The same list, with a Remove
button, is on the page `etalon ui` under "Language bank".

Numbers written in digits are read out: by Etalon's own tables for the four languages above, by
the package `num2words` for the others it knows. Where neither can, write the numbers as words.

Languages written one way and said another (English, French) are beyond this method: they would
need a dictionary of pronunciations.

## Accuracy

The share of word starts that lie within 50 ms of the times of another aligner. Those times are
not the truth, only the ruler that exists.

| speech | against | within 50 ms |
|---|---|---|
| Serbian parliament, short pieces | times published with ParlaSpeech-RS | 93.8 % (MFA on the same: 93.9 %) |
| Serbian videos, short pieces, one speaker (14 videos) | Montreal Forced Aligner | 88.5 % |
| Serbian video of 90 minutes, several speakers | Montreal Forced Aligner | 87.3 % |
| Serbian, a whole hour from its plain text | Montreal Forced Aligner | 85.4 % |
| Croatian parliament, one hour, Serbian bank | times published with ParlaSpeech-HR | 81.2 % (97.1 % within 100 ms) |

## Settings

Every setting is an argument of the command; three of them can also be set as a variable of the terminal.

| argument | variable | what it does |
|---|---|---|
| `--lang CODE` | `ETALON_LANG` | the language (default `sr`) |
| `--slide MODE` | `ETALON_SLIDE` | one voice or several. `auto` (default): Etalon decides. `video`: one voice, the fastest. `piece`: always several |
| `--fit 65` | `ETALON_FIT` | the text must explain at least so many percent of the start of the sound, or a warning is given and the command exits with 2 |
| `--bank NAME` | | which bank of sounds (default `auto`) |
| `--minutes N` | | only the first N minutes of the recording |
| | `ETALON_DATA` | a folder with other banks |

## How it works

1. **Rows.** The sound becomes one row of 26 frequency bands every 10 ms. A *slide* moves the
   bands of a speaker towards a standard voice; there are 9 slides.
2. **Bank.** From speech whose letter times are known, the bank keeps the usual row of every
   half of a letter together with its neighbour.
3. **Path.** The text becomes a chain of letter halves and silences, and the chain is stretched
   over the rows of the sound so that the sum of the fits is the largest.
4. **Voices.** Pieces spread over the recording each pick the slide that fits them. If they
   agree it is one voice; if they do not, several people talk and every piece gets its own slide.

## Limits

- The text must be what is said. Abbreviations must be written the way they are spoken; numbers
  in digits are read in their basic form.
- The check "does the text belong to the sound" looks only at the first minute and a half, and
  works only where the recording is mostly speech. With street noise or music and little speech
  a wrong text can pass.
- Bosnian, Montenegrin and banks made for other languages are not measured.
- The times of word ends agree with other aligners less than the times of word starts.

## Development

```
pip install -e ".[dev]"
pytest            # needs no sound
black .           # line length 110
```

The code lies in folders by what it does:

| folder | modules | what |
|---|---|---|
| `etalon/` | `config.py`, `errors.py` | constants and file locations; the one error |
| `audio/` | `reading.py`, `features.py` | reading the sound; its rows of 26 bands, the 9 slides |
| `text/` | `words.py`, `numbers.py`, `languages.py` | words to the letters that are said, numbers, languages |
| `soundbank/` | `model.py`, `files.py` | what a bank is and how one is made; the bank files: keep, list, remove |
| `laying/` | `path.py`, `slide.py`, `align.py` | a text stretched over its sound; the slide; `align` and `align_all` |
| `output/` | `subtitles.py`, `pages.py`, `save.py` | srt, vtt, tsv, TextGrid; pages to hear; writing the files |
| `dataset/` | `files.py`, `rows.py`, `language_bank.py`, `bank_check.py` | whole datasets; making and checking the bank of a language |
| `app/` | `cli.py`, `ui.py`, `ui.html` | the command line and the page in the browser |

What is meant to be used from outside is on the package itself (`etalon.align`, `etalon.save`,
`etalon.list_banks` ...) and in `etalon.dataset` (`align_dataset`, `make_language_bank`).

## Licence

The code is under the Apache License 2.0 (see `LICENSE`).

The bank of sounds, `etalon/data/bank_parliament.pkl`, is derived from
[ParlaSpeech-RS 4.0](http://hdl.handle.net/11356/1885) (CLARIN.SI) and is shared under the same
licence, [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/).
