






"""simple docstring"""





import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
  from transformers import WhisperFeatureExtractor

if is_torch_available():
  import torch

_SCREAMING_SNAKE_CASE					:				Tuple					    =						random.Random()







def      lowerCamelCase__       (							_lowerCamelCase       :		Tuple		,				_lowerCamelCase       :		Union[str, Any]=1.0		,				_lowerCamelCase       :		Union[str, Any]=None		,				_lowerCamelCase       :		Tuple=None )      ->				Tuple:
  if rng is None:
    lowerCamelCase_         =						global_rng

  lowerCamelCase_         =						[]
  for batch_idx in range(shape[0] ):
    values.append([] )
    for _ in range(shape[1] ):
      values[-1].append(rng.random() * scale )

  return values






@require_torch
@require_torchaudio
class      a						(					unittest.TestCase		):






    def __init__(       self     :							Tuple       ,						__SCREAMING_SNAKE_CASE     :							int       ,						__SCREAMING_SNAKE_CASE     :							Tuple=7       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]=400       ,						__SCREAMING_SNAKE_CASE     :							List[str]=2000       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]=10       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]=160       ,						__SCREAMING_SNAKE_CASE     :							str=8       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]=0.0       ,						__SCREAMING_SNAKE_CASE     :							int=4000       ,						__SCREAMING_SNAKE_CASE     :							List[Any]=False       ,						__SCREAMING_SNAKE_CASE     :							List[Any]=True       ,						)     ->		Union[str, Any]:
      lowerCamelCase_         =						parent
      lowerCamelCase_         =						batch_size
      lowerCamelCase_         =						min_seq_length
      lowerCamelCase_         =						max_seq_length
      lowerCamelCase_         =						(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
      lowerCamelCase_         =						padding_value
      lowerCamelCase_         =						sampling_rate
      lowerCamelCase_         =						return_attention_mask
      lowerCamelCase_         =						do_normalize
      lowerCamelCase_         =						feature_size
      lowerCamelCase_         =						chunk_length
      lowerCamelCase_         =						hop_length






    def    UpperCamelCase       (       self     :							Optional[Any]					)     ->		Optional[int]:
      return {
          "feature_size": self.feature_size,
          "hop_length": self.hop_length,
          "chunk_length": self.chunk_length,
          "padding_value": self.padding_value,
          "sampling_rate": self.sampling_rate,
          "return_attention_mask": self.return_attention_mask,
          "do_normalize": self.do_normalize,
      }






    def    UpperCamelCase       (       self     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Dict=False       ,						__SCREAMING_SNAKE_CASE     :							List[str]=False					)     ->		str:
      def _flatten(__SCREAMING_SNAKE_CASE     :							List[Any]					):
        return list(itertools.chain(*__SCREAMING_SNAKE_CASE					)					)

      if equal_length:
        lowerCamelCase_         =						[floats_list((self.max_seq_length, self.feature_size)					) for _ in range(self.batch_size					)]
      else:
        # make sure that inputs increase in size
        lowerCamelCase_         =						[
            floats_list((x, self.feature_size)					)
            for x in range(self.min_seq_length       ,						self.max_seq_length       ,						self.seq_length_diff					)
        ]
      if numpify:
        lowerCamelCase_         =						[np.asarray(__SCREAMING_SNAKE_CASE					) for x in speech_inputs]
      return speech_inputs






@require_torch
@require_torchaudio
class      a						(					__snake_case  ,      unittest.TestCase		):
    SCREAMING_SNAKE_CASE						:     Dict						= WhisperFeatureExtractor if is_speech_available() else None






    def    UpperCamelCase       (       self     :							Dict					)     ->		List[str]:
      lowerCamelCase_         =						WhisperFeatureExtractionTester(self					)






    def    UpperCamelCase       (       self     :							Tuple					)     ->		Optional[int]:
      lowerCamelCase_         =						self.feature_extraction_class(**self.feat_extract_dict					)

      with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCamelCase_         =						feat_extract_first.save_pretrained(__SCREAMING_SNAKE_CASE					)[0]
        check_json_file_has_correct_format(__SCREAMING_SNAKE_CASE					)
        lowerCamelCase_         =						self.feature_extraction_class.from_pretrained(__SCREAMING_SNAKE_CASE					)

      lowerCamelCase_         =						feat_extract_first.to_dict()
      lowerCamelCase_         =						feat_extract_second.to_dict()
      lowerCamelCase_         =						feat_extract_first.mel_filters
      lowerCamelCase_         =						feat_extract_second.mel_filters
      self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)					)
      self.assertEqual(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							List[Any]					)     ->		Optional[int]:
      lowerCamelCase_         =						self.feature_extraction_class(**self.feat_extract_dict					)

      with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCamelCase_         =						os.path.join(__SCREAMING_SNAKE_CASE       ,						'feat_extract.json'					)
        feat_extract_first.to_json_file(__SCREAMING_SNAKE_CASE					)
        lowerCamelCase_         =						self.feature_extraction_class.from_json_file(__SCREAMING_SNAKE_CASE					)

      lowerCamelCase_         =						feat_extract_first.to_dict()
      lowerCamelCase_         =						feat_extract_second.to_dict()
      lowerCamelCase_         =						feat_extract_first.mel_filters
      lowerCamelCase_         =						feat_extract_second.mel_filters
      self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)					)
      self.assertEqual(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Optional[int]					)     ->		int:
      # Tests that all call wrap to encode_plus and batch_encode_plus
      lowerCamelCase_         =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      # create three inputs of length 800, 1000, and 1200
      lowerCamelCase_         =						[floats_list((1, x)					)[0] for x in range(800       ,						1400       ,						200					)]
      lowerCamelCase_         =						[np.asarray(__SCREAMING_SNAKE_CASE					) for speech_input in speech_inputs]

      # Test feature size
      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						padding='max_length'       ,						return_tensors='np'					).input_features
      self.assertTrue(input_features.ndim == 3					)
      self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames					)
      self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size					)

      # Test not batched input
      lowerCamelCase_         =						feature_extractor(speech_inputs[0]       ,						return_tensors='np'					).input_features
      lowerCamelCase_         =						feature_extractor(np_speech_inputs[0]       ,						return_tensors='np'					).input_features
      self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						atol=1e-3					)					)

      # Test batched
      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						return_tensors='np'					).input_features
      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						return_tensors='np'					).input_features
      for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					):
        self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						atol=1e-3					)					)

      # Test 2-D numpy arrays are batched.
      lowerCamelCase_         =						[floats_list((1, x)					)[0] for x in (800, 800, 800)]
      lowerCamelCase_         =						np.asarray(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						return_tensors='np'					).input_features
      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						return_tensors='np'					).input_features
      for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					):
        self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						atol=1e-3					)					)

      # Test truncation required
      lowerCamelCase_         =						[floats_list((1, x)					)[0] for x in range(200       ,						(feature_extractor.n_samples + 500)       ,						200					)]
      lowerCamelCase_         =						[np.asarray(__SCREAMING_SNAKE_CASE					) for speech_input in speech_inputs]

      lowerCamelCase_         =						[x[: feature_extractor.n_samples] for x in speech_inputs]
      lowerCamelCase_         =						[np.asarray(__SCREAMING_SNAKE_CASE					) for speech_input in speech_inputs_truncated]

      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						return_tensors='np'					).input_features
      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						return_tensors='np'					).input_features
      for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					):
        self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						atol=1e-3					)					)






    def    UpperCamelCase       (       self     :							str					)     ->		List[str]:
      import torch

      lowerCamelCase_         =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      lowerCamelCase_         =						np.random.rand(100       ,						32					).astype(np.floataa					)
      lowerCamelCase_         =						np_speech_inputs.tolist()

      for inputs in [py_speech_inputs, np_speech_inputs]:
        lowerCamelCase_         =						feature_extractor.pad([{'input_features': inputs}]       ,						return_tensors='np'					)
        self.assertTrue(np_processed.input_features.dtype == np.floataa					)
        lowerCamelCase_         =						feature_extractor.pad([{'input_features': inputs}]       ,						return_tensors='pt'					)
        self.assertTrue(pt_processed.input_features.dtype == torch.floataa					)






    def    UpperCamelCase       (       self     :							Any       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]					)     ->		Union[str, Any]:
      lowerCamelCase_         =						load_dataset('hf-internal-testing/librispeech_asr_dummy'       ,						'clean'       ,						split='validation'					)
      # automatic decoding with librispeech
      lowerCamelCase_         =						ds.sort('id'					).select(range(__SCREAMING_SNAKE_CASE					)					)[:num_samples]['audio']

      return [x["array"] for x in speech_samples]






    def    UpperCamelCase       (       self     :							List[Any]					)     ->		List[Any]:
      # fmt: off
      lowerCamelCase_         =						torch.tensor(
          [
              0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
              0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
              0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
              -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
          ]					)
      # fmt: on

      lowerCamelCase_         =						self._load_datasamples(1					)
      lowerCamelCase_         =						WhisperFeatureExtractor()
      lowerCamelCase_         =						feature_extractor(__SCREAMING_SNAKE_CASE       ,						return_tensors='pt'					).input_features
      self.assertEqual(input_features.shape       ,						(1, 80, 3000)					)
      self.assertTrue(torch.allclose(input_features[0, 0, :30]       ,						__SCREAMING_SNAKE_CASE       ,						atol=1e-4					)					)






    def    UpperCamelCase       (       self     :							Dict					)     ->		int:
      lowerCamelCase_         =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      lowerCamelCase_         =						self._load_datasamples(1					)[0]
      lowerCamelCase_         =						((audio - audio.min()) / (audio.max() - audio.min())) * 65535  # Rescale to [0, 65535] to show issue
      lowerCamelCase_         =						feat_extract.zero_mean_unit_var_norm([audio]       ,						attention_mask=__SCREAMING_SNAKE_CASE					)[0]

      self.assertTrue(np.all(np.mean(__SCREAMING_SNAKE_CASE					) < 1e-3					)					)
      self.assertTrue(np.all(np.abs(np.var(__SCREAMING_SNAKE_CASE					) - 1					) < 1e-3					)					)












"""simple docstring"""





import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


_SCREAMING_SNAKE_CASE					:				Tuple					    =						version.parse(importlib_metadata.version('''nltk'''))
if NLTK_VERSION >= version.Version('''3.6.4'''):
  from nltk import word_tokenize


_SCREAMING_SNAKE_CASE					:				Tuple					    =						'''\
@inproceedings{banarjee2005,
  title     = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},
  author    = {Banerjee, Satanjeev  and Lavie, Alon},
  booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},
  month     = jun,
  year      = {2005},
  address   = {Ann Arbor, Michigan},
  publisher = {Association for Computational Linguistics},
  url       = {https://www.aclweb.org/anthology/W05-0909},
  pages     = {65--72},
}
'''

_SCREAMING_SNAKE_CASE					:				int					    =						'''\
METEOR, an automatic metric for machine translation evaluation
that is based on a generalized concept of unigram matching between the
machine-produced translation and human-produced reference translations.
Unigrams can be matched based on their surface forms, stemmed forms,
and meanings; furthermore, METEOR can be easily extended to include more
advanced matching strategies. Once all generalized unigram matches
between the two strings have been found, METEOR computes a score for
this matching using a combination of unigram-precision, unigram-recall, and
a measure of fragmentation that is designed to directly capture how
well-ordered the matched words in the machine translation are in relation
to the reference.

METEOR gets an R correlation value of 0.347 with human evaluation on the Arabic
data and 0.331 on the Chinese data. This is shown to be an improvement on
using simply unigram-precision, unigram-recall and their harmonic F1
combination.
'''

_SCREAMING_SNAKE_CASE					:				List[Any]					    =						'''
Computes METEOR score of translated segments against one or more references.
Args:
    predictions: list of predictions to score. Each prediction
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.
    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9
    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3
    gamma: Relative weight assigned to fragmentation penalty. default: 0.5
Returns:
    \'meteor\': meteor score.
Examples:

    >>> meteor = datasets.load_metric(\'meteor\')
    >>> predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party"]
    >>> references = ["It is a guide to action that ensures that the military will forever heed Party commands"]
    >>> results = meteor.compute(predictions=predictions, references=references)
    >>> print(round(results["meteor"], 4))
    0.6944
'''






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






    def    UpperCamelCase       (       self     :							List[str]					)     ->		List[str]:
      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/nltk/nltk/blob/develop/nltk/translate/meteor_score.py']       ,						reference_urls=[
              'https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score',
              'https://en.wikipedia.org/wiki/METEOR',
          ]       ,						)






    def    UpperCamelCase       (       self     :							Optional[int]       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]					)     ->		Dict:
      import nltk

      nltk.download('wordnet'					)
      if NLTK_VERSION >= version.Version('3.6.5'					):
        nltk.download('punkt'					)
      if NLTK_VERSION >= version.Version('3.6.6'					):
        nltk.download('omw-1.4'					)






    def    UpperCamelCase       (       self     :							Optional[int]       ,						__SCREAMING_SNAKE_CASE     :							Any       ,						__SCREAMING_SNAKE_CASE     :							Any       ,						__SCREAMING_SNAKE_CASE     :							List[Any]=0.9       ,						__SCREAMING_SNAKE_CASE     :							Dict=3       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]=0.5					)     ->		int:
      if NLTK_VERSION >= version.Version('3.6.5'					):
        lowerCamelCase_         =						[
            meteor_score.single_meteor_score(
                word_tokenize(__SCREAMING_SNAKE_CASE					)       ,						word_tokenize(__SCREAMING_SNAKE_CASE					)       ,						alpha=__SCREAMING_SNAKE_CASE       ,						beta=__SCREAMING_SNAKE_CASE       ,						gamma=__SCREAMING_SNAKE_CASE					)
            for ref, pred in zip(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)
        ]
      else:
        lowerCamelCase_         =						[
            meteor_score.single_meteor_score(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						alpha=__SCREAMING_SNAKE_CASE       ,						beta=__SCREAMING_SNAKE_CASE       ,						gamma=__SCREAMING_SNAKE_CASE					)
            for ref, pred in zip(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)
        ]

      return {"meteor": np.mean(__SCREAMING_SNAKE_CASE					)}




