Commit 8200fae
fix(evaluation): support non-English responses in ROUGE-1 matching
Merge #6292
Closes #3111
Problem:
The `response_match_score` metric (`RougeEvaluator`) always returns 0 for responses in non-Latin scripts, even when the actual and expected responses are identical.
Root Cause:
The default `rouge_score` tokenizer (`DefaultTokenizer`) lowercases text and replaces every character outside `[a-z0-9]` with a space. Consequently, non-Latin scripts (Thai, Chinese, Arabic, Japanese, Cyrillic, etc.) tokenize to an empty token list (`[]`) and every comparison scores 0.0.
Reproduction on `main`:
```python
from rouge_score import rouge_scorer
scorer = rouge_scorer.RougeScorer(["rouge1"], use_stemmer=True)
scorer.score("สวัสดี", "สวัสดี")["rouge1"].fmeasure # 0.0 — identical strings
scorer.score("hello", "hello")["rouge1"].fmeasure # 1.0
```
Solution:
Pass a custom `_UnicodeAwareTokenizer` to `RougeScorer` in `final_response_match_v1.py` to handle non-English scripts:
1. CJK (Chinese, Japanese Hiragana/Katakana, Hangul): Characters identified via `_is_cjk()` (Unicode ranges `0x4E00..0x9FFF`, `0x3040..0x309F`, `0x30A0..0x30FF`, `0xAC00..0xD7AF`) are separated into individual character tokens. This enables character-level ROUGE-1 unigram matching for CJK text without external heavy NLP segmentation dependencies.
2. Non-spaced scripts (Thai, Lao, Khmer, Myanmar): Detected via `_is_non_spaced_script()` (`0x0E00..0x0E7F` etc.). Characters are bundled into grapheme clusters where base consonants trigger new word boundaries while combining marks/vowels/tone marks (Unicode category `M`, e.g. `Mn` like Thai vowel signs) stay attached to their base consonant.
3. Spaced Non-ASCII scripts (Arabic, Cyrillic, Hindi, etc.): Non-ASCII word characters (`str.isalnum()` or category `M`) are preserved as distinct tokens instead of being stripped.
4. ASCII compatibility: Pure-ASCII tokens are delegated to `rouge_score`s default `DefaultTokenizer`, keeping lowercasing and Porter stemming identical for English text.
Testing Plan:
Unit Tests:
- [x] I have added or updated unit tests for my change.
- [x] All unit tests pass locally.
New tests in `tests/unittests/evaluation/test_final_response_match_v1.py`:
- Identical non-English text scores 1.0 (Thai, Chinese, Arabic, Japanese, Russian)
- Partially overlapping non-English text scores expected ROUGE-1 fractions (CJK & Russian)
- Mixed English + non-English text handling
- `_UnicodeAwareTokenizer` produces identical tokens to `DefaultTokenizer` for ASCII inputs (regression guard for English scoring)
- Evaluator-level test cases for identical, partially matching (Chinese & Thai), and completely non-overlapping non-English responses
```
$ pytest tests/unittests/evaluation/test_final_response_match_v1.py -q
65 passed in 14.17s
```
Manual End-to-End (E2E) Tests:
Ran the evaluator directly on the scenario from #3111 (agent instructed to reply with the word `สวัสดี`, expected response `สวัสดี`):
```python
ev = RougeEvaluator(EvalMetric(metric_name="response_match_score", threshold=0.8))
result = ev.evaluate_invocations([inv("สวัสดี")], [inv("สวัสดี")])
# before: score=0.0, status=EvalStatus.FAILED
# after: score=1.0, status=EvalStatus.PASSED
```
Original PR by @agharsallah
Co-authored-by: Yi Liu <yiliuly@google.com>
COPYBARA_INTEGRATE_REVIEW=#6292 from agharsallah:fix/eval-rouge-non-english-3111 4810719975
PiperOrigin-RevId: 9548092491 parent d31b5e7 commit 8200fae
3 files changed
Lines changed: 291 additions & 1 deletion
File tree
- src/google/adk
- dependencies
- evaluation
- tests/unittests/evaluation
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| |||
96 | 98 | | |
97 | 99 | | |
98 | 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 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
99 | 178 | | |
100 | 179 | | |
101 | 180 | | |
| |||
114 | 193 | | |
115 | 194 | | |
116 | 195 | | |
117 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
118 | 199 | | |
119 | 200 | | |
120 | 201 | | |
| |||
Lines changed: 208 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
22 | 28 | | |
23 | 29 | | |
24 | 30 | | |
| 31 | + | |
25 | 32 | | |
26 | 33 | | |
27 | 34 | | |
| |||
87 | 94 | | |
88 | 95 | | |
89 | 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 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
90 | 274 | | |
91 | 275 | | |
92 | 276 | | |
| |||
114 | 298 | | |
115 | 299 | | |
116 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
117 | 325 | | |
118 | 326 | | |
119 | 327 | | |
| |||
0 commit comments