Skip to content

Commit 1f21804

Browse files
committed
Workaround fix for japanese word count with heuristic to reduce skippings
1 parent b9bd09b commit 1f21804

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

evaluators/lingua/langevals_lingua/language_detection.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ class LinguaLanguageDetectionRawResponse(BaseModel):
121121
class LinguaLanguageDetectionResult(EvaluationResult):
122122
score: float = Field(description="How many languages were detected")
123123
passed: Optional[bool] = Field(
124-
description="Passes if the detected language on the output matches the detected language on the input, or if the output matches the expected language", default=None
124+
description="Passes if the detected language on the output matches the detected language on the input, or if the output matches the expected language",
125+
default=None,
125126
)
126127
label: Optional[str] = Field(
127-
description="Language detected on the input for input_matches_output, or language detected on the output for output_matches_language", default=None
128+
description="Language detected on the input for input_matches_output, or language detected on the output for output_matches_language",
129+
default=None,
128130
)
129131
raw_response: LinguaLanguageDetectionRawResponse
130132

@@ -157,14 +159,16 @@ def preload(cls):
157159
super().preload()
158160

159161
def evaluate(self, entry: LinguaLanguageDetectionEntry) -> SingleEvaluationResult:
160-
words = entry.input.split(" ")
162+
words = max(len(entry.input.split(" ")), len(entry.input.encode("utf-8")) // 5)
161163
if self.settings.check_for == "input_matches_output":
162-
if len(words) < self.settings.min_words:
164+
if words < self.settings.min_words:
163165
return EvaluationResultSkipped(
164166
details=f"Skipped because the input has less than {self.settings.min_words} words"
165167
)
166-
words = entry.output.split(" ")
167-
if len(words) < self.settings.min_words:
168+
words = max(
169+
len(entry.output.split(" ")), len(entry.output.encode("utf-8")) // 5
170+
)
171+
if words < self.settings.min_words:
168172
return EvaluationResultSkipped(
169173
details=f"Skipped because the output has less than {self.settings.min_words} words"
170174
)
@@ -183,7 +187,9 @@ def evaluate(self, entry: LinguaLanguageDetectionEntry) -> SingleEvaluationResul
183187
return EvaluationResultSkipped(
184188
details=f"Skipped because no language could be detected on the output with a confidence higher than {self.settings.threshold}"
185189
)
186-
output_language_highest_confidence = sorted(output_languages.items(), key=lambda x: x[1], reverse=True)[0][0]
190+
output_language_highest_confidence = sorted(
191+
output_languages.items(), key=lambda x: x[1], reverse=True
192+
)[0][0]
187193

188194
if self.settings.check_for == "output_matches_language":
189195
passed = (
@@ -212,7 +218,9 @@ def evaluate(self, entry: LinguaLanguageDetectionEntry) -> SingleEvaluationResul
212218
return EvaluationResultSkipped(
213219
details=f"Skipped because no language could be detected on the input with a confidence higher than {self.settings.threshold}"
214220
)
215-
input_language_highest_confidence = sorted(input_languages.items(), key=lambda x: x[1], reverse=True)[0][0]
221+
input_language_highest_confidence = sorted(
222+
input_languages.items(), key=lambda x: x[1], reverse=True
223+
)[0][0]
216224

217225
passed = any(lang in input_languages for lang in output_languages)
218226
details = "" if passed else "Input and output languages do not match. "

evaluators/lingua/tests/test_language_detection.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,22 @@ def test_language_detection_evaluator_long_context():
100100
assert (
101101
result.details == "Input languages detected: LA. Output languages detected: LA"
102102
)
103+
104+
105+
def test_detecting_japanese():
106+
entry = LinguaLanguageDetectionEntry(
107+
input="自動更新の解約の仕方を教えてください",
108+
output="自動更新を解約するには、ユーザーのアカウントにログインして、「設定」を選択して、「自動更新」を解約してください。",
109+
)
110+
evaluator = LinguaLanguageDetectionEvaluator(
111+
settings=LinguaLanguageDetectionSettings(check_for="input_matches_output")
112+
)
113+
result = evaluator.evaluate(entry)
114+
115+
assert result.status == "processed"
116+
assert result.passed == True
117+
assert result.label == "JA"
118+
assert result.score == 1.0
119+
assert (
120+
result.details == "Input languages detected: JA. Output languages detected: JA"
121+
)

0 commit comments

Comments
 (0)