langdetect
Python port of Google's Java language-detection library for text language identification
Repository Health
Technical Analysis
langdetect is a Python port of Nakatani Shuyo’s Java language-detection library (itself inspired by work at Google), providing statistical natural-language identification for a piece of text. Given a string, it returns the most likely ISO 639-1 language code, or a ranked list of language probabilities, across 55 supported languages.
The library uses the same n-gram-based Naive Bayes classifier and character-frequency profiles as the original Java implementation, aiming for algorithmic parity with the ported project rather than introducing a new detection approach. It has no runtime dependency beyond six, making it easy to drop into text-processing pipelines that need a lightweight, offline language guess without calling an external API.
What You Get
detect(text)returning the single most probable ISO 639-1 language code for a stringdetect_langs(text)returning a ranked list of language codes with probability scores- Support for 55 languages out of the box via precomputed n-gram profiles
- A
DetectorFactoryfor seeding deterministic detection results (useful for reproducible tests) - Minimal runtime dependency footprint (
sixonly), keeping the library lightweight for pipeline integration
Common Use Cases
- Auto-detecting the source language of user-submitted text before routing to language-specific processing (translation, sentiment analysis, etc.)
- Filtering or tagging documents/messages by language in multilingual data pipelines
- Pre-filtering non-target-language content in NLP preprocessing before more expensive downstream models run
- Adding a quick, offline language guess to chat, search, or content-moderation systems without an external API call
Under The Hood
Architecture — The library follows the same structure as the original Java project: detector_factory.py builds and caches Detector instances seeded with per-language n-gram probability profiles bundled with the package, detector.py implements the actual Naive Bayes classification over those profiles, and language.py is a small value object pairing a language code with its detection probability. lang_detect_exception.py provides a dedicated exception type distinguishing detection failures (e.g. text too short) from other errors. Tech Stack — Pure Python, supporting Python 2.7 and 3.4+ per its setup.py/classifiers, packaged with plain setuptools. The sole runtime dependency is six, present specifically to support that Python 2/3 compatibility span — a sign the library predates the Python 2 sunset and has not been modernized to drop that shim. Code Quality — Tests live in langdetect/tests/ (test_detector.py, test_language.py, plus a tests/utils subpackage) and are shipped as part of the installable package itself (packages=['langdetect', 'langdetect.utils', 'langdetect.tests'] in setup.py), an older packaging convention rather than a modern src-layout with tests kept out of the built wheel. CI is configured via Travis CI (a badge still referencing travis-ci.org), reflecting that tooling hasn’t been migrated to a currently-maintained CI provider. API Design — The two-function surface (detect, detect_langs) is about as low-friction as an NLP utility API gets — a single import and call returns a usable result — though the library exposes no dependency-free type hints and its exception handling (raising LangDetectException for very short input) needs to be learned rather than being self-evident from the return type.
Used by 4 apps in this directory
GPT Researcher
Productivity · AI Assistants
The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.
Paperless-ngx
Bookmarks Archiving
Turn your paper pile into a searchable digital archive with OCR, AI classification, and automated workflows — all running on your own server.
PrivateGPT
AI Development
The open-source API layer that turns local LLMs into production private AI applications with full Claude API compatibility
Social Analyzer
Security
Find and analyze a person's digital footprint across 1000+ social media platforms using multi-layer detection, OCR, and metadata extraction.