Metadata-Version: 2.4
Name: tinyg2p
Version: 0.1.0
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Natural Language :: English
Classifier: Natural Language :: French
Classifier: Natural Language :: German
Classifier: Natural Language :: Spanish
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
Classifier: Programming Language :: Rust
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Typing :: Typed
License-File: LICENSE
Summary: Fast, small and accurate multilingual grapheme-to-phoneme conversion (text to IPA) with a compiled core and no dependencies
Keywords: g2p,grapheme-to-phoneme,phonemizer,ipa,text-to-speech,tts,pronunciation
Author: Lars Nieradzik
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/lars76/tinyg2p
Project-URL: Issues, https://github.com/lars76/tinyg2p/issues
Project-URL: Source, https://github.com/lars76/tinyg2p

# tinyg2p

Fast, small and accurate multilingual grapheme-to-phoneme conversion: text to IPA. A compiled Rust core with no Python
dependencies. Each language is one conditional random field over the letters of a word. It picks the stressed vowel and
the phones of every letter in one joint decision. It is more accurate than the other systems we tested.

## Install

```sh
pip install tinyg2p
```

Python 3.10 or newer, also the free-threaded builds 3.14t and newer. Wheels exist for Linux (x86_64 and aarch64, glibc
and musl), macOS (Apple silicon, Intel) and Windows (x64, arm64). On other platforms pip builds from source, which needs
a Rust toolchain 1.88 or newer.

## Use

```python
from tinyg2p import G2P

G2P("de-DE").ipa("Die Bundesregierung plant 2025 neue Gesetze.")
# 'diː ˈbʊndəsʁeɡiːʁʊŋ ˈplaːnt 2025 ˈnɔʏə ɡəˈzɛtsə.'
G2P("en-US").ipa("The CEO bought an old guitar in the USA.")
# 'ðə siːiːˈoʊ ˈbɔt ən ˈoʊld ɡɪˈtɑɹ ɪn ðə juːɛsˈeɪ.'
G2P("fr-FR").ipa("Les enfants ont un grand appartement.")
# 'lez ɑ̃fɑ̃ ɔ̃ ɛ̃ ɡʁɑ̃t apaʁtəmɑ̃.'
G2P("es-ES").ipa("La Organización Mundial de la Salud publicó el informe.")
# 'la oɾɡaniθaˈθjon munˈdjal de la saˈlud publiˈko el inˈfoɾme.'

G2P("de-DE").read("Das Krankenhaus ist groß.")
# [Word(start=0, end=3, ipa='das', phones=('d', 'a', 's')),
#  Word(start=4, end=15, ipa='ˈkʁaŋkənhaʊs', phones=('ˈ', 'k', 'ʁ', ...)), ...]
```

`ipa` returns the text with each word replaced by its phones. `ˈ` marks the stressed syllable. Numbers, punctuation
and words that cannot be read stay as written. French output has no `ˈ`: French stress falls on the last vowel of a
phrase, not of a word.

`read` returns one `Word` per word read. It holds the character offsets in the text (`start`, `end`) and the phones as a
tuple (`phones`), one item per sound. `ˈ` is an item of its own. A German or English diphthong is one item (`aɪ`). A
Spanish diphthong is a vowel and a glide (`a`, `j`). An affricate (`tʃ`, `ts`, `pf`) is one item, except in French,
where it is two (`t`, `ʃ`).

## Languages

| Tag | Variety | Model | Exception words | Load |
|---|---|---|---|---|
| `de-DE` | Standard German | 276 kB | 2,561 | 21 ms |
| `en-US` | General American | 290 kB | 2,318 | 21 ms |
| `fr-FR` | Parisian standard | 65 kB | 1,514 | 5 ms |
| `es-ES` | Castilian Spanish | 16 kB | 1,149 | 1.5 ms |

Load is the time of the first `G2P(lang)` in a new process, the median of 25 runs on an AMD Ryzen 9 8945HS.

## Accuracy

Words read wrong per 100 bench words, the best per language in bold. A word is wrong when its phones or its stressed
syllable differ from the gold reading. Some systems mark stress on fewer than a quarter of the words. For them only the
phones are scored: DeepPhonemizer in English, gruut and epitran in German and Spanish, CharsiuG2P in Spanish, and all
systems in French. In French, a final z, t, n, p or ə that the gold lacks is not counted. It is a liaison or a schwa
that the gold leaves out.

| | de-DE | en-US | fr-FR | es-ES |
|---|---|---|---|---|
| tinyg2p | **2.27** | **1.35** | **1.17** | **1.00** |
| DeepPhonemizer |  | 3.46 |  |  |
| misaki |  | 3.99 |  |  |
| g2p_en |  | 5.30 |  |  |
| espeak-ng | 14.39 | 6.78 | 4.60 | 1.74 |
| gruut | 25.21 | 6.12 | 4.40 | 1.90 |
| CharsiuG2P | 33.43 | 12.79 | 7.02 | 11.00 |
| epitran | 38.72 |  | 32.86 | 10.95 |

The bench has 502 sentences per language. 250 are drawn at random from the Leipzig Corpora Collection (CC BY 4.0):
news in German, French and Spanish, Wikipedia in English. The other 252 are 21 from each of 12 Wikipedia articles of
that language (CC BY-SA 4.0): diabetes, photosynthesis, compiler, contract, bread, volcano, jazz, black hole, vaccine,
inflation, operating system, hospital. The other systems ran with the versions locked in `benchmark/systems/` and with
the espeak-ng library of the system. The bench and its scripts are in
[`benchmark/`](https://github.com/lars76/tinyg2p/tree/main/benchmark), where you can also score your own system.

## Speed

Bench words per second on one thread, model loaded, empty cache, the fastest per language in bold. DeepPhonemizer,
CharsiuG2P and epitran read each word alone, the others one sentence per call. tinyg2p caches every word it has read and
uses all cores on long texts. A call empties the cache first when it holds more than 500,000 words. One call reads the
English bench repeated to 20 million words in 1.6 s, most of them cache hits. Measured on an AMD Ryzen 9 8945HS.

| | de-DE | en-US | fr-FR | es-ES |
|---|---|---|---|---|
| tinyg2p | 96k | **154k** | 184k | 237k |
| DeepPhonemizer |  | 3k |  |  |
| misaki |  | 8k |  |  |
| g2p_en |  | 16k |  |  |
| espeak-ng | **178k** | 139k | **189k** | **459k** |
| gruut | 25 | 124 | 137 | 26 |
| CharsiuG2P | 18 | 22 | 24 | 28 |
| epitran | 28k |  | 36k | 123k |

## License

MIT, see [LICENSE](https://github.com/lars76/tinyg2p/blob/main/LICENSE).


