BCP-47 language tags: standardization, distance, closest match, and the default region of a bare tag
Maintained. Last release 3 days ago, commits in 5 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install linguatagREADME as published on PyPI with version 0.22.1a1.
linguatag
linguatag reads BCP 47 language tags. It puts a tag in one canonical form and gives a bare tag its default region and its likely script. It also measures how near two tags are. It picks the best tag from a list of available tags. The package carries all its data: CLDR 49, the IANA Language Subtag Registry, and a cited genealogy of languages and dialects.
Try it in the browser: linguatag on Hugging Face.
from linguatag import closest_lang, lang_distance, likely_script, standardize_lang, with_default_region
print(standardize_lang("en_us")) # en-US
print(with_default_region("pt")) # pt-PT
print(likely_script("zh-TW")) # Hant
print(lang_distance("pt", "pt-BR")) # 5
print(closest_lang("en-AU", ["fr-FR", "en-US"])) # en-US
print(closest_lang("de", ["fr", "it"])) # None
A bare tag means its motherland: pt is pt-PT and en is en-GB. closest_lang returns the candidate string as you gave it, or None when no candidate is near enough.
Where this fits
Alone: tag normalisation and language matching for any program, with from linguatag import closest_lang.
In the stack: the base library for language tags. text2spoken, scriptconv, phoonnx, orthography2ipa and ovos-spec-tools call it to pick a resource for a requested language. It calls no other stack library.
Through scriptconv: uv pip install --prerelease=allow "scriptconv[phonemizers]".
Install
uv pip install --prerelease=allow linguatag
linguatag needs Python 3.11 or later and has no dependencies. The langcodes extra is empty. It installs nothing, and it exists so that a pin on linguatag[langcodes] resolves.
Documentation
| Page | What it covers |
|---|---|
| Getting started | A walk-through of the main calls, with real output |
| Parsing and normalising tags |