


import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__snake_case         =					logging.get_logger(__name__)

__snake_case         =					{
    '''microsoft/unispeech-sat-base-100h-libri-ft''': (
        '''https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'''
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}







class _lowerCAmelCase       (   snake_case_			):
   __UpperCAmelCase				: Union[str, Any]													=						'''unispeech-sat'''







   def __init__(			self		,			UpperCamelCase__=32		,			UpperCamelCase__=768		,			UpperCamelCase__=12		,			UpperCamelCase__=12		,			UpperCamelCase__=3072		,			UpperCamelCase__="gelu"		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.0		,			UpperCamelCase__=0.0		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.02		,			UpperCamelCase__=1e-5		,			UpperCamelCase__="group"		,			UpperCamelCase__="gelu"		,			UpperCamelCase__=(512, 512, 512, 512, 512, 512, 512)		,			UpperCamelCase__=(5, 2, 2, 2, 2, 2, 2)		,			UpperCamelCase__=(10, 3, 3, 3, 3, 2, 2)		,			UpperCamelCase__=False		,			UpperCamelCase__=128		,			UpperCamelCase__=16		,			UpperCamelCase__=False		,			UpperCamelCase__=True		,			UpperCamelCase__=0.05		,			UpperCamelCase__=10		,			UpperCamelCase__=2		,			UpperCamelCase__=0.0		,			UpperCamelCase__=10		,			UpperCamelCase__=0		,			UpperCamelCase__=320		,			UpperCamelCase__=2		,			UpperCamelCase__=0.1		,			UpperCamelCase__=100		,			UpperCamelCase__=256		,			UpperCamelCase__=256		,			UpperCamelCase__=0.1		,			UpperCamelCase__="mean"		,			UpperCamelCase__=False		,			UpperCamelCase__=False		,			UpperCamelCase__=256		,			UpperCamelCase__=(512, 512, 512, 512, 1500)		,			UpperCamelCase__=(5, 3, 3, 1, 1)		,			UpperCamelCase__=(1, 2, 3, 1, 1)		,			UpperCamelCase__=512		,			UpperCamelCase__=0		,			UpperCamelCase__=1		,			UpperCamelCase__=2		,			UpperCamelCase__=504		,			**UpperCamelCase__		,			)			->	List[Any]:

       '''simple docstring'''




       super().__init__(**UpperCamelCase_		,			pad_token_id=UpperCamelCase_		,			bos_token_id=UpperCamelCase_		,			eos_token_id=UpperCamelCase_		)
       snake_case					:   int         =					hidden_size
       snake_case					:   Union[str, Any]         =					feat_extract_norm
       snake_case					:   Union[str, Any]         =					feat_extract_activation
       snake_case					:   Optional[Any]         =					list(UpperCamelCase_		)
       snake_case					:   Optional[int]         =					list(UpperCamelCase_		)
       snake_case					:   Dict         =					list(UpperCamelCase_		)
       snake_case					:   Tuple         =					conv_bias
       snake_case					:   Dict         =					num_conv_pos_embeddings
       snake_case					:   List[Any]         =					num_conv_pos_embedding_groups
       snake_case					:   List[Any]         =					len(self.conv_dim		)
       snake_case					:   List[str]         =					num_hidden_layers
       snake_case					:   List[Any]         =					intermediate_size
       snake_case					:   List[Any]         =					hidden_act
       snake_case					:   int         =					num_attention_heads
       snake_case					:   Dict         =					hidden_dropout
       snake_case					:   Optional[Any]         =					attention_dropout
       snake_case					:   List[str]         =					activation_dropout
       snake_case					:   List[Any]         =					feat_proj_dropout
       snake_case					:   Optional[int]         =					final_dropout
       snake_case					:   List[str]         =					layerdrop
       snake_case					:   Optional[Any]         =					layer_norm_eps
       snake_case					:   Optional[int]         =					initializer_range
       snake_case					:   Tuple         =					vocab_size
       snake_case					:   List[str]         =					num_clusters
       snake_case					:   Tuple         =					do_stable_layer_norm
       snake_case					:   Any         =					use_weighted_layer_sum

       if (
           (len(self.conv_stride		) != self.num_feat_extract_layers)
           or (len(self.conv_kernel		) != self.num_feat_extract_layers)
           or (len(self.conv_dim		) != self.num_feat_extract_layers)
       ):
           raise ValueError(
               "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
               " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
               F' {len(self.conv_dim		)}`, `len(config.conv_stride) = {len(self.conv_stride		)}`,'
               F' `len(config.conv_kernel) = {len(self.conv_kernel		)}`.'		)

       # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
       snake_case					:   Tuple         =					apply_spec_augment
       snake_case					:   List[str]         =					mask_time_prob
       snake_case					:   List[str]         =					mask_time_length
       snake_case					:   Dict         =					mask_time_min_masks
       snake_case					:   List[str]         =					mask_feature_prob
       snake_case					:   Optional[int]         =					mask_feature_length
       snake_case					:   Tuple         =					mask_feature_min_masks

       # parameters for pretraining with codevector quantized representations
       snake_case					:   List[Any]         =					num_codevectors_per_group
       snake_case					:   Optional[int]         =					num_codevector_groups
       snake_case					:   str         =					contrastive_logits_temperature
       snake_case					:   Union[str, Any]         =					feat_quantizer_dropout
       snake_case					:   Tuple         =					num_negatives
       snake_case					:   Dict         =					codevector_dim
       snake_case					:   Tuple         =					proj_codevector_dim
       snake_case					:   Optional[int]         =					diversity_loss_weight

       # ctc loss
       snake_case					:   List[Any]         =					ctc_loss_reduction
       snake_case					:   Dict         =					ctc_zero_infinity

       # SequenceClassification-specific parameter. Feel free to ignore for other classes.
       snake_case					:   Any         =					classifier_proj_size

       # XVector-specific parameters. Feel free to ignore for other classes.
       snake_case					:   Union[str, Any]         =					list(UpperCamelCase_		)
       snake_case					:   str         =					list(UpperCamelCase_		)
       snake_case					:   Tuple         =					list(UpperCamelCase_		)
       snake_case					:   Optional[Any]         =					xvector_output_dim







   @property
   def 				lowerCamelCase	(			self		)			->	Optional[int]:

       '''simple docstring'''




       return functools.reduce(operator.mul		,			self.conv_stride		,			1		)









"""simple docstring"""



from typing import List

import jiwer
import jiwer.transforms as tr
from packaging import version

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse("""3.8"""):
  import importlib_metadata
else:
  import importlib.metadata as importlib_metadata


__snake_case         =					""""""


if version.parse(importlib_metadata.version("""jiwer""")) < version.parse("""2.3.0"""):







  class _lowerCAmelCase       (   tr.AbstractTransform			):







     def __init__(			self		,			UpperCamelCase__ = " "		)			->	Any:

         '''simple docstring'''




         snake_case					:   Optional[Any]         =					sentence_delimiter







     def 				lowerCamelCase	(			self		,			UpperCamelCase__		)			->	Optional[Any]:

         '''simple docstring'''




         return list(UpperCamelCase__		)







     def 				lowerCamelCase	(			self		,			UpperCamelCase__		)			->	List[Any]:

         '''simple docstring'''




         snake_case					:   Dict         =					[]
         for sent_idx, sentence in enumerate(UpperCamelCase__		):
             chars.extend(self.process_string(UpperCamelCase__		)		)
             if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(UpperCamelCase__		) - 1:
                 chars.append(self.sentence_delimiter		)
         return chars



  __snake_case         =					tr.Compose(
      [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
  )
else:
  __snake_case         =					tr.Compose(
      [
          tr.RemoveMultipleSpaces(),
          tr.Strip(),
          tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
          tr.ReduceToListOfListOfChars(),
      ]
  )


__snake_case         =					"""\
@inproceedings{inproceedings,
    author = {Morris, Andrew and Maier, Viktoria and Green, Phil},
    year = {2004},
    month = {01},
    pages = {},
    title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}
}
"""

__snake_case         =					"""\
Character error rate (CER) is a common metric of the performance of an automatic speech recognition system.

CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.

Character error rate can be computed as:

CER = (S + D + I) / N = (S + D + I) / (S + D + C)

where

S is the number of substitutions,
D is the number of deletions,
I is the number of insertions,
C is the number of correct characters,
N is the number of characters in the reference (N=S+D+C).

CER's output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the
performance of the ASR system with a CER of 0 being a perfect score.
"""

__snake_case         =					"""
Computes CER score of transcribed segments against references.
Args:
    references: list of references for each speech input.
    predictions: list of transcribtions to score.
    concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.
Returns:
    (float): the character error rate

Examples:

    >>> predictions = [\"this is the prediction\", \"there is an other sample\"]
    >>> references = [\"this is the reference\", \"there is another one\"]
    >>> cer = datasets.load_metric(\"cer\")
    >>> cer_score = cer.compute(predictions=predictions, references=references)
    >>> print(cer_score)
    0.34146341463414637
"""







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION					,							_KWARGS_DESCRIPTION			)
class _lowerCAmelCase       (   datasets.Metric			):







   def 				lowerCamelCase	(			self		)			->	Optional[int]:

       '''simple docstring'''




       return datasets.MetricInfo(
           description=_DESCRIPTION		,			citation=_CITATION		,			inputs_description=_KWARGS_DESCRIPTION		,			features=datasets.Features(
               {
                   "predictions": datasets.Value("string"		,			id="sequence"		),
                   "references": datasets.Value("string"		,			id="sequence"		),
               }		)		,			codebase_urls=["https://github.com/jitsi/jiwer/"]		,			reference_urls=[
               "https://en.wikipedia.org/wiki/Word_error_rate",
               "https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates",
           ]		,			)







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__=False		)			->	Optional[int]:

       '''simple docstring'''




       if concatenate_texts:
           return jiwer.compute_measures(
               UpperCamelCase__		,			UpperCamelCase__		,			truth_transform=UpperCamelCase__		,			hypothesis_transform=UpperCamelCase__		,			)["wer"]

       snake_case					:   Optional[int]         =					0
       snake_case					:   int         =					0
       for prediction, reference in zip(UpperCamelCase__		,			UpperCamelCase__		):
           snake_case					:   Dict         =					jiwer.compute_measures(
               UpperCamelCase__		,			UpperCamelCase__		,			truth_transform=UpperCamelCase__		,			hypothesis_transform=UpperCamelCase__		,			)
           incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
           total += measures["substitutions"] + measures["deletions"] + measures["hits"]

       return incorrect / total





