Add pure Python BLEU, ROUGE, WER, and CER metrics with automatic Thai tokenization - #1295
Conversation
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
…English content Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
|
@copilot Add docs to docs/api/benchmarks.rst |
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Clarified the context of the test for BLEU score with lowercase option.
|
Markdown lint issues are already fixed in dev branch. |
|
@copilot Update the fix Markdown lint from |
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
|
@copilot Add word error rate (or WER) and fix https://github.com/PyThaiNLP/pythainlp/actions/runs/21917638767/job/63289013991?pr=1295 |
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Added Word Error Rate (WER) metric with automatic Thai tokenization. Implemented Regarding the CI failure, I've verified all imports work correctly and the code has valid Python syntax. The implementation follows the same patterns as BLEU and ROUGE. If there's a specific test failure, please let me know the details. |
Update Markdown fixes
Removed numpy and PyYAML dependencies from the project.
|
@copilot Add CER |
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
Added Character Error Rate (CER) metric for character-level evaluation. Implemented |
| # CER should be > 0 due to deletion | ||
| self.assertGreater(cer, 0.0) | ||
| self.assertLess(cer, 1.0) | ||
| self.assertGreater(wer, 0.0) |
There was a problem hiding this comment.
@copilot Fix
ruff (F821)
Check failure: ruff (F821)
tests/extra/testx_benchmarks.py:397:28: F821 Undefined name `wer`
There was a problem hiding this comment.
Fixed the undefined name error. Removed the incorrect reference to wer in the test_character_error_rate_deletions test - it was a copy-paste error from the WER tests. The test now correctly only checks cer. Commit: 445e835
Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
|




What do these changes do
Adds
bleu_score(),rouge_score(),word_error_rate(), andcharacter_error_rate()functions topythainlp.benchmarksthat automatically tokenize Thai text before computing metrics.Key additions:
word_tokenize(configurable engine for word-level metrics)docs/api/benchmarks.rstwith usage examplesUsage:
What was wrong
Users had to manually tokenize Thai text before using external libraries (sacrebleu, rouge-score) to calculate BLEU/ROUGE scores, requiring repetitive preprocessing boilerplate. Additionally, there was no built-in support for calculating Word Error Rate (WER) or Character Error Rate (CER) for Thai text evaluation.
How this fixes it
Implements metrics natively in PyThaiNLP with automatic tokenization. No external dependencies required—uses pure Python with existing
word_tokenizeinfrastructure. Includes comprehensive API documentation with examples in the official documentation. WER operates at word-level with automatic tokenization, while CER operates at character-level for more granular evaluation (useful for OCR and speech recognition). Also incorporates markdown lint fixes from dev branch to maintain consistency with project formatting standards.Your checklist for this pull request
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.