





import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


_a							= version.parse(importlib_metadata.version("nltk"))
if NLTK_VERSION >= version.Version("3.6.4"):
			from nltk import word_tokenize


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

_a							= "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n"

_a							= "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n    predictions: list of predictions to score. Each prediction\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\n    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n    gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n    'meteor': meteor score.\nExamples:\n\n    >>> meteor = datasets.load_metric('meteor')\n    >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n    >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n    >>> results = meteor.compute(predictions=predictions, references=references)\n    >>> print(round(results[\"meteor\"], 4))\n    0.6944\n"






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

			'''simple docstring'''







			def 						__lowerCamelCase							(       self				):



				'''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/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 						__lowerCamelCase							(       self			,    __lowerCAmelCase				):



				'''simple docstring'''







				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 						__lowerCamelCase							(       self			,    __lowerCAmelCase			,    __lowerCAmelCase			,    __lowerCAmelCase=0.9			,    __lowerCAmelCase=3			,    __lowerCAmelCase=0.5				):



				'''simple docstring'''







				if NLTK_VERSION >= version.Version('''3.6.5'''				):
					lowerCamelCase__														=						[
					    meteor_score.single_meteor_score(
					        word_tokenize(__lowerCAmelCase				)			,    word_tokenize(__lowerCAmelCase				)			,    alpha=__lowerCAmelCase			,    beta=__lowerCAmelCase			,    gamma=__lowerCAmelCase				)
					    for ref, pred in zip(__lowerCAmelCase			,    __lowerCAmelCase				)
					]
				else:
					lowerCamelCase__														=						[
					    meteor_score.single_meteor_score(__lowerCAmelCase			,    __lowerCAmelCase			,    alpha=__lowerCAmelCase			,    beta=__lowerCAmelCase			,    gamma=__lowerCAmelCase				)
					    for ref, pred in zip(__lowerCAmelCase			,    __lowerCAmelCase				)
					]

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











import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


lowercase    				=   random.Random()

if is_torch_available():
	import torch





def 		__UpperCAmelCase     (	a_				,	a_=1.0				,	a_=None				,	a_=None):
							if rng is None:
														snake_case_           =				global_rng

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

							return values




class 					UpperCamelCase_ (     unittest.TestCase							):



	'''simple docstring'''




	def __init__(			self			,     a			,     a=7			,     a=4_00			,     a=20_00			,     a=1			,     a=0.0			,     a=1_60_00			,     a=True			,     a=True			,     )			->       Tuple:
								snake_case_           =				parent
								snake_case_           =				batch_size
								snake_case_           =				min_seq_length
								snake_case_           =				max_seq_length
								snake_case_           =				(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								snake_case_           =				feature_size
								snake_case_           =				padding_value
								snake_case_           =				sampling_rate
								snake_case_           =				return_attention_mask
								snake_case_           =				do_normalize




	def 				_UpperCamelCase     (			self   )			->       Union[str, Any]:
								return {
								    "feature_size": self.feature_size,
								    "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			,     a=False			,     a=False   )			->       Optional[int]:
								def _flatten(a   ):
															return list(itertools.chain(*a   )   )

								if equal_length:
															snake_case_           =				floats_list((self.batch_size, self.max_seq_length)   )
								else:
															# make sure that inputs increase in size
															snake_case_           =				[
															    _flatten(floats_list((x, self.feature_size)   )   )
															    for x in range(self.min_seq_length			,     self.max_seq_length			,     self.seq_length_diff   )
															]

								if numpify:
															snake_case_           =				[np.asarray(a   ) for x in speech_inputs]

								return speech_inputs






@require_torch
@require_torchaudio
class 					UpperCamelCase_ (     snake_case_			,   unittest.TestCase							):



	'''simple docstring'''




	lowerCAmelCase						=					ASTFeatureExtractor




	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								snake_case_           =				ASTFeatureExtractionTester(self   )




	def 				_UpperCamelCase     (			self   )			->       Union[str, Any]:
								# Tests that all call wrap to encode_plus and batch_encode_plus
								snake_case_           =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								# create three inputs of length 800, 1000, and 1200
								snake_case_           =				[floats_list((1, x)   )[0] for x in range(8_00			,     14_00			,     2_00   )]
								snake_case_           =				[np.asarray(a   ) for speech_input in speech_inputs]

								# Test not batched input
								snake_case_           =				feat_extract(speech_inputs[0]			,     return_tensors='np'   ).input_values
								snake_case_           =				feat_extract(np_speech_inputs[0]			,     return_tensors='np'   ).input_values
								self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )

								# Test batched
								snake_case_           =				feat_extract(a			,     padding=a			,     return_tensors='np'   ).input_values
								snake_case_           =				feat_extract(a			,     padding=a			,     return_tensors='np'   ).input_values
								for enc_seq_a, enc_seq_a in zip(a			,     a   ):
															self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )

								# Test 2-D numpy arrays are batched.
								snake_case_           =				[floats_list((1, x)   )[0] for x in (8_00, 8_00, 8_00)]
								snake_case_           =				np.asarray(a   )
								snake_case_           =				feat_extract(a			,     return_tensors='np'   ).input_values
								snake_case_           =				feat_extract(a			,     return_tensors='np'   ).input_values
								for enc_seq_a, enc_seq_a in zip(a			,     a   ):
															self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )




	@require_torch
	def 				_UpperCamelCase     (			self   )			->       List[str]:
								import torch

								snake_case_           =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								snake_case_           =				np.random.rand(1_00   ).astype(np.floataa   )
								snake_case_           =				np_speech_inputs.tolist()

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




	def 				_UpperCamelCase     (			self			,     a   )			->       Tuple:
								from datasets import load_dataset

								snake_case_           =				load_dataset('hf-internal-testing/librispeech_asr_dummy'			,     'clean'			,     split='validation'   )
								# automatic decoding with librispeech
								snake_case_           =				ds.sort('id'   ).select(range(a   )   )[:num_samples]['audio']

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






	@require_torch
	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								# fmt: off
								snake_case_           =				torch.tensor(
								    [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
								     -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
								     -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
								     -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]   )
								# fmt: on

								snake_case_           =				self._load_datasamples(1   )
								snake_case_           =				ASTFeatureExtractor()
								snake_case_           =				feature_extractor(a			,     return_tensors='pt'   ).input_values
								self.assertEquals(input_values.shape			,     (1, 10_24, 1_28)   )
								self.assertTrue(torch.allclose(input_values[0, 0, :30]			,     a			,     atol=1E-4   )   )





