-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_scripts.py
More file actions
129 lines (107 loc) · 5.84 KB
/
Copy pathtest_scripts.py
File metadata and controls
129 lines (107 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from __future__ import annotations
import pytest
from app.errors import LanguageUnsupportedError
from app.scripts import transcript_matches_language
from app.service import _require_matching_script
HINDI_LANGUAGE_CODE = "hi"
ENGLISH_LANGUAGE_CODE = "en"
def test_the_dolphin_cyrillic_bug_is_rejected() -> None:
"""The exact failure this guard exists for: dictating "नमस्ते" in Hindi to
Dolphin returns "насте" — fluent, confident, and in the wrong alphabet."""
assert transcript_matches_language("насте", HINDI_LANGUAGE_CODE) is False
with pytest.raises(LanguageUnsupportedError, match="different language"):
_require_matching_script("насте", HINDI_LANGUAGE_CODE)
def test_transliteration_into_another_scrip_aa() -> None:
"""A model under evaluation rendered "send me the report by Friday" in Arabic
script. A presence test let it through on the single stray Latin "o" in
"رoرت", which is why the check is proportional rather than binary."""
transliterated = "ل سند مي رoرت بي فريداي"
assert "o" in transliterated # the character that used to rescue it
assert transcript_matches_language(transliterated, ENGLISH_LANGUAGE_CODE) is False
with pytest.raises(LanguageUnsupportedError):
_require_matching_script(transliterated, ENGLISH_LANGUAGE_CODE)
def test_code_switching_is_never_rejected() -> None:
"""Indian speakers routinely mix English into Hindi, so the threshold has to
sit below the share of the base script even heavy Hinglish keeps."""
# Including heavy Hinglish, which is where a proportional check could bite.
for text in (
"मैं office जा रहा हूँ",
"मैं ठीक हूँ",
"kal मैं आऊंगा",
"मैं report Friday तक send करूंगा",
"ठीक है OK",
):
assert transcript_matches_language(text, HINDI_LANGUAGE_CODE) is True
_require_matching_script(text, HINDI_LANGUAGE_CODE)
def test_uncertain_cases_pass_rather_than_fail() -> None:
"""Anything the check cannot judge must be let through: a guard that guesses
would reject good transcripts."""
assert transcript_matches_language("anything at all", "auto") is True
assert transcript_matches_language("anything at all", "zz") is True # unknown code
assert transcript_matches_language("", HINDI_LANGUAGE_CODE) is True
assert transcript_matches_language("1,200 $45 —", HINDI_LANGUAGE_CODE) is True # no letters
# Chinese and Japanese share Han characters, so CJK is deliberately lenient.
assert transcript_matches_language("私は元気です", "zh") is True
def test_roman_hinglish_uses_the_latin_script_contract() -> None:
assert transcript_matches_language("Aaj mujhe office jaana hai", "hinglish_roman") is True
assert transcript_matches_language("आज मुझे जाना है", "hinglish_roman") is False
assert transcript_matches_language("Aaj office में meeting hai", "hinglish_roman") is False
@pytest.mark.parametrize(
("text", "language"),
[
("मैं ठीक हूँ", ENGLISH_LANGUAGE_CODE),
("hello there", HINDI_LANGUAGE_CODE),
("आज", "bn"),
("The quick brown fox", "ru"),
("مرحبا", HINDI_LANGUAGE_CODE),
],
)
def test_wrong_script_is_rejected(text: str, language: str) -> None:
assert transcript_matches_language(text, language) is False
@pytest.mark.parametrize(
("text", "language"),
[
("मैं कल बाजार जाऊंगा", HINDI_LANGUAGE_CODE),
("আমি ভালো আছি", "bn"),
("நான் நன்றாக இருக்கிறேன்", "ta"),
("私は元気です", "ja"),
("집에 갔어요", "ko"),
("ผมสบายดี", "th"),
("The quick brown fox", ENGLISH_LANGUAGE_CODE),
("Здравствуйте", "ru"),
("مان ٺيڪ آهيان", "sd"),
],
)
def test_matching_script_passes(text: str, language: str) -> None:
assert transcript_matches_language(text, language) is True
_require_matching_script(text, language)
def test_every_client_language_can_be_judged() -> None:
"""A language the clients offer but the table does not know would silently
skip the guard, which is how this class of bug goes unnoticed."""
from app.scripts import expected_scripts
client_languages = [
ENGLISH_LANGUAGE_CODE, "es", "ar", "ja", "ko", "zh", "uk", "ru", "vi", "fr", "de", "it",
"pt", "nl", "pl", HINDI_LANGUAGE_CODE, "bn", "mr", "te", "ta", "gu", "ur", "kn", "ml",
"pa", "as", "ne",
] # fmt: skip
unjudgeable = [code for code in client_languages if not expected_scripts(code)]
assert not unjudgeable, f"no expected script for: {unjudgeable}"
@pytest.mark.parametrize(
("text", "language", "script"),
[
("Здраво свете", "sr", "Serbian Cyrillic — official in Serbia"),
("Zdravo svete", "sr", "Serbian Latin — everyday use"),
("Салам дүнја", "az", "Azerbaijani Cyrillic"),
("Salam dünya", "az", "Azerbaijani Latin"),
("Салом дунё", "uz", "Uzbek Cyrillic"),
("Salom dunyo", "uz", "Uzbek Latin"),
("ਸਤ ਸ੍ਰੀ ਅਕਾਲ", "pa", "Punjabi Gurmukhi — India"),
("السلام علیکم", "pa", "Punjabi Shahmukhi — Pakistan"),
("Сәлеметсіз бе", "kk", "Kazakh Cyrillic"),
],
)
def test_languages_written_in_two_scripts_a_aaa(text: str, language: str, script: str) -> None:
"""A guard that rejects transcripts must not be wrong about a language's
writing system. Holding Serbian to one alphabet would throw away half the
country's writing, and a false rejection destroys a good dictation."""
assert transcript_matches_language(text, language), script