langdetect

Python port of Google's Java language-detection library for text language identification

Library
PyPI
v1.0.9
1,900stars
Custom / Unknown

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
46/100Fair
Development Activity0
Maintenance20
Community64
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
63/100Good
Architecture62
Code Quality58
Innovation55
Learning Curve78

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 string
  • detect_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 DetectorFactory for seeding deterministic detection results (useful for reproducible tests)
  • Minimal runtime dependency footprint (six only), 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search