class_docstr: Class for computation puncutation-related absolute amounts of operations and thier rates
  between reference and hypothesis strings:
      - Absolute amounts of correct predictions, deletions, insertions
      and substitutions for each given punctuation mark
      - Rates of correct predictions, deletions, insertions
      and substitutions for each given punctuation mark
      - Overall rates of correct predictions, deletions, insertions
      and substiturions between reference and hypothesis string
      - Punctuation Error Rate
  
  Args to init:
      punctuation_marks (list[str]) - list of punctuation marks for computing metrics
      punctuation_mask (str, by default "[PUNCT]") - mask token that will be applied to
      given punctuation marks while edit distance calculation 
  
  How to use:
      1. Create object of OccurancePunctuationErrorRate class.
         Example:
              punctuation_marks = [".", ",", "!", "?"]
              oper_obj = OccurancePunctuationErrorRate(punctuation_marks)
      
      2. To compute punctuation metrics, pass reference and hypothesis string to the "compute" method
      of created object.
          Example:
              reference_str = "Hi, dear! Nice to see you. What's"
              hypothesis_str = "Hi dear! Nice to see you! What's?"
              oper_obj.compute(reference_str, hypothesis_str)
  
  Output (listed in order of output):
      1. Dict of absolute operations amounts for each given punctuation mark:
          Example:
          {'.': {'Correct': 0, 'Deletions': 0, 'Insertions': 0, 'Substitutions': 1},
           ',': {'Correct': 0, 'Deletions': 1, 'Insertions': 0, 'Substitutions': 0},
           '!': {'Correct': 1, 'Deletions': 0, 'Insertions': 0, 'Substitutions': 0},
           '?': {'Correct': 0, 'Deletions': 0, 'Insertions': 1, 'Substitutions': 0}}
            
      2. Dict of substitutions absolute amounts between given punctuation marks:
          Example:
          {'.': {'.': 0, ',': 0, '!': 1, '?': 0},
           ',': {'.': 0, ',': 0, '!': 0, '?': 0},
           '!': {'.': 0, ',': 0, '!': 0, '?': 0},
           '?': {'.': 0, ',': 0, '!': 0, '?': 0}}
          
      3. namedtuple "PunctuationRates" of punctuation operation rates (in range from 0 to 1):
          3.1. correct_rate - overall correct rate 
              Example: correct_rate=0.25
          3.2. deletions_rate - overall deletions rate
              Example: deletions_rate=0.25
          3.3. insertions_rate - overall insertions rate
              Example: insertions_rate=0.25
          3.4. substitutions_rate - overall substitutions_rate
              Example: substitutions_rate=0.25
          3.5. punct_er - Punctuation Error Rate
              Example: punct_er=0.75
          3.6. operation_rates - dict of operations rates for each given punctuation mark
              Example: 
              operation_rates={
                  '.': {'Correct': 0.0, 'Deletions': 0.0, 'Insertions': 0.0, 'Substitutions': 1.0},
                  ',': {'Correct': 0.0, 'Deletions': 1.0, 'Insertions': 0.0, 'Substitutions': 0.0},
                  '!': {'Correct': 1.0, 'Deletions': 0.0, 'Insertions': 0.0, 'Substitutions': 0.0},
                  '?': {'Correct': 0.0, 'Deletions': 0.0, 'Insertions': 1.0, 'Substitutions': 0.0}
                  }
  
          3.7. substitution_rates - dict of substitution rates for each given punctuation mark
              Example:
              substitution_rates={
                  '.': {'.': 0.0, ',': 0.0, '!': 1.0, '?': 0.0},
                  ',': {'.': 0.0, ',': 0.0, '!': 0.0, '?': 0.0},
                  '!': {'.': 0.0, ',': 0.0, '!': 0.0, '?': 0.0},
                  '?': {'.': 0.0, ',': 0.0, '!': 0.0, '?': 0.0}
                  }
class_docstr_tok: ["Class","for","computation","puncutation-related","absolute","amounts","of","operations","and","thier","rates","between","reference","and","hypothesis","strings",":","-","Absolute","amounts","of","correct","predictions",",","deletions",",","insertions","and","substitutions","for","each","given","punctuation","mark","-","Rates","of","correct","predictions",",","deletions",",","insertions","and","substitutions","for","each","given","punctuation","mark","-","Overall","rates","of","correct","predictions",",","deletions",",","insertions","and","substiturions","between","reference","and","hypothesis","string","-","Punctuation","Error","Rate","Args","to","init",":","punctuation_marks","(","list","[","str","]",")","-","list","of","punctuation","marks","for","computing","metrics","punctuation_mask","(","str",",","by","default","``","[","PUNCT","]","''",")","-","mask","token","that","will","be","applied","to","given","punctuation","marks","while","edit","distance","calculation","How","to","use",":","1",".","Create","object","of","OccurancePunctuationErrorRate","class",".","Example",":","punctuation_marks","=","[","``",".","``",",","``",",","''",",","``","!","``",",","``","?","''","]","oper_obj","=","OccurancePunctuationErrorRate","(","punctuation_marks",")","2",".","To","compute","punctuation","metrics",",","pass","reference","and","hypothesis","string","to","the","``","compute","''","method","of","created","object",".","Example",":","reference_str","=","``","Hi",",","dear","!","Nice","to","see","you",".","What","'s","''","hypothesis_str","=","``","Hi","dear","!","Nice","to","see","you","!","What","'s","?","''","oper_obj.compute","(","reference_str",",","hypothesis_str",")","Output","(","listed","in","order","of","output",")",":","1",".","Dict","of","absolute","operations","amounts","for","each","given","punctuation","mark",":","Example",":","{","'",".","'",":","{","'Correct","'",":","0",",","'Deletions","'",":","0",",","'Insertions","'",":","0",",","'Substitutions","'",":","1","}",",","'",",","'",":","{","'Correct","'",":","0",",","'Deletions","'",":","1",",","'Insertions","'",":","0",",","'Substitutions","'",":","0","}",",","'","!","'",":","{","'Correct","'",":","1",",","'Deletions","'",":","0",",","'Insertions","'",":","0",",","'Substitutions","'",":","0","}",",","'","?","'",":","{","'Correct","'",":","0",",","'Deletions","'",":","0",",","'Insertions","'",":","1",",","'Substitutions","'",":","0","}","}","2",".","Dict","of","substitutions","absolute","amounts","between","given","punctuation","marks",":","Example",":","{","'",".","'",":","{","'",".","'",":","0",",","'",",","'",":","0",",","'","!","'",":","1",",","'","?","'",":","0","}",",","'",",","'",":","{","'",".","'",":","0",",","'",",","'",":","0",",","'","!","'",":","0",",","'","?","'",":","0","}",",","'","!","'",":","{","'",".","'",":","0",",","'",",","'",":","0",",","'","!","'",":","0",",","'","?","'",":","0","}",",","'","?","'",":","{","'",".","'",":","0",",","'",",","'",":","0",",","'","!","'",":","0",",","'","?","'",":","0","}","}","3.","namedtuple","``","PunctuationRates","''","of","punctuation","operation","rates","(","in","range","from","0","to","1",")",":","3.1.","correct_rate","-","overall","correct","rate","Example",":","correct_rate=0.25","3.2.","deletions_rate","-","overall","deletions","rate","Example",":","deletions_rate=0.25","3.3.","insertions_rate","-","overall","insertions","rate","Example",":","insertions_rate=0.25","3.4.","substitutions_rate","-","overall","substitutions_rate","Example",":","substitutions_rate=0.25","3.5.","punct_er","-","Punctuation","Error","Rate","Example",":","punct_er=0.75","3.6.","operation_rates","-","dict","of","operations","rates","for","each","given","punctuation","mark","Example",":","operation_rates=","{","'",".","'",":","{","'Correct","'",":","0.0",",","'Deletions","'",":","0.0",",","'Insertions","'",":","0.0",",","'Substitutions","'",":","1.0","}",",","'",",","'",":","{","'Correct","'",":","0.0",",","'Deletions","'",":","1.0",",","'Insertions","'",":","0.0",",","'Substitutions","'",":","0.0","}",",","'","!","'",":","{","'Correct","'",":","1.0",",","'Deletions","'",":","0.0",",","'Insertions","'",":","0.0",",","'Substitutions","'",":","0.0","}",",","'","?","'",":","{","'Correct","'",":","0.0",",","'Deletions","'",":","0.0",",","'Insertions","'",":","1.0",",","'Substitutions","'",":","0.0","}","}","3.7.","substitution_rates","-","dict","of","substitution","rates","for","each","given","punctuation","mark","Example",":","substitution_rates=","{","'",".","'",":","{","'",".","'",":","0.0",",","'",",","'",":","0.0",",","'","!","'",":","1.0",",","'","?","'",":","0.0","}",",","'",",","'",":","{","'",".","'",":","0.0",",","'",",","'",":","0.0",",","'","!","'",":","0.0",",","'","?","'",":","0.0","}",",","'","!","'",":","{","'",".","'",":","0.0",",","'",",","'",":","0.0",",","'","!","'",":","0.0",",","'","?","'",":","0.0","}",",","'","?","'",":","{","'",".","'",":","0.0",",","'",",","'",":","0.0",",","'","!","'",":","0.0",",","'","?","'",":","0.0","}","}"]
code: def compute_rates(
      self, operation_amounts: dict, substitution_amounts: dict
  ):
      operation_rates = {
          pm: {operation: 0 for operation in self.operations}
          for pm in self.punctuation_marks
      }
      substitution_rates = {
          pm: {pm: 0 for pm in self.punctuation_marks}
          for pm in self.punctuation_marks
      }
  
      for pm in self.punctuation_marks:
          operations_amount_by_pm = sum(operation_amounts[pm].values())
  
          if operations_amount_by_pm == 0:
              continue
  
          operation_rates[pm] = {
              operation: (
                  operation_amounts[pm][operation]
                  / operations_amount_by_pm
              )
              for operation in self.operations
          }
  
          substitution_rates[pm] = {
              _pm: (
                  substitution_amounts[pm][_pm]
                  / operations_amount_by_pm
              )
              for _pm in substitution_amounts[pm].keys()
          }
  
      _operation_amounts = {
          operation: {
              pm: operation_amounts[operation]
              for pm, operation_amounts in operation_amounts.items()
          }
          for operation in self.operations
      }
  
      overall_amounts_by_operation = {
          operation: sum(_operation_amounts[operation].values())
          for operation in _operation_amounts
      }
      overall_operations_amount = sum(
          overall_amounts_by_operation.values()
      )
  
      punctuation_rates = namedtuple(
          "PunctuationRates",
          [
              "correct_rate",
              "deletions_rate",
              "insertions_rate",
              "substitutions_rate",
              "punct_er",
              "operation_rates",
              "substitution_rates",
          ],
      )
  
      if overall_operations_amount == 0:
          rates = punctuation_rates(
              0, 0, 0, 0, 0, operation_rates, substitution_rates
          )
      else:
          correct_rate = (
              overall_amounts_by_operation["Correct"]
              / overall_operations_amount
          )
          deletions_rate = (
              overall_amounts_by_operation["Deletions"]
              / overall_operations_amount
          )
          insertions_rate = (
              overall_amounts_by_operation["Insertions"]
              / overall_operations_amount
          )
          substitutions_rate = (
              overall_amounts_by_operation["Substitutions"]
              / overall_operations_amount
          )
          punct_er = (
              deletions_rate + insertions_rate + substitutions_rate
          )
  
          rates = punctuation_rates(
              correct_rate,
              deletions_rate,
              insertions_rate,
              substitutions_rate,
              punct_er,
              operation_rates,
              substitution_rates,
          )
  
      return rates
  
file_path: NeMo/nemo/collections/common/metrics/punct_er.py
imports: import re
  from collections import namedtuple
  from tqdm import tqdm
  from nemo.utils import logging
label_desc: Use image node_id 2 for calling the OccurancePunctuationErrorRate obj's underlying member method code with example usage: obj.compute_rates(operation_amounts, substitution_amounts) and returns: rates