@@ -241,3 +241,81 @@ def test_rouge_score_no_overlap(self):
241241 self .assertGreaterEqual (precision , 0.0 )
242242 self .assertGreaterEqual (recall , 0.0 )
243243 self .assertGreaterEqual (fmeasure , 0.0 )
244+
245+ def test_word_error_rate_basic (self ):
246+ """Test WER with basic Thai text."""
247+ from pythainlp .benchmarks import word_error_rate
248+
249+ reference = "สวัสดีครับ วันนี้อากาศดีมาก"
250+ hypothesis = "สวัสดีค่ะ วันนี้อากาศดี"
251+
252+ wer = word_error_rate (reference , hypothesis )
253+
254+ self .assertIsNotNone (wer )
255+ self .assertGreaterEqual (wer , 0.0 )
256+ # WER can be > 1.0 if hypothesis has many insertions
257+ self .assertIsInstance (wer , float )
258+
259+ def test_word_error_rate_perfect_match (self ):
260+ """Test WER when reference and hypothesis are identical."""
261+ from pythainlp .benchmarks import word_error_rate
262+
263+ text = "สวัสดีครับ วันนี้อากาศดีมาก"
264+
265+ wer = word_error_rate (text , text )
266+
267+ # Perfect match should have WER = 0
268+ self .assertEqual (wer , 0.0 )
269+
270+ def test_word_error_rate_completely_different (self ):
271+ """Test WER with completely different text."""
272+ from pythainlp .benchmarks import word_error_rate
273+
274+ reference = "สวัสดีครับ"
275+ hypothesis = "ลาก่อนค่ะ"
276+
277+ wer = word_error_rate (reference , hypothesis )
278+
279+ # Should be > 0 since texts are different
280+ self .assertGreater (wer , 0.0 )
281+
282+ def test_word_error_rate_different_engines (self ):
283+ """Test WER with different tokenization engines."""
284+ from pythainlp .benchmarks import word_error_rate
285+
286+ reference = "สวัสดีครับ"
287+ hypothesis = "สวัสดีค่ะ"
288+
289+ # Test with newmm (default)
290+ wer_newmm = word_error_rate (reference , hypothesis , tokenize = "newmm" )
291+ self .assertIsNotNone (wer_newmm )
292+ self .assertGreaterEqual (wer_newmm , 0.0 )
293+
294+ # Test with longest
295+ wer_longest = word_error_rate (reference , hypothesis , tokenize = "longest" )
296+ self .assertIsNotNone (wer_longest )
297+ self .assertGreaterEqual (wer_longest , 0.0 )
298+
299+ def test_word_error_rate_empty_reference (self ):
300+ """Test WER with empty reference."""
301+ from pythainlp .benchmarks import word_error_rate
302+
303+ reference = ""
304+ hypothesis = "สวัสดีครับ"
305+
306+ wer = word_error_rate (reference , hypothesis )
307+
308+ # Empty reference with non-empty hypothesis should return inf or 0
309+ self .assertTrue (wer == 0.0 or wer == float ('inf' ))
310+
311+ def test_word_error_rate_insertions (self ):
312+ """Test WER with insertions (hypothesis longer than reference)."""
313+ from pythainlp .benchmarks import word_error_rate
314+
315+ reference = "สวัสดี"
316+ hypothesis = "สวัสดี ครับ วันนี้"
317+
318+ wer = word_error_rate (reference , hypothesis )
319+
320+ # WER can be > 1.0 due to insertions
321+ self .assertGreater (wer , 0.0 )
0 commit comments