






"""simple docstring"""
import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


snake_case   :   Any    		=		logging.get_logger(__name__)



@dataclass
class 					UpperCamelCase__   :







    """simple docstring"""
    __UpperCAmelCase											=   42
    __UpperCAmelCase											=   42
    __UpperCAmelCase											=   None
    __UpperCAmelCase											=   None







    def 							a__ (		self   :  str   ):





           '''simple docstring'''

           return json.dumps(dataclasses.asdict(self   )							,						indent=2   ) + "\n"



@dataclass(frozen=snake_case__)
class 					UpperCamelCase__   :







    """simple docstring"""
    __UpperCAmelCase											=   42
    __UpperCAmelCase											=   None
    __UpperCAmelCase											=   None
    __UpperCAmelCase											=   None







    def 							a__ (		self   :  List[Any]   ):





           '''simple docstring'''

           return json.dumps(dataclasses.asdict(self   )   ) + "\n"



class 					UpperCamelCase__   :







    """simple docstring"""







    def 							a__ (		self   :  List[str]							,						UpperCamelCase_   :  Tuple   ):





           '''simple docstring'''

           raise NotImplementedError()







    def 							a__ (		self   :  Union[str, Any]							,						UpperCamelCase_   :  int   ):





           '''simple docstring'''

           raise NotImplementedError()







    def 							a__ (		self   :  str							,						UpperCamelCase_   :  Optional[Any]   ):





           '''simple docstring'''

           raise NotImplementedError()







    def 							a__ (		self   :  Union[str, Any]							,						UpperCamelCase_   :  Optional[int]   ):





           '''simple docstring'''

           raise NotImplementedError()







    def 							a__ (		self   :  Union[str, Any]   ):





           '''simple docstring'''

           raise NotImplementedError()







    def 							a__ (		self   :  List[str]							,						UpperCamelCase_   :  Any   ):





           '''simple docstring'''

           if len(self.get_labels()   ) > 1:
                  __magic_name__										=						self.get_labels()[int(example.label   )]
           return example







    @classmethod
    def 							a__ (		cls   :  List[str]							,						UpperCamelCase_   :  Any							,						UpperCamelCase_   :  List[str]=None   ):





           '''simple docstring'''

           with open(UpperCamelCase_							,						'r'							,						encoding='utf-8-sig'   ) as f:
                  return list(csv.reader(UpperCamelCase_							,						delimiter='\t'							,						quotechar=UpperCamelCase_   )   )



class 					UpperCamelCase__   (							snake_case__):







    """simple docstring"""







    def __init__(		self   :  Any							,						UpperCamelCase_   :  Dict=None							,						UpperCamelCase_   :  List[Any]=None							,						UpperCamelCase_   :  Optional[int]="classification"							,						UpperCamelCase_   :  str=False   ):





           '''simple docstring'''

           __magic_name__										=						[] if labels is None else labels
           __magic_name__										=						[] if examples is None else examples
           __magic_name__										=						mode
           __magic_name__										=						verbose







    def __len__(		self   :  Any   ):





           '''simple docstring'''

           return len(self.examples   )







    def __getitem__(		self   :  Any							,						UpperCamelCase_   :  Optional[Any]   ):





           '''simple docstring'''

           if isinstance(UpperCamelCase_							,						UpperCamelCase_   ):
                  return SingleSentenceClassificationProcessor(labels=self.labels							,						examples=self.examples[idx]   )
           return self.examples[idx]







    @classmethod
    def 							a__ (		cls   :  Union[str, Any]							,						UpperCamelCase_   :  List[Any]							,						UpperCamelCase_   :  str=""							,						UpperCamelCase_   :  Optional[int]=0							,						UpperCamelCase_   :  List[str]=1							,						UpperCamelCase_   :  Dict=None							,						UpperCamelCase_   :  Dict=False							,						**UpperCamelCase_   :  int   ):





           '''simple docstring'''

           __magic_name__										=						cls(**UpperCamelCase_   )
           processor.add_examples_from_csv(
               UpperCamelCase_							,						split_name=UpperCamelCase_							,						column_label=UpperCamelCase_							,						column_text=UpperCamelCase_							,						column_id=UpperCamelCase_							,						skip_first_row=UpperCamelCase_							,						overwrite_labels=UpperCamelCase_							,						overwrite_examples=UpperCamelCase_							,						)
           return processor







    @classmethod
    def 							a__ (		cls   :  Tuple							,						UpperCamelCase_   :  Optional[Any]							,						UpperCamelCase_   :  Any=None							,						**UpperCamelCase_   :  Optional[int]   ):





           '''simple docstring'''

           __magic_name__										=						cls(**UpperCamelCase_   )
           processor.add_examples(UpperCamelCase_							,						labels=UpperCamelCase_   )
           return processor







    def 							a__ (		self   :  List[str]							,						UpperCamelCase_   :  str							,						UpperCamelCase_   :  int=""							,						UpperCamelCase_   :  Optional[int]=0							,						UpperCamelCase_   :  Dict=1							,						UpperCamelCase_   :  Dict=None							,						UpperCamelCase_   :  Optional[Any]=False							,						UpperCamelCase_   :  Tuple=False							,						UpperCamelCase_   :  Optional[int]=False							,						):





           '''simple docstring'''

           __magic_name__										=						self._read_tsv(UpperCamelCase_   )
           if skip_first_row:
                  __magic_name__										=						lines[1:]
           __magic_name__										=						[]
           __magic_name__										=						[]
           __magic_name__										=						[]
           for i, line in enumerate(UpperCamelCase_   ):
                  texts.append(line[column_text]   )
                  labels.append(line[column_label]   )
                  if column_id is not None:
                         ids.append(line[column_id]   )
                  else:
                         __magic_name__										=						f"""{split_name}-{i}""" if split_name else str(UpperCamelCase_   )
                         ids.append(UpperCamelCase_   )

           return self.add_examples(
               UpperCamelCase_							,						UpperCamelCase_							,						UpperCamelCase_							,						overwrite_labels=UpperCamelCase_							,						overwrite_examples=UpperCamelCase_   )







    def 							a__ (		self   :  Dict							,						UpperCamelCase_   :  Optional[int]							,						UpperCamelCase_   :  Union[str, Any]=None							,						UpperCamelCase_   :  Optional[int]=None							,						UpperCamelCase_   :  str=False							,						UpperCamelCase_   :  str=False   ):





           '''simple docstring'''

           if labels is not None and len(UpperCamelCase_   ) != len(UpperCamelCase_   ):
                  raise ValueError(
                      f"""Text and labels have mismatched lengths {len(UpperCamelCase_   )} and {len(UpperCamelCase_   )}"""   )
           if ids is not None and len(UpperCamelCase_   ) != len(UpperCamelCase_   ):
                  raise ValueError(f"""Text and ids have mismatched lengths {len(UpperCamelCase_   )} and {len(UpperCamelCase_   )}"""   )
           if ids is None:
                  __magic_name__										=						[None] * len(UpperCamelCase_   )
           if labels is None:
                  __magic_name__										=						[None] * len(UpperCamelCase_   )
           __magic_name__										=						[]
           __magic_name__										=						set()
           for text_or_text_and_label, label, guid in zip(UpperCamelCase_							,						UpperCamelCase_							,						UpperCamelCase_   ):
                  if isinstance(UpperCamelCase_							,						(tuple, list)   ) and label is None:
                         __magic_name__										=						text_or_text_and_label
                  else:
                         __magic_name__										=						text_or_text_and_label
                  added_labels.add(UpperCamelCase_   )
                  examples.append(InputExample(guid=UpperCamelCase_							,						text_a=UpperCamelCase_							,						text_b=UpperCamelCase_							,						label=UpperCamelCase_   )   )

           # Update examples
           if overwrite_examples:
                  __magic_name__										=						examples
           else:
                  self.examples.extend(UpperCamelCase_   )

           # Update labels
           if overwrite_labels:
                  __magic_name__										=						list(UpperCamelCase_   )
           else:
                  __magic_name__										=						list(set(self.labels   ).union(UpperCamelCase_   )   )

           return self.examples







    def 							a__ (		self   :  Optional[int]							,						UpperCamelCase_   :  Optional[Any]							,						UpperCamelCase_   :  Tuple=None							,						UpperCamelCase_   :  Tuple=False							,						UpperCamelCase_   :  Optional[int]=0							,						UpperCamelCase_   :  Optional[Any]=True							,						UpperCamelCase_   :  List[str]=None							,						):





           '''simple docstring'''

           if max_length is None:
                  __magic_name__										=						tokenizer.max_len

           __magic_name__										=						{label: i for i, label in enumerate(self.labels   )}

           __magic_name__										=						[]
           for ex_index, example in enumerate(self.examples   ):
                  if ex_index % 1_0_0_0_0 == 0:
                         logger.info(f"""Tokenizing example {ex_index}"""   )

                  __magic_name__										=						tokenizer.encode(
                      example.text_a							,						add_special_tokens=UpperCamelCase_							,						max_length=min(UpperCamelCase_							,						tokenizer.max_len   )							,						)
                  all_input_ids.append(UpperCamelCase_   )

           __magic_name__										=						max(len(UpperCamelCase_   ) for input_ids in all_input_ids   )

           __magic_name__										=						[]
           for ex_index, (input_ids, example) in enumerate(zip(UpperCamelCase_							,						self.examples   )   ):
                  if ex_index % 1_0_0_0_0 == 0:
                         logger.info(f"""Writing example {ex_index}/{len(self.examples   )}"""   )
                  # The mask has 1 for real tokens and 0 for padding tokens. Only real
                  # tokens are attended to.
                  __magic_name__										=						[1 if mask_padding_with_zero else 0] * len(UpperCamelCase_   )

                  # Zero-pad up to the sequence length.
                  __magic_name__										=						batch_length - len(UpperCamelCase_   )
                  if pad_on_left:
                         __magic_name__										=						([pad_token] * padding_length) + input_ids
                         __magic_name__										=						([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
                  else:
                         __magic_name__										=						input_ids + ([pad_token] * padding_length)
                         __magic_name__										=						attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

                  if len(UpperCamelCase_   ) != batch_length:
                         raise ValueError(f"""Error with input length {len(UpperCamelCase_   )} vs {batch_length}"""   )
                  if len(UpperCamelCase_   ) != batch_length:
                         raise ValueError(f"""Error with input length {len(UpperCamelCase_   )} vs {batch_length}"""   )

                  if self.mode == "classification":
                         __magic_name__										=						label_map[example.label]
                  elif self.mode == "regression":
                         __magic_name__										=						float(example.label   )
                  else:
                         raise ValueError(self.mode   )

                  if ex_index < 5 and self.verbose:
                         logger.info('*** Example ***'   )
                         logger.info(f"""guid: {example.guid}"""   )
                         logger.info(f"""input_ids: {" ".join([str(UpperCamelCase_   ) for x in input_ids]   )}"""   )
                         logger.info(f"""attention_mask: {" ".join([str(UpperCamelCase_   ) for x in attention_mask]   )}"""   )
                         logger.info(f"""label: {example.label} (id = {label})"""   )

                  features.append(InputFeatures(input_ids=UpperCamelCase_							,						attention_mask=UpperCamelCase_							,						label=UpperCamelCase_   )   )

           if return_tensors is None:
                  return features
           elif return_tensors == "tf":
                  if not is_tf_available():
                         raise RuntimeError('return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported'   )
                  import tensorflow as tf

                  def gen():
                         for ex in features:
                                yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

                  __magic_name__										=						tf.data.Dataset.from_generator(
                      UpperCamelCase_							,						({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa)							,						({'input_ids': tf.TensorShape([None]   ), 'attention_mask': tf.TensorShape([None]   )}, tf.TensorShape([]   ))							,						)
                  return dataset
           elif return_tensors == "pt":
                  if not is_torch_available():
                         raise RuntimeError('return_tensors set to \'pt\' but PyTorch can\'t be imported'   )
                  import torch
                  from torch.utils.data import TensorDataset

                  __magic_name__										=						torch.tensor([f.input_ids for f in features]							,						dtype=torch.long   )
                  __magic_name__										=						torch.tensor([f.attention_mask for f in features]							,						dtype=torch.long   )
                  if self.mode == "classification":
                         __magic_name__										=						torch.tensor([f.label for f in features]							,						dtype=torch.long   )
                  elif self.mode == "regression":
                         __magic_name__										=						torch.tensor([f.label for f in features]							,						dtype=torch.float   )

                  __magic_name__										=						TensorDataset(UpperCamelCase_							,						UpperCamelCase_							,						UpperCamelCase_   )
                  return dataset
           else:
                  raise ValueError('return_tensors should be one of \'tf\' or \'pt\''   )









"""simple docstring"""
from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


snake_case   :   Tuple    		=		logging.get_logger(__name__)



class 					UpperCamelCase__   (							a_):







    """simple docstring"""
    __UpperCAmelCase											=   ["""input_values""", """attention_mask"""]







    def __init__(		self   :  Union[str, Any]							,						UpperCamelCase_   :  int=1							,						UpperCamelCase_   :  Optional[int]=1_6_0_0_0							,						UpperCamelCase_   :  List[str]=0.0							,						UpperCamelCase_   :  Optional[int]=False							,						UpperCamelCase_   :  Tuple=True							,						**UpperCamelCase_   :  Tuple							,						):





           '''simple docstring'''

           super().__init__(feature_size=UpperCamelCase_							,						sampling_rate=UpperCamelCase_							,						padding_value=UpperCamelCase_							,						**UpperCamelCase_   )
           __magic_name__										=						return_attention_mask
           __magic_name__										=						do_normalize







    @staticmethod
    def 							a__ (		UpperCamelCase_   :  List[np.ndarray]							,						UpperCamelCase_   :  List[np.ndarray]							,						UpperCamelCase_   :  float = 0.0   ):





           '''simple docstring'''

           if attention_mask is not None:
                  __magic_name__										=						np.array(UpperCamelCase_							,						np.intaa   )
                  __magic_name__										=						[]

                  for vector, length in zip(UpperCamelCase_							,						attention_mask.sum(-1   )   ):
                         __magic_name__										=						(vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7   )
                         if length < normed_slice.shape[0]:
                                __magic_name__										=						padding_value

                         normed_input_values.append(UpperCamelCase_   )
           else:
                  __magic_name__										=						[(x - x.mean()) / np.sqrt(x.var() + 1e-7   ) for x in input_values]

           return normed_input_values







    def __call__(		self   :  Any							,						UpperCamelCase_   :  Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]							,						UpperCamelCase_   :  Union[bool, str, PaddingStrategy] = False							,						UpperCamelCase_   :  Optional[int] = None							,						UpperCamelCase_   :  bool = False							,						UpperCamelCase_   :  Optional[int] = None							,						UpperCamelCase_   :  Optional[bool] = None							,						UpperCamelCase_   :  Optional[Union[str, TensorType]] = None							,						UpperCamelCase_   :  Optional[int] = None							,						**UpperCamelCase_   :  Any							,						):





           '''simple docstring'''

           if sampling_rate is not None:
                  if sampling_rate != self.sampling_rate:
                         raise ValueError(
                             f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of"""
                             f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"""
                             f""" {self.sampling_rate} and not {sampling_rate}."""   )
           else:
                  logger.warning(
                      'It is strongly recommended to pass the ``sampling_rate`` argument to this function. '
                      'Failing to do so can result in silent errors that might be hard to debug.'   )

           __magic_name__										=						isinstance(UpperCamelCase_							,						np.ndarray   ) and len(raw_speech.shape   ) > 1
           if is_batched_numpy and len(raw_speech.shape   ) > 2:
                  raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""   )
           __magic_name__										=						is_batched_numpy or (
               isinstance(UpperCamelCase_							,						(list, tuple)   ) and (isinstance(raw_speech[0]							,						(np.ndarray, tuple, list)   ))
           )

           # always return batch
           if not is_batched:
                  __magic_name__										=						[raw_speech]

           # convert into correct format for padding
           __magic_name__										=						BatchFeature({'input_values': raw_speech}   )

           __magic_name__										=						self.pad(
               UpperCamelCase_							,						padding=UpperCamelCase_							,						max_length=UpperCamelCase_							,						truncation=UpperCamelCase_							,						pad_to_multiple_of=UpperCamelCase_							,						return_attention_mask=UpperCamelCase_							,						)

           # convert input values to correct format
           __magic_name__										=						padded_inputs['input_values']
           if not isinstance(input_values[0]							,						np.ndarray   ):
                  __magic_name__										=						[np.asarray(UpperCamelCase_							,						dtype=np.floataa   ) for array in input_values]
           elif (
               not isinstance(UpperCamelCase_							,						np.ndarray   )
               and isinstance(input_values[0]							,						np.ndarray   )
               and input_values[0].dtype is np.dtype(np.floataa   )
           ):
                  __magic_name__										=						[array.astype(np.floataa   ) for array in input_values]
           elif isinstance(UpperCamelCase_							,						np.ndarray   ) and input_values.dtype is np.dtype(np.floataa   ):
                  __magic_name__										=						input_values.astype(np.floataa   )

           # convert attention_mask to correct format
           __magic_name__										=						padded_inputs.get('attention_mask'   )
           if attention_mask is not None:
                  __magic_name__										=						[np.asarray(UpperCamelCase_							,						dtype=np.intaa   ) for array in attention_mask]

           # zero-mean and unit-variance normalization
           if self.do_normalize:
                  __magic_name__										=						(
                      attention_mask
                      if self._get_padding_strategies(UpperCamelCase_							,						max_length=UpperCamelCase_   ) is not PaddingStrategy.DO_NOT_PAD
                      else None
                  )
                  __magic_name__										=						self.zero_mean_unit_var_norm(
                      padded_inputs['input_values']							,						attention_mask=UpperCamelCase_							,						padding_value=self.padding_value   )

           if return_tensors is not None:
                  __magic_name__										=						padded_inputs.convert_to_tensors(UpperCamelCase_   )

           return padded_inputs