Skip to content

Language Detection

Detect languages in extracted text using whatlang — supports 60+ languages with ISO 639-3 codes. Set detect_multiple: true to chunk the text into 200-character segments and return all detected languages sorted by prevalence.

Set min_confidence (0.01.0, default 0.8) to the lowest whatlang confidence a detection must reach to be reported. In single-language mode, the primary detection is dropped and no language is returned when it scores below the threshold. In detect_multiple mode, the threshold is applied to each 200-character chunk; chunks below it are discarded, and if no chunk clears it, detection falls back to single-language mode. Per-chunk confidence runs lower than whole-document confidence, so a high threshold can suppress multi-language results.

Python
import asyncio
from xberg import ExtractInput, ExtractionConfig, LanguageDetectionConfig, extract
async def main() -> None:
config: ExtractionConfig = ExtractionConfig(
language_detection=LanguageDetectionConfig(
enabled=True,
min_confidence=0.85,
detect_multiple=False
)
)
result = await extract(ExtractInput.from_uri("document.pdf"), config)
if result.detected_languages:
print(f"Primary language: {result.detected_languages[0]}")
print(f"Content length: {len(result.results[0].content)} chars")
asyncio.run(main())
Python
import asyncio
from xberg import ExtractInput, extract, ExtractionConfig, LanguageDetectionConfig
async def main() -> None:
config: ExtractionConfig = ExtractionConfig(
language_detection=LanguageDetectionConfig(
enabled=True,
min_confidence=0.7,
detect_multiple=True
)
)
result = await extract(ExtractInput.from_uri("multilingual_document.pdf"), config)
languages: list[str] = result.detected_languages or []
print(f"Detected {len(languages)} languages: {languages}")
asyncio.run(main())