





'''simple docstring'''







from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

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


snake_case_  :							Optional[int]            =	logging.get_logger(__name__)


class        __a   (lowerCamelCase  ):
							__a       : List[str]          =							["input_values", "attention_mask"]

							def __init__(				self				:  List[str]							,  __magic_name__				:  str=1							,  __magic_name__				:  List[str]=1_60_00							,  __magic_name__				:  Any=1_28							,  __magic_name__				:  List[Any]=10_24							,  __magic_name__				:  Optional[Any]=0.0							,  __magic_name__				:  Optional[Any]=True							,  __magic_name__				:  Optional[int]=-4.2_6_7_7_3_9_3							,  __magic_name__				:  List[Any]=4.5_6_8_9_9_7_4							,  __magic_name__				:  Dict=False							,  **__magic_name__				:  Tuple							,  )       ->							Optional[Any]:







													"""simple docstring"""
													super().__init__(feature_size=__magic_name__							,  sampling_rate=__magic_name__							,  padding_value=__magic_name__							,  **__magic_name__ )
													UpperCAmelCase_      :   Tuple      =	num_mel_bins
													UpperCAmelCase_      :   Union[str, Any]      =	max_length
													UpperCAmelCase_      :   Dict      =	do_normalize
													UpperCAmelCase_      :   int      =	mean
													UpperCAmelCase_      :   int      =	std
													UpperCAmelCase_      :   int      =	return_attention_mask

							def      UpperCAmelCase__						(				self				:  Dict							,  __magic_name__				:  np.ndarray							,  __magic_name__				:  int							,  )       ->							np.ndarray:







													"""simple docstring"""
													UpperCAmelCase_      :   str      =	torch.from_numpy(__magic_name__ ).unsqueeze(0 )
													UpperCAmelCase_      :   Dict      =	ta_kaldi.fbank(
													    __magic_name__							,  htk_compat=__magic_name__							,  sample_frequency=self.sampling_rate							,  use_energy=__magic_name__							,  window_type='''hanning'''							,  num_mel_bins=self.num_mel_bins							,  dither=0.0							,  frame_shift=10							,  )

													UpperCAmelCase_      :   int      =	fbank.shape[0]
													UpperCAmelCase_      :   int      =	max_length - n_frames

													# pad or truncate, depending on difference
													if difference > 0:
																			UpperCAmelCase_      :   Any      =	torch.nn.ZeroPadad((0, 0, 0, difference) )
																			UpperCAmelCase_      :   Optional[Any]      =	pad_module(__magic_name__ )
													elif difference < 0:
																			UpperCAmelCase_      :   List[str]      =	fbank[0:max_length, :]

													UpperCAmelCase_      :   Dict      =	fbank.numpy()

													return fbank

							def      UpperCAmelCase__						(				self				:  Any							,  __magic_name__				:  np.ndarray )       ->							np.ndarray:







													"""simple docstring"""
													return (input_values - (self.mean)) / (self.std * 2)



							def __call__(				self				:  Dict							,  __magic_name__				:  Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]							,  __magic_name__				:  Optional[int] = None							,  __magic_name__				:  Optional[Union[str, TensorType]] = None							,  **__magic_name__				:  Union[str, Any]							,  )       ->							BatchFeature:







													"""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.''' )

													UpperCAmelCase_      :   Tuple      =	isinstance(__magic_name__							,  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}""" )
													UpperCAmelCase_      :   Dict      =	is_batched_numpy or (
													    isinstance(__magic_name__							,  (list, tuple) ) and (isinstance(raw_speech[0]							,  (np.ndarray, tuple, list) ))
													)

													if is_batched:
																			UpperCAmelCase_      :   int      =	[np.asarray(__magic_name__							,  dtype=np.floataa ) for speech in raw_speech]
													elif not is_batched and not isinstance(__magic_name__							,  np.ndarray ):
																			UpperCAmelCase_      :   Optional[int]      =	np.asarray(__magic_name__							,  dtype=np.floataa )
													elif isinstance(__magic_name__							,  np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ):
																			UpperCAmelCase_      :   int      =	raw_speech.astype(np.floataa )

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

													# extract fbank features and pad/truncate to max_length
													UpperCAmelCase_      :   int      =	[self._extract_fbank_features(__magic_name__							,  max_length=self.max_length ) for waveform in raw_speech]

													# convert into BatchFeature
													UpperCAmelCase_      :   Tuple      =	BatchFeature({'''input_values''': features} )

													# make sure list is in array format
													UpperCAmelCase_      :   int      =	padded_inputs.get('''input_values''' )
													if isinstance(input_values[0]							,  __magic_name__ ):
																			UpperCAmelCase_      :   Any      =	[np.asarray(__magic_name__							,  dtype=np.floataa ) for feature in input_values]

													# normalization
													if self.do_normalize:
																			UpperCAmelCase_      :   Tuple      =	[self.normalize(__magic_name__ ) for feature in input_values]

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

													return padded_inputs












'''simple docstring'''







import argparse
import json
import re
import string
import sys
from collections import Counter





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Optional[Any] )			->						str:

						def remove_articles(SCREAMING_SNAKE_CASE__   :      Union[str, Any] ):
												return re.sub(R'''\b(a|an|the)\b''',      ''' ''',      SCREAMING_SNAKE_CASE__ )

						def white_space_fix(SCREAMING_SNAKE_CASE__   :      Dict ):
												return " ".join(text.split() )

						def remove_punc(SCREAMING_SNAKE_CASE__   :      Union[str, Any] ):
												UpperCAmelCase_      :   int      =	set(string.punctuation )
												return "".join(ch for ch in text if ch not in exclude )

						def lower(SCREAMING_SNAKE_CASE__   :      str ):
												return text.lower()

						return white_space_fix(remove_articles(remove_punc(lower(SCREAMING_SNAKE_CASE__ ) ) ) )





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Union[str, Any],      SCREAMING_SNAKE_CASE__   :      Union[str, Any] )			->						Tuple:
						UpperCAmelCase_      :   List[Any]      =	normalize_answer(SCREAMING_SNAKE_CASE__ ).split()
						UpperCAmelCase_      :   Optional[Any]      =	normalize_answer(SCREAMING_SNAKE_CASE__ ).split()
						UpperCAmelCase_      :   Any      =	Counter(SCREAMING_SNAKE_CASE__ ) & Counter(SCREAMING_SNAKE_CASE__ )
						UpperCAmelCase_      :   Tuple      =	sum(common.values() )
						if num_same == 0:
												return 0
						UpperCAmelCase_      :   Union[str, Any]      =	1.0 * num_same / len(SCREAMING_SNAKE_CASE__ )
						UpperCAmelCase_      :   Dict      =	1.0 * num_same / len(SCREAMING_SNAKE_CASE__ )
						UpperCAmelCase_      :   str      =	(2 * precision * recall) / (precision + recall)
						return fa





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      int,      SCREAMING_SNAKE_CASE__   :      int )			->						Any:
						return normalize_answer(SCREAMING_SNAKE_CASE__ ) == normalize_answer(SCREAMING_SNAKE_CASE__ )





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Union[str, Any],      SCREAMING_SNAKE_CASE__   :      Any,      SCREAMING_SNAKE_CASE__   :      Dict )			->						Optional[int]:
						UpperCAmelCase_      :   Dict      =	[]
						for ground_truth in ground_truths:
												UpperCAmelCase_      :   str      =	metric_fn(SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__ )
												scores_for_ground_truths.append(SCREAMING_SNAKE_CASE__ )
						return max(SCREAMING_SNAKE_CASE__ )





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      List[str],      SCREAMING_SNAKE_CASE__   :      Optional[Any] )			->						Any:
						UpperCAmelCase_      :   str      =	0
						for article in dataset:
												for paragraph in article["paragraphs"]:
																		for qa in paragraph["qas"]:
																								total += 1
																								if qa["id"] not in predictions:
																														UpperCAmelCase_      :   Union[str, Any]      =	'''Unanswered question ''' + qa['''id'''] + ''' will receive score 0.'''
																														print(SCREAMING_SNAKE_CASE__,      file=sys.stderr )
																														continue
																								UpperCAmelCase_      :   Optional[Any]      =	[x['''text'''] for x in qa['''answers''']]
																								UpperCAmelCase_      :   int      =	predictions[qa['''id''']]
																								exact_match += metric_max_over_ground_truths(SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__ )
																								fa += metric_max_over_ground_truths(SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__ )

						UpperCAmelCase_      :   Any      =	1_00.0 * exact_match / total
						UpperCAmelCase_      :   Optional[int]      =	1_00.0 * fa / total

						return {"exact_match": exact_match, "f1": fa}


if __name__ == "__main__":
					snake_case_  :							Any            =	"1.1"
					snake_case_  :							Any            =	argparse.ArgumentParser(description="Evaluation for SQuAD " + expected_version)
					parser.add_argument("dataset_file", help="Dataset file")
					parser.add_argument("prediction_file", help="Prediction File")
					snake_case_  :							int            =	parser.parse_args()
					with open(args.dataset_file) as dataset_file:
										snake_case_  :							List[Any]            =	json.load(dataset_file)
										if dataset_json["version"] != expected_version:
															print(
															    "Evaluation expects v-" + expected_version + ", but got dataset with v-" + dataset_json["version"],
															    file=sys.stderr,
															)
										snake_case_  :							Tuple            =	dataset_json["data"]
					with open(args.prediction_file) as prediction_file:
										snake_case_  :							Tuple            =	json.load(prediction_file)
					print(json.dumps(evaluate(dataset, predictions)))





