code: def compute_asr_errors(
      output_folder, hyp_segs, ref_segs, mapping=None, uem=None
  ):
      if mapping is not None:  # using diarization
          hyp_segs_reordered = []
          for s in hyp_segs:
              new_segment = deepcopy(s)
              c_speaker = new_segment["speaker"]
              if c_speaker not in mapping.keys():
                  mapping[c_speaker] = (
                      "FA_" + c_speaker
                  )  # false speaker
              new_segment["speaker"] = mapping[c_speaker]
              hyp_segs_reordered.append(new_segment)
          hyp_segs = hyp_segs_reordered
  
      def spk2utts(segs, uem=None):
          st_uem, end_uem = uem
          spk2utt = OrderedDict({k["speaker"]: [] for k in segs})
          for s in segs:
              start = float(s["start_time"])
              end = float(s["end_time"])
              # discard segments whose end is in the uem.
              if uem is not None and (end < st_uem or start > end_uem):
                  continue
              spk2utt[s["speaker"]].append(s)
  
          for spk in spk2utt.keys():
              spk2utt[spk] = sorted(
                  spk2utt[spk], key=lambda x: float(x["start_time"])
              )
          return spk2utt
  
      hyp = spk2utts(hyp_segs, uem)
      ref = spk2utts(ref_segs, uem)
  
      log_asr(output_folder, hyp, ref)
  
      if mapping is not None:
          # check if they have same speakers
          false_speakers = set(hyp.keys()).difference(set(ref.keys()))
          if false_speakers:
              for f_spk in list(false_speakers):
                  ref[f_spk] = [{"words": "", "speaker": f_spk}]
          missed_speakers = set(ref.keys()).difference(set(hyp.keys()))
          if missed_speakers:
              for m_spk in list(missed_speakers):
                  hyp[m_spk] = [{"words": "", "speaker": m_spk}]
  
      tot_stats = {
          "hits": 0,
          "substitutions": 0,
          "deletions": 0,
          "insertions": 0,
      }
      speakers_stats = []
      for spk in ref.keys():
          cat_refs = " ".join([x["words"] for x in ref[spk]])
          cat_hyps = " ".join([x["words"] for x in hyp[spk]])
          if len(cat_refs) == 0:
              # need this because jiwer cannot handle empty refs
              ldist = {
                  "hits": 0,
                  "substitutions": 0,
                  "deletions": 0,
                  "insertions": len(cat_hyps.split()),
              }
          else:
              wordsout = jiwer.process_words(cat_refs, cat_hyps)
              ldist = {
                  k: v
                  for k, v in wordsout.__dict__.items()
                  if k in tot_stats.keys()
              }
  
          ldist.update(
              {
                  "speaker": spk,
                  "tot utterances ref": len(ref[spk]),
                  "tot utterances hyp": len(hyp[spk]),
              }
          )
          speakers_stats.append(ldist)
          for k in tot_stats.keys():
              tot_stats[k] += ldist[k]
  
      c_wer = compute_wer(tot_stats)
      tot_stats.update({"wer": c_wer})
  
      if DEBUG:
          if mapping is not None:
              # remove the dummy speakers
              if missed_speakers:
                  for m_spk in list(missed_speakers):
                      del hyp[m_spk]
              if false_speakers:
                  for f_spk in list(false_speakers):
                      del ref[f_spk]
          # check consistency with meeteval cpWER, note if diarization is bad
          # this consistency will fail.
          cp_wer = meeteval_wer.cp_word_error_rate(
              {
                  k: " ".join([x["words"] for x in ref[k]])
                  for k in ref.keys()
              },
              {
                  k: " ".join([x["words"] for x in hyp[k]])
                  for k in hyp.keys()
              },
          )
          assert abs(cp_wer.error_rate - c_wer) < 1e-4
  
      return tot_stats, speakers_stats
  
imports: import argparse
  import glob
  import json
  import os
  import pickle
  import warnings
  from collections import OrderedDict
  from copy import deepcopy
  from pathlib import Path
  from typing import Dict, Optional
  import jiwer
  import pandas
  from pyannote.core.utils.types import Label
  from pyannote.metrics.diarization import DiarizationErrorRate
  from pyannote.metrics.errors.identification import IdentificationErrorAnalysis
  from pyannote.metrics.identification import IdentificationErrorRate
  from pyannote.metrics.matcher import HungarianMapper
  from pyannote.metrics.segmentation import Annotation, Segment, Timeline
  from pyannote.metrics.types import MetricComponents
  from tabulate import tabulate
  from tqdm import tqdm
label_desc: Use image node_id 6 for calling a global function with example usage: compute_asr_errors(output_folder, hyp_segs, ref_segs, mapping, uem) and returns: tot_stats,  speakers_stats, spk2utt