class_docstr: CTCBaseSearcher class to be inherited by other
  CTC beam searchers.
  
  This class provides the basic functionalities for
  CTC beam search decoding.
  
  The space_token is required with a non-sentencepiece vocabulary list
  if your transcription is expecting to contain spaces.
  
  Arguments
  ---------
  blank_index : int
      The index of the blank token.
  vocab_list : list
      The list of the vocabulary tokens.
  space_token : int, optional
      The index of the space token. (default: -1)
  kenlm_model_path : str, optional
      The path to the kenlm model. Use .bin for a faster loading.
      If None, no language model will be used. (default: None)
  unigrams : list, optional
      The list of known word unigrams. (default: None)
  alpha : float
      Weight for language model during shallow fusion. (default: 0.5)
  beta : float
      Weight for length score adjustment of during scoring. (default: 1.5)
  unk_score_offset : float
      Amount of log score offset for unknown tokens. (default: -10.0)
  score_boundary : bool
      Whether to have kenlm respect boundaries when scoring. (default: True)
  beam_size : int, optional
      The width of the beam. (default: 100)
  beam_prune_logp : float, optional
      The pruning threshold for the beam. (default: -10.0)
  token_prune_min_logp : float, optional
      The pruning threshold for the tokens. (default: -5.0)
  prune_history : bool, optional
      Whether to prune the history. (default: True)
      Note: when using topk > 1, this should be set to False as
      it is pruning a lot of beams.
  blank_skip_threshold : float, optional
      Skip frames if log_prob(blank) > log(blank_skip_threshold), to speed up decoding.
      Note: This is only used when using the CUDA decoder, and it might worsen the WER/CER results. Use it at your own risk. (default: 1.0)
  topk : int, optional
      The number of top hypotheses to return. (default: 1)
  spm_token: str, optional
      The sentencepiece token. (default: "▁")
  
  Example
  -------
  >>> blank_index = 0
  >>> vocab_list = ['blank', 'a', 'b', 'c', ' ']
  >>> space_token = ' '
  >>> kenlm_model_path = None
  >>> unigrams = None
  >>> beam_size = 100
  >>> beam_prune_logp = -10.0
  >>> token_prune_min_logp = -5.0
  >>> prune_history = True
  >>> blank_skip_threshold = 1.0
  >>> topk = 1
  >>> searcher = CTCBaseSearcher(
  ...     blank_index=blank_index,
  ...     vocab_list=vocab_list,
  ...     space_token=space_token,
  ...     kenlm_model_path=kenlm_model_path,
  ...     unigrams=unigrams,
  ...     beam_size=beam_size,
  ...     beam_prune_logp=beam_prune_logp,
  ...     token_prune_min_logp=token_prune_min_logp,
  ...     prune_history=prune_history,
  ...     blank_skip_threshold=blank_skip_threshold,
  ...     topk=topk,
  ... )
class_docstr_tok: ["CTCBaseSearcher","class","to","be","inherited","by","other","CTC","beam","searchers",".","This","class","provides","the","basic","functionalities","for","CTC","beam","search","decoding",".","The","space_token","is","required","with","a","non-sentencepiece","vocabulary","list","if","your","transcription","is","expecting","to","contain","spaces",".","Arguments","--","--","--","--","-","blank_index",":","int","The","index","of","the","blank","token",".","vocab_list",":","list","The","list","of","the","vocabulary","tokens",".","space_token",":","int",",","optional","The","index","of","the","space","token",".","(","default",":","-1",")","kenlm_model_path",":","str",",","optional","The","path","to","the","kenlm","model",".","Use",".bin","for","a","faster","loading",".","If","None",",","no","language","model","will","be","used",".","(","default",":","None",")","unigrams",":","list",",","optional","The","list","of","known","word","unigrams",".","(","default",":","None",")","alpha",":","float","Weight","for","language","model","during","shallow","fusion",".","(","default",":","0.5",")","beta",":","float","Weight","for","length","score","adjustment","of","during","scoring",".","(","default",":","1.5",")","unk_score_offset",":","float","Amount","of","log","score","offset","for","unknown","tokens",".","(","default",":","-10.0",")","score_boundary",":","bool","Whether","to","have","kenlm","respect","boundaries","when","scoring",".","(","default",":","True",")","beam_size",":","int",",","optional","The","width","of","the","beam",".","(","default",":","100",")","beam_prune_logp",":","float",",","optional","The","pruning","threshold","for","the","beam",".","(","default",":","-10.0",")","token_prune_min_logp",":","float",",","optional","The","pruning","threshold","for","the","tokens",".","(","default",":","-5.0",")","prune_history",":","bool",",","optional","Whether","to","prune","the","history",".","(","default",":","True",")","Note",":","when","using","topk",">","1",",","this","should","be","set","to","False","as","it","is","pruning","a","lot","of","beams",".","blank_skip_threshold",":","float",",","optional","Skip","frames","if","log_prob","(","blank",")",">","log","(","blank_skip_threshold",")",",","to","speed","up","decoding",".","Note",":","This","is","only","used","when","using","the","CUDA","decoder",",","and","it","might","worsen","the","WER\/CER","results",".","Use","it","at","your","own","risk",".","(","default",":","1.0",")","topk",":","int",",","optional","The","number","of","top","hypotheses","to","return",".","(","default",":","1",")","spm_token",":","str",",","optional","The","sentencepiece","token",".","(","default",":","``","\u2581","''",")","Example","--","--","--","-",">",">",">","blank_index","=","0",">",">",">","vocab_list","=","[","'blank","'",",","'","a","'",",","'","b","'",",","'","c","'",",","'","'","]",">",">",">","space_token","=","'","'",">",">",">","kenlm_model_path","=","None",">",">",">","unigrams","=","None",">",">",">","beam_size","=","100",">",">",">","beam_prune_logp","=","-10.0",">",">",">","token_prune_min_logp","=","-5.0",">",">",">","prune_history","=","True",">",">",">","blank_skip_threshold","=","1.0",">",">",">","topk","=","1",">",">",">","searcher","=","CTCBaseSearcher","(","...","blank_index=blank_index",",","...","vocab_list=vocab_list",",","...","space_token=space_token",",","...","kenlm_model_path=kenlm_model_path",",","...","unigrams=unigrams",",","...","beam_size=beam_size",",","...","beam_prune_logp=beam_prune_logp",",","...","token_prune_min_logp=token_prune_min_logp",",","...","prune_history=prune_history",",","...","blank_skip_threshold=blank_skip_threshold",",","...","topk=topk",",","...",")"]
code: def __init__(
      self,
      blank_index: int,
      vocab_list: List[str],
      space_token: str = " ",
      kenlm_model_path: Union[None, str] = None,
      unigrams: Union[None, List[str]] = None,
      alpha: float = 0.5,
      beta: float = 1.5,
      unk_score_offset: float = -10.0,
      score_boundary: bool = True,
      beam_size: int = 100,
      beam_prune_logp: int = -10.0,
      token_prune_min_logp: int = -5.0,
      prune_history: bool = True,
      blank_skip_threshold: Union[None, int] = 1.0,
      topk: int = 1,
      spm_token: str = "▁",
  ):
      super().__init__()
  
      self.blank_index = blank_index
      self.vocab_list = vocab_list
      self.space_token = space_token
      self.kenlm_model_path = kenlm_model_path
      self.unigrams = unigrams
      self.alpha = alpha
      self.beta = beta
      self.unk_score_offset = unk_score_offset
      self.score_boundary = score_boundary
      self.beam_size = beam_size
      self.beam_prune_logp = beam_prune_logp
      self.token_prune_min_logp = token_prune_min_logp
      self.prune_history = prune_history
      self.blank_skip_threshold = math.log(blank_skip_threshold)
      self.topk = topk
      self.spm_token = spm_token
  
      # check if the vocab is coming from SentencePiece
      self.is_spm = any(
          [str(s).startswith(self.spm_token) for s in vocab_list]
      )
  
      # fetch the index of space_token
      if not self.is_spm:
          try:
              self.space_index = vocab_list.index(space_token)
          except ValueError:
              logger.warning(
                  f"space_token `{space_token}` not found in the vocabulary."
                  "Using value -1 as `space_index`."
                  "Note: If your transcription is not expected to contain spaces, "
                  "you can ignore this warning."
              )
              self.space_index = -1
          logger.info(
              f"Found `space_token` at index {self.space_index}."
          )
  
      self.kenlm_model = None
      if kenlm_model_path is not None:
          try:
              import kenlm  # type: ignore
  
              from speechbrain.decoders.language_model import (
                  LanguageModel,
                  load_unigram_set_from_arpa,
              )
          except ImportError:
              raise ImportError(
                  "kenlm python bindings are not installed. To install it use: "
                  "pip install https://github.com/kpu/kenlm/archive/master.zip"
              )
  
          self.kenlm_model = kenlm.Model(kenlm_model_path)
  
      if kenlm_model_path is not None and kenlm_model_path.endswith(
          ".arpa"
      ):
          logger.info(
              "Using arpa instead of binary LM file, decoder instantiation might be slow."
          )
  
      if unigrams is None and kenlm_model_path is not None:
          if kenlm_model_path.endswith(".arpa"):
              unigrams = load_unigram_set_from_arpa(kenlm_model_path)
          else:
              logger.warning(
                  "Unigrams not provided and cannot be automatically determined from LM file (only "
                  "arpa format). Decoding accuracy might be reduced."
              )
  
      if self.kenlm_model is not None:
          self.lm = LanguageModel(
              kenlm_model=self.kenlm_model,
              unigrams=unigrams,
              alpha=self.alpha,
              beta=self.beta,
              unk_score_offset=self.unk_score_offset,
              score_boundary=self.score_boundary,
          )
      else:
          self.lm = None
  
imports: from itertools import groupby
  from speechbrain.dataio.dataio import length_to_mask
  import math
  import dataclasses
  import numpy
  import heapq
  import logging
  import torch
  import warnings
  from typing import Dict, List, Optional, Union, Any, Tuple
label_desc: Use image node_id 1 to create a new CTCBaseSearcher object from inherited base classes: torch.nn with example: obj = CTCBaseSearcher(blank_index, vocab_list, space_token, kenlm_model_path, unigrams, alpha, beta, unk_score_offset, score_boundary, beam_size, beam_prune_logp, token_prune_min_logp, prune_history, blank_skip_threshold, topk, spm_token)