feat: Add CharacterErrorRate (CER) metric to ignite.metrics.nlp
Summary
Implements CharacterErrorRate (CER) as a follow-up to #3638 (WER), resolving #3634.
CER measures the edit distance at the character level — used in ASR and OCR evaluation where a single character error (e.g. misreading a financial figure) is a severe failure.
Changes
ignite/metrics/nlp/character_error_rate.py— CER metric inheriting from_BaseErrorRate, using character-level Levenshtein distancetests/ignite/metrics/nlp/test_character_error_rate.py— 15 test cases covering: identical sequences, single deletion/insertion/substitution, empty inputs, batch accumulation, multi-update accumulation, reset, single string input, whitespace as character, unicodeignite/metrics/nlp/__init__.py— exportsCharacterErrorRate
Design
Follows the same structure as word_error_rate.py and bleu.py:
- Separate file per metric (per maintainer feedback in #3634)
- Inherits
_BaseErrorRatefromword_error_rate.py - Only difference from WER:
_tokenizereturnslist(text)instead oftext.split()
Closes #3634
"Updated in commit 329d981: made self-contained — inlined _edit_distance and full Metric implementation directly, removing dependency on unmerged PR #3638. All 15 tests pass on master."
Hi, @Shubh3005 we really appreciate your pr, but I would still recommend you check withe owner of the other open pr if they are still working on it or not just be sure :)
If not then we can move forward with your pr.
Thank you! I'll reach out to @Manimaran-tech on PR #3638 to check their status. Will follow up shortly.
Hi @aaishwarymishra, following up after reaching out to @Manimaran-tech on PR #3638 — I haven't heard back from them in the two weeks since. Given the WER PR appears inactive, would it make sense to move forward with this one? Happy to rebase against master if that helps move things along. Let me know if there's anything else you'd like me to address.
Also there is some problems with the docs.
Also add metric in docs/source/metrics.rst
Thanks for the review @aaishwarymishra!
Addressing all three points:
- The explicit
list(p)is for character tokenization — converting a string to a list of individual characters. Happy to simplify to direct iteration if you prefer, will clean that up. - Will add explicit type validation for the list case to match WER's handling.
- Will update the docstring to explicitly mention input types and add the entry to
docs/source/metrics.rst.
Will push fixes shortly.
Hi @aaishwarymishra, addressed all three points in the latest commit:
list(p)/list(r)— removed. Strings are already character sequences so direct iteration is equivalent and cleaner._edit_distanceworks on anySequenceso passing the string directly is fine.- Type validation — added explicit
isinstancechecks for the list case, raisingTypeErrorif elements are not strings. Covers bothy_predandy. - Docstring + metrics.rst — updated docstring to explicitly document that inputs can be
strorlist of str, and addedCharacterErrorRatetodocs/source/metrics.rst.
Thanks @aaishwarymishra! Addressed in latest commit:
Removed the redundant element-level isinstance loops from update() Fixed docstring: "receive output" → "receive input", removed unsupported dict form Consolidated y_pred/y description per your suggestion
Hi doc tests are failing :( Also the code style check, please use pre-commit hook for that.
Hi @aaishwarymishra, fixed both issues:
- Switched example from
.. code-block:: pythonto.. testcode::/.. testoutput::format (matching Bleu's convention) - Ran pre-commit hooks — ruff-format applied, all checks passing
I don't think we need to explicitly call super().reset() you can check other metrics implementation for reference.
Thanks @aaishwarymishra! Removed the super().reset() call — confirmed Metric.reset() is abstract with no body so calling it was unnecessary. Checked other metrics for reference as suggested. All 15 tests still passing, pre-commit clean. Ready for merge! 🙏
Hi @Shubh3005 I appreciate you being helping. If you are using an agent its's fine, but its generally good Idea to check whats its generating. It randomly replace | with Union also added the unnecessary comments. :)
Hi @aaishwarymishra, I've been reviewing more carefully now.
I think these are last few changes
Awesome added 3 changes let me know if anything else needed.
Added and fixed those 3 tests :)
@vfdev-5 It looks good and can be merged, link check is failing but I think its not related to this pr :)
@aaishwarymishra approve the PR it it is looks good to you
@vfdev-5 done