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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.038s · 21.7MB
glibcpy 3.10–3.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.")
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.
fixInstall 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.
fixEnsure 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.
fixBefore 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.