class_docstr: A convenience class for calculating the Levenshtein edit distance.
  
  Class will cache some intermediate values to hasten the calculation. The implementation follows the implementation
  from https://github.com/mjpost/sacrebleu/blob/master/sacrebleu/metrics/lib_ter.py,
  where the most of this implementation is adapted and copied from.
  
  Args:
      reference_tokens: list of reference tokens
      op_insert: cost of insertion operation
      op_delete: cost of deletion operation
      op_substitute: cost of substitution operation
class_docstr_tok: ["A","convenience","class","for","calculating","the","Levenshtein","edit","distance",".","Class","will","cache","some","intermediate","values","to","hasten","the","calculation",".","The","implementation","follows","the","implementation","from","https",":","\/\/github.com\/mjpost\/sacrebleu\/blob\/master\/sacrebleu\/metrics\/lib_ter.py",",","where","the","most","of","this","implementation","is","adapted","and","copied","from",".","Args",":","reference_tokens",":","list","of","reference","tokens","op_insert",":","cost","of","insertion","operation","op_delete",":","cost","of","deletion","operation","op_substitute",":","cost","of","substitution","operation"]
docstr: Add newly computed rows to cache.
  
  Since edit distance is only calculated on the hypothesis suffix that was not in cache, the number of rows in
  `edit_distance` matrx may be shorter than hypothesis length. In that case we skip over these initial words.
  
  Args:
      prediction_tokens: A tokenized predicted sentence.
      edit_distance:
          A matrix of the Levenshtedin edit distance. The element part of the matrix is a tuple of an edit
          operation cost and an edit operation itself.
docstr_tok: ["Add","newly","computed","rows","to","cache",".","Since","edit","distance","is","only","calculated","on","the","hypothesis","suffix","that","was","not","in","cache",",","the","number","of","rows","in","`","edit_distance","`","matrx","may","be","shorter","than","hypothesis","length",".","In","that","case","we","skip","over","these","initial","words",".","Args",":","prediction_tokens",":","A","tokenized","predicted","sentence",".","edit_distance",":","A","matrix","of","the","Levenshtedin","edit","distance",".","The","element","part","of","the","matrix","is","a","tuple","of","an","edit","operation","cost","and","an","edit","operation","itself","."]
code: def _add_cache(
      self,
      prediction_tokens: List[str],
      edit_distance: List[List[Tuple[int, _EditOperations]]],
  ) -> None:
      """Add newly computed rows to cache.
  
      Since edit distance is only calculated on the hypothesis suffix that was not in cache, the number of rows in
      `edit_distance` matrx may be shorter than hypothesis length. In that case we skip over these initial words.
  
      Args:
          prediction_tokens: A tokenized predicted sentence.
          edit_distance:
              A matrix of the Levenshtedin edit distance. The element part of the matrix is a tuple of an edit
              operation cost and an edit operation itself.
  
      """
      if self.cache_size >= _MAX_CACHE_SIZE:
          return
  
      node = self.cache
  
      # how many initial words to skip
      skip_num = len(prediction_tokens) - len(edit_distance)
  
      # Jump through the cache to the current position
      for i in range(skip_num):
          node = node[prediction_tokens[i]][0]  # type: ignore
  
      # Update cache with newly computed rows
      for word, row in zip(prediction_tokens[skip_num:], edit_distance):
          if word not in node:
              node[word] = ({}, tuple(row))  # type: ignore
              self.cache_size += 1
          value = node[word]
          node = value[0]
  
file_path: torchmetrics/src/torchmetrics/functional/text/helper.py
imports: import math
  from enum import Enum, unique
  from typing import Dict, List, Sequence, Tuple, Union
label_desc: Use image node_id 5 for calling the _LevenshteinEditDistance obj's underlying member method code with example usage: obj._add_cache(prediction_tokens, edit_distance) without return types