Registry / ai-ml / langdetect

langdetect

JSON →
library1.0.9pypypi✓ verified 25d ago

langdetect is a pure Python port of Google's language-detection library, offering capabilities to identify the language of a given text. It supports over 50 languages and provides both a single-best guess and a list of probable languages with confidence scores. The current version is 1.0.9, released in 2018, indicating a very stable but slow release cadence, effectively in a maintenance state.

pip install langdetect
INSTALL
IMPORT
SIG · LANGDETECT
L
langdetect
ai-mlpythonv1.0.9
Install
2.9s avg
Import
37ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.9 · 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.038s · 21.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.9s · import 0.036s · 22MB
19MB installed
● package 19MB
Code
Verified usage

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

detect
from langdetect import detect
detect_langs
from langdetect import detect_langs
set_seed
from langdetect import set_seed
Use to ensure reproducible results for short texts.
LangDetectException
from langdetect import LangDetectException
Crucial for handling inputs that cannot be reliably detected.

Demonstrates how to detect the primary language of a text and retrieve a list of possible languages with their confidence scores. It also includes error handling for `LangDetectException`, which is common for short or non-linguistic inputs, and how to use `set_seed` for reproducible results.

from langdetect import detect, detect_langs, set_seed, LangDetectException # For reproducible results, especially with short texts where probabilities are close set_seed(0) text_en = "This is a simple English sentence." text_fr = "Ceci est une simple phrase française." text_mixed = "Hallo Welt! This is a mixed text." try: # Detect the primary language print(f"'{text_en}' detected as: {detect(text_en)}") print(f"'{text_fr}' detected as: {detect(text_fr)}") print(f"'{text_mixed}' detected as: {detect(text_mixed)}") # May vary due to mix # Get a list of detected languages with their probabilities print(f"Probabilities for '{text_en}': {[str(l) for l in detect_langs(text_en)]}") print(f"Probabilities for '{text_fr}': {[str(l) for l in detect_langs(text_fr)]}") # Handling short/invalid text text_short_or_invalid = "a" print(f"Attempting to detect '{text_short_or_invalid}'...") print(f"Probabilities for '{text_short_or_invalid}': {[str(l) for l in detect_langs(text_short_or_invalid)]}") except LangDetectException as e: # This exception is common for very short or non-linguistic texts print(f"An error occurred: {e}. This often happens with very short or unsuitable input text.")
Debug
Known issues
gotchaThe library frequently raises `langdetect.lang_detect_exception.LangDetectException` for short texts, empty strings, or text that doesn't contain enough linguistic information for reliable detection.
fix
Always wrap `detect()` and `detect_langs()` calls in a `try...except LangDetectException` block. Consider pre-validating input length or content.
affects: All versions
gotchaFor very short texts, `langdetect` can produce non-deterministic or inconsistent results due to internal sampling. This can lead to different outputs for the same input across multiple runs if not handled.
fix
Call `langdetect.set_seed(seed_value)` at the beginning of your program to ensure reproducible results, especially important for testing and debugging.
affects: All versions
gotchaThe language models used by `langdetect` are based on an older Google project (circa 2014-2018) and are not actively updated. This may lead to less accurate results compared to newer, more sophisticated language detection libraries, especially for modern slang, domain-specific text, or less common languages/dialects.
fix
For critical applications requiring high accuracy or specific language nuances, evaluate newer libraries like `fasttext` or `cld3` (a port of Google's newer CLD3) that offer more recent models and potentially better performance.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'langdetect'
The 'langdetect' library has not been installed in the Python environment where the code is being executed, or the environment's path is not correctly configured to find the installed package.
fix
Install the 'langdetect' package using pip: `pip install langdetect`
No features in text
The input text provided to `langdetect.detect()` or `langdetect.detect_langs()` is too short, empty, contains only non-alphabetic characters, or lacks sufficient linguistic features for the library to make a detection.
fix
Ensure the input string is not empty and contains enough meaningful text for language detection. Wrap the detection call in a `try-except` block to gracefully handle `langdetect.lang_detect_exception.LangDetectException` for such cases.
IndexError: list index out of range
This error often occurs when `langdetect.detect_langs()` is called with text where no language can be confidently identified, causing it to return an empty list, and the subsequent code attempts to access an element (e.g., `[0]`) from this empty list.
fix
Before accessing elements from the list returned by `detect_langs()`, check if the list is not empty. Use a conditional statement (e.g., `if len(languages) > 0:`) or a `try-except` block to handle cases where no languages are detected.
Upgrade
Version history
1.0.9latest on PyPI · released May 7, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
langdetect — pip install langdetect · libregistry