mem: load only 15 common langdetect profiles to reduce memory#4297
Open
KRRT7 wants to merge 6 commits intoUnstructured-IO:mainfrom
Open
mem: load only 15 common langdetect profiles to reduce memory#4297KRRT7 wants to merge 6 commits intoUnstructured-IO:mainfrom
KRRT7 wants to merge 6 commits intoUnstructured-IO:mainfrom
Conversation
Monkey-patch langdetect's init_factory() to load 15 high-coverage language profiles instead of all 55. Cuts n-gram probability map memory by ~77% (58 MiB -> 14 MiB). Documents in excluded languages still get a result from the closest loaded profile.
…ile-subset # Conflicts: # CHANGELOG.md
cragwolfe
approved these changes
Mar 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Monkey-patch langdetect's
init_factory()to load only 15 high-coverage language profiles instead of all 55. The full set creates large n-gram probability arrays (word_lang_prob_map) that persist for the lifetime of the process. The subset covers >95% of web content by language share.What happens with unsupported languages?
langdetect always returns a result — it scores text against loaded profiles using n-gram frequency comparison and picks the best match. For a document in an excluded language (e.g., Thai, Polish), langdetect returns the closest loaded profile instead of the correct one. This is the same behavior as before for the downstream code path:
detect_languages()maps the langdetect result to an ISO 639-3 code for element metadata. The language metadata is informational — it does not gate OCR model selection or partitioning logic.In practice this means:
idfor Thai text) instead ofth. No crash, no empty result — just a less precise language tag in metadata.The 15 languages were chosen to cover the vast majority of real-world document processing:
en, es, ar, fr, de, it, pt, ru, ja, ko, zh-cn, zh-tw, hi, bn, id. If a deployment needs accurate metadata for additional languages, the set can be extended by adding toLANGDETECT_LANGUAGESinlang.py.Benchmark
Measured with memray (
memray run+memray stats --json), 3 rounds × 5 texts (EN/DE/FR/JP + short ASCII) through the realdetect_languages()code path, Python 3.12.