-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Migrate langdetect to langid.py with lazy loading (#1208) #1899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| import pytest | ||
|
|
||
| from garak.langservice import _load_langprovider | ||
| from garak.langproviders.base import split_input_text | ||
| from garak.langproviders.base import is_meaning_string, split_input_text | ||
|
|
||
|
|
||
| def test_split_input_text(): | ||
|
|
@@ -17,6 +17,52 @@ def test_split_input_text(): | |
| assert split_input_text(input_text) == expected_output | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text", | ||
| [ | ||
| "Bonjour, ceci est une phrase en français.", | ||
| "これは日本語の文章です。", | ||
| "Hola mundo, esto es una frase en español.", | ||
| ], | ||
| ) | ||
| def test_is_meaning_string_true_for_non_english(text): | ||
| # Non-English, translatable text should be flagged as meaningful | ||
| assert is_meaning_string(text) is True | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good, i like that we have coverage here |
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text", | ||
| [ | ||
| "This is a normal English sentence about cats.", # English needs no translation | ||
| "ab", # too short | ||
| "a", # too short | ||
| "", # empty | ||
| "aaaa", # repetitive | ||
| "123123123", # no alphabetic content | ||
| "12345", # no alphabetic content | ||
| "!!! ??? ...", # no alphabetic content | ||
| "$", # no alphabetic content | ||
| " ", # whitespace only | ||
| ], | ||
| ) | ||
| def test_is_meaning_string_false_for_meaningless_or_english(text): | ||
| assert is_meaning_string(text) is False | ||
|
|
||
|
|
||
| def test_langproviders_base_uses_langid_lazily(): | ||
| # Migration contract for issue #1208: langdetect is gone, and langid is | ||
| # imported lazily inside is_meaning_string rather than at module load. | ||
| import inspect | ||
|
|
||
| from garak.langproviders import base | ||
|
|
||
| module_source = inspect.getsource(base) | ||
| assert "langdetect" not in module_source | ||
| module_top = module_source.split("\ndef ")[0] | ||
| assert "import langid" not in module_top | ||
| assert "import langid" in inspect.getsource(base.is_meaning_string) | ||
|
|
||
|
|
||
|
Comment on lines
+52
to
+65
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a bit too in the weeds and locks us in to a specific implementation pattern, i think it should be dropped |
||
| @pytest.mark.parametrize( | ||
| "langprovider_class, target_lang, model_name", | ||
| [ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should rename function and change this comment - it's too cryptic
suggest "check the input text is suitable for translation"
and
def is_suitable_for_translation