class_docstr: CharacTer is a character-level metric inspired by the commonly applied translation edit rate (TER).
code: def _compute(
      self,
      predictions: Iterable[str],
      references: Union[Iterable[str], Iterable[Iterable[str]]],
      aggregate: str = "mean",
      return_all_scores: bool = False,
  ):
      if aggregate not in ("mean", "sum", "median"):
          raise ValueError(
              "'aggregate' must be one of 'sum', 'mean', 'median'"
          )
  
      predictions = [p.split() for p in predictions]
      # Predictions and references have the same internal types (both lists of strings),
      # so only one reference per prediction
      if isinstance(references[0], str):
          references = [r.split() for r in references]
  
          scores_d = cer.calculate_cer_corpus(predictions, references)
          cer_scores: List[float] = scores_d["cer_scores"]
  
          if aggregate == "sum":
              score = sum(cer_scores)
          elif aggregate == "mean":
              score = scores_d["mean"]
          else:
              score = scores_d["median"]
      else:
          # In the case of multiple references, we just find the "best score",
          # i.e., the reference that the prediction is closest to, i.e. the lowest characTER score
          references = [
              [r.split() for r in refs] for refs in references
          ]
  
          cer_scores = []
          for pred, refs in zip(predictions, references):
              min_score = math.inf
              for ref in refs:
                  score = calculate_cer(pred, ref)
  
                  if score < min_score:
                      min_score = score
  
              cer_scores.append(min_score)
  
          if aggregate == "sum":
              score = sum(cer_scores)
          elif aggregate == "mean":
              score = mean(cer_scores)
          else:
              score = median(cer_scores)
  
      # Return scores
      if return_all_scores:
          return {"cer_score": score, "cer_scores": cer_scores}
      else:
          return {"cer_score": score}
  
file_path: evaluate/metrics/character/character.py
imports: import math
  from statistics import mean, median
  from typing import Iterable, List, Union
  import cer
  import datasets
  from cer import calculate_cer
  from datasets import Sequence, Value
  import evaluate
label_desc: Use image node_id 2 for calling the Character obj's underlying member method code with example usage: obj._compute(predictions, references, aggregate, return_all_scores) and returns: dict, dict