chunklet.common.lang_detection
Functions:
-
detect_top_language–Detect the top language of the given text using py3langid.
detect_top_language
Detect the top language of the given text using py3langid.
The LanguageIdentifier is built lazily on first use and cached for reuse.
Parameters:
-
(textstr) –The input text to detect the language for.
Returns:
-
str–A tuple of the ISO 639-1 language code and its confidence in
[0, 1]. -
float–Confidence depends on the
py3langidmodel, so treat it as -
tuple[str, float]–approximate rather than a threshold you can rely on across versions.
Raises:
-
ImportError–If py3langid is not installed.
Examples:
>>> lang, confidence = detect_top_language("This sentence is written in English.")
>>> lang, confidence > 0.8
('en', True)
>>> detect_top_language("Ceci est une phrase ecrite en francais.")[0]
'fr'
>>> code, confidence = detect_top_language("")
>>> round(confidence, 2)
0.01