code: def calculate_session_cpWER(
      spk_hypothesis: List[str],
      spk_reference: List[str],
      use_lsa_only: bool = False,
  ) -> Tuple[float, str, str]:
      """
      Calculate a session-level concatenated minimum-permutation word error rate (cpWER) value. cpWER is
      a scoring method that can evaluate speaker diarization and speech recognition performance at the same time.
      cpWER is calculated by going through the following steps.
  
      1. Concatenate all utterances of each speaker for both reference and hypothesis files.
      2. Compute the WER between the reference and all possible speaker permutations of the hypothesis.
      3. Pick the lowest WER among them (this is assumed to be the best permutation: `min_perm_hyp_trans`).
  
      cpWER was proposed in the following article:
          CHiME-6 Challenge: Tackling Multispeaker Speech Recognition for Unsegmented Recordings
          https://arxiv.org/pdf/2004.09249.pdf
  
      Implementation:
          - Brute force permutation method for calculating cpWER has a time complexity of `O(n!)`.
          - To reduce the computational burden, linear sum assignment (LSA) algorithm is applied
            (also known as Hungarian algorithm) to find the permutation that leads to the lowest WER.
          - In this implementation, instead of calculating all WER values for all permutation of hypotheses,
            we only calculate WER values of (estimated number of speakers) x (reference number of speakers)
            combinations with `O(n^2)`) time complexity and then select the permutation that yields the lowest
            WER based on LSA algorithm.
          - LSA algorithm has `O(n^3)` time complexity in the worst case.
          - We cannot use LSA algorithm to find the best permutation when there are more hypothesis speakers
            than reference speakers. In this case, we use the brute-force permutation method instead.
  
            Example:
                >>> transcript_A = ['a', 'b', 'c', 'd', 'e', 'f'] # 6 speakers
                >>> transcript_B = ['a c b d', 'e f'] # 2 speakers
  
                [case1] hypothesis is transcript_A, reference is transcript_B
                [case2] hypothesis is transcript_B, reference is transcript_A
  
                LSA algorithm based cpWER is:
                  [case1] 4/6 (4 deletion)
                  [case2] 2/6 (2 substitution)
                brute force permutation based cpWER is:
                  [case1] 0
                  [case2] 2/6 (2 substitution)
  
      Args:
          spk_hypothesis (list):
              List containing the hypothesis transcript for each speaker. A list containing the sequence
              of words is assigned for each speaker.
  
              Example:
              >>> spk_hypothesis = ["hey how are you we that's nice", "i'm good yes hi is your sister"]
  
          spk_reference (list):
              List containing the reference transcript for each speaker. A list containing the sequence
              of words is assigned for each speaker.
  
              Example:
              >>> spk_reference = ["hi how are you well that's nice", "i'm good yeah how is your sister"]
  
      Returns:
          cpWER (float):
              cpWER value for the given session.
          min_perm_hyp_trans (str):
              Hypothesis transcript containing the permutation that minimizes WER. Words are separated by spaces.
          ref_trans (str):
              Reference transcript in an arbitrary permutation. Words are separated by spaces.
      """
      # Get all pairs of (estimated num of spks) x (reference num of spks) combinations
      hyp_ref_pair = [spk_hypothesis, spk_reference]
      all_pairs = list(itertools.product(*hyp_ref_pair))
  
      num_hyp_spks, num_ref_spks = len(spk_hypothesis), len(
          spk_reference
      )
  
      if not use_lsa_only and num_ref_spks < num_hyp_spks:
          # Brute force algorithm when there are more speakers in the hypothesis
          (
              cpWER,
              min_perm_hyp_trans,
              ref_trans,
          ) = calculate_session_cpWER_bruteforce(
              spk_hypothesis, spk_reference
          )
      else:
          # Calculate WER for each speaker in hypothesis with reference
          # There are (number of hyp speakers) x (number of ref speakers) combinations
          lsa_wer_list = []
          for spk_hyp_trans, spk_ref_trans in all_pairs:
              spk_wer = word_error_rate(
                  hypotheses=[spk_hyp_trans], references=[spk_ref_trans]
              )
              lsa_wer_list.append(spk_wer)
  
          # Make a cost matrix and calculate a linear sum assignment on the cost matrix.
          # Row is hypothesis index and column is reference index
          cost_wer = torch.tensor(lsa_wer_list).reshape(
              [len(spk_hypothesis), len(spk_reference)]
          )
          row_hyp_ind, col_ref_ind = linear_sum_assignment(cost_wer)
  
          # In case where hypothesis has more speakers, add words from residual speakers
          hyp_permed = [
              spk_hypothesis[k] for k in np.argsort(col_ref_ind)
          ]
          min_perm_hyp_trans = " ".join(hyp_permed)
  
          # Concatenate the reference transcripts into a string variable
          ref_trans = " ".join(spk_reference)
  
          # Calculate a WER value from the permutation that yields the lowest WER.
          cpWER = word_error_rate(
              hypotheses=[min_perm_hyp_trans], references=[ref_trans]
          )
  
      return cpWER, min_perm_hyp_trans, ref_trans
  
file_path: NeMo/nemo/collections/asr/metrics/der.py
imports: import itertools
  from itertools import permutations
  from typing import Dict, List, Optional, Tuple
  import numpy
  import torch
  from pyannote.core import Segment, Timeline
  from pyannote.metrics.diarization import DiarizationErrorRate
  from nemo.collections.asr.metrics.wer import word_error_rate
  from nemo.collections.asr.parts.utils.optimization_utils import linear_sum_assignment
  from nemo.utils import logging
label_desc: Use image node_id 7 for calling a global function with example usage: calculate_session_cpWER(spk_hypothesis, spk_reference, use_lsa_only) and returns: cpWER,  min_perm_hyp_trans,  ref_trans