Registry / ai-ml / langid

langid

JSON →
library1.1.6pypypi✓ verified 22d ago

langid.py is a standalone Language Identification (LangID) tool for Python. It comes pre-trained on 97 languages and is designed for fast, domain-insensitive classification with minimal dependencies. It can be used as a simple Python library or deployed as a web service. The current version is 1.1.6, and while the original project maintains stability, a community-driven fork (`py3langid`) exists for modern Python 3 performance optimizations.

pip install langid
INSTALL
IMPORT
SIG · LANGID
L
langid
ai-mlpythonv1.1.6
Install
4.7s avg
Import
426ms
Disk
95MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.430s · 95.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.7s · import 0.422s · 92MB
95MB installed
● package 95MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

langid
import langid

The `langid.classify()` function takes a string and returns a tuple containing the predicted ISO 639-1 language code and an unnormalized log-probability estimate. For normalized probabilities (0-1), a `LanguageIdentifier` instance must be explicitly configured.

import langid text1 = "This is a sample text in English." text2 = "Ceci est un exemple de texte en français." text3 = "Dies ist ein Beispieltext auf Deutsch." print(f"'{text1}' -> {langid.classify(text1)}") print(f"'{text2}' -> {langid.classify(text2)}") print(f"'{text3}' -> {langid.classify(text3)}") # To get normalized probabilities (0-1 range): from langid.langid import LanguageIdentifier, MODEL_FILE identifier = LanguageIdentifier.from_pickled_model(MODEL_FILE, norm_probs=True) print(f"Normalized for '{text1}' -> {identifier.classify(text1)}")
langid --version
Debug
Known issues
gotchaThe `langid.classify()` function, by default, returns an unnormalized log-probability estimate, not a standard probability (0-1 range). Interpreting this value directly as a confidence score between 0 and 1 is incorrect.
fix
To obtain normalized probabilities (0-1), you must explicitly initialize a `LanguageIdentifier` instance with `norm_probs=True`. Example: `from langid.langid import LanguageIdentifier, MODEL_FILE; identifier = LanguageIdentifier.from_pickled_model(MODEL_FILE, norm_probs=True); identifier.classify(text)`.
affects: All versions
breakingThe training tools (`LDfeatureselect.py`, `train.py`) accompanying `langid.py` are explicitly stated to be Python 2-only. Attempting to use them with Python 3 will result in compatibility errors.
fix
Users needing to train custom models in modern Python environments should be aware that the original `langid` library's training tools are not Python 3 compatible. Consider using the `py3langid` fork, which has adapted training scripts (though noted as untested), or an alternative library for custom training.
affects: All versions when using training tools
gotchaFor optimal performance when processing large numbers of texts, `langid` should be used as a Python library (importing and calling `classify()`) or as a web service. Repeatedly invoking `langid.py` from the command line for each text incurs significant overhead due to repeated model loading.
fix
Initialize the `langid` module once within your application (e.g., `import langid`) and then call `langid.classify()` multiple times. For batch processing of files, the library offers a batch mode (`-b` flag) that utilizes multiprocessing.
affects: All versions
gotchaA modernized and significantly faster fork, `py3langid`, exists and is optimized for Python 3.6+. It offers substantial performance improvements (e.g., 25-30x faster model loading, 5-6x faster classification) over the original `langid` package.
fix
For new Python 3.6+ projects or performance-critical applications, consider using `py3langid`. It's designed as a drop-in replacement: `pip install py3langid` and then `import py3langid as langid`.
affects: All versions of `langid` on Python 3.6+
gotchaPassing non-string types or strings with unexpected formatting (e.g., a tuple represented as a string, or unhandled encoding issues from external sources like databases) to `langid.classify()` can lead to incorrect classifications or `KeyError` exceptions.
fix
Ensure that all input to `langid.classify()` is a clean Unicode string. Always validate and potentially preprocess inputs, especially when sourcing text from databases or other external systems, to prevent unexpected formatting or encoding issues.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'langid'
The 'langid' library has not been installed in the current Python environment or the Python interpreter cannot find it.
fix
Install the package using pip: `pip install langid`
Could not find a version that satisfies the requirement langid (from versions: ...)
This error, often encountered with older versions of pip, indicates that pip cannot find a compatible release of 'langid' on PyPI. Historically, 'langid' releases were sometimes marked as 'dev' versions, which required a specific flag for installation.
fix
First, upgrade your pip installer: `python -m pip install --upgrade pip`. Then, retry the installation: `pip install langid`. If the problem persists for very old Python environments, you might try `pip install --pre langid` (though generally not necessary for version 1.1.6).
ImportError: cannot import name classify (from langid)
This issue was reported in older versions or specific manual installations of 'langid', often related to Python 3's stricter import rules or an incorrect package structure after installation.
fix
Ensure you have installed the package correctly using `pip install langid` in a clean Python 3 environment. If using the community-driven `py3langid` fork, the import statement is `import py3langid as langid`.
SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.
This error occurs during installation, typically when building the package from source, because the system lacks the Python development headers required to compile certain components.
fix
Install the Python development packages appropriate for your operating system. For Debian/Ubuntu, use `sudo apt-get install python3-dev` (or `python-dev` for Python 2). For CentOS/RHEL, use `sudo yum install python3-devel` (or `python-devel`).
Upgrade
Version history
1.1.6latest on PyPI · released Apr 5, 2016
Audit
Dependencies
numpyrequiredRequired for numerical computations and model handling within the library.
Agent activity
7 hits · last 30 days
node
6
Resources