ignite icon indicating copy to clipboard operation
ignite copied to clipboard

feat: Add CharacterErrorRate (CER) metric to ignite.metrics.nlp

Open Shubh3005 opened this issue 5 months ago • 8 comments

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 distance
  • tests/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, unicode
  • ignite/metrics/nlp/__init__.py — exports CharacterErrorRate

Design

Follows the same structure as word_error_rate.py and bleu.py:

  • Separate file per metric (per maintainer feedback in #3634)
  • Inherits _BaseErrorRate from word_error_rate.py
  • Only difference from WER: _tokenize returns list(text) instead of text.split()

Closes #3634

Shubh3005 avatar Jun 09 '26 18:06 Shubh3005

"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."

Shubh3005 avatar Jun 09 '26 18:06 Shubh3005

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.

aaishwarymishra avatar Jun 10 '26 12:06 aaishwarymishra

Thank you! I'll reach out to @Manimaran-tech on PR #3638 to check their status. Will follow up shortly.

Shubh3005 avatar Jun 10 '26 15:06 Shubh3005

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.

Shubh3005 avatar Jun 21 '26 21:06 Shubh3005

Also there is some problems with the docs.

Also add metric in docs/source/metrics.rst

aaishwarymishra avatar Jun 22 '26 07:06 aaishwarymishra

Thanks for the review @aaishwarymishra!

Addressing all three points:

  1. 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.
  2. Will add explicit type validation for the list case to match WER's handling.
  3. Will update the docstring to explicitly mention input types and add the entry to docs/source/metrics.rst.

Will push fixes shortly.

Shubh3005 avatar Jun 22 '26 13:06 Shubh3005

Hi @aaishwarymishra, addressed all three points in the latest commit:

  1. list(p) / list(r) — removed. Strings are already character sequences so direct iteration is equivalent and cleaner. _edit_distance works on any Sequence so passing the string directly is fine.
  2. Type validation — added explicit isinstance checks for the list case, raising TypeError if elements are not strings. Covers both y_pred and y.
  3. Docstring + metrics.rst — updated docstring to explicitly document that inputs can be str or list of str, and added CharacterErrorRate to docs/source/metrics.rst.

Shubh3005 avatar Jun 22 '26 21:06 Shubh3005

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

Shubh3005 avatar Jun 24 '26 11:06 Shubh3005

Hi doc tests are failing :( Also the code style check, please use pre-commit hook for that.

aaishwarymishra avatar Jun 25 '26 11:06 aaishwarymishra

Hi @aaishwarymishra, fixed both issues:

  1. Switched example from .. code-block:: python to .. testcode:: / .. testoutput:: format (matching Bleu's convention)
  2. Ran pre-commit hooks — ruff-format applied, all checks passing

Shubh3005 avatar Jun 25 '26 13:06 Shubh3005

I don't think we need to explicitly call super().reset() you can check other metrics implementation for reference.

aaishwarymishra avatar Jun 25 '26 13:06 aaishwarymishra

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! 🙏

Shubh3005 avatar Jun 25 '26 13:06 Shubh3005

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. :)

aaishwarymishra avatar Jun 25 '26 16:06 aaishwarymishra

Hi @aaishwarymishra, I've been reviewing more carefully now.

Shubh3005 avatar Jun 25 '26 17:06 Shubh3005

I think these are last few changes

aaishwarymishra avatar Jun 26 '26 09:06 aaishwarymishra

Awesome added 3 changes let me know if anything else needed.

Shubh3005 avatar Jun 26 '26 15:06 Shubh3005

Added and fixed those 3 tests :)

Shubh3005 avatar Jun 26 '26 18:06 Shubh3005

@vfdev-5 It looks good and can be merged, link check is failing but I think its not related to this pr :)

aaishwarymishra avatar Jun 26 '26 19:06 aaishwarymishra

@aaishwarymishra approve the PR it it is looks good to you

vfdev-5 avatar Jun 26 '26 21:06 vfdev-5

@vfdev-5 done

aaishwarymishra avatar Jun 27 '26 00:06 aaishwarymishra