Replace langdetect with langid for improved language detection #1365
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.
Summary
This PR replaces the
langdetectlibrary withlangid.pyfor language detection functionality as proposed in issue #1208.Changes Made
Core Changes
garak/langproviders/base.pylangdetecttolangidis_meaning_string()function to uselangid.classify()instead ofdetect()DetectorFactory.seedinitialization (not needed with langid)Dependencies
pyproject.tomlandrequirements.txtlangdetect==1.0.9withlangid==1.1.6Testing
tests/langservice/test_langid_migration.pyWhy langid?
As mentioned in issue #1208, langid offers several advantages:
API Changes
The migration maintains the same external interface - only the internal implementation changes:
Before (langdetect):
After (langid):
Testing Results
All tests pass successfully:
Language Detection Examples
enesfrjazhFunction Behavior
is_meaning_string("Hello world")→False(English text)is_meaning_string("Hola mundo")→True(Non-English text)Backward Compatibility
This change is backward compatible - the external behavior of
is_meaning_string()and related functions remains the same. Only the internal implementation changes from langdetect to langid.Performance Impact
Expected improvements:
Fixes
Closes #1208
Type of Change