"""simple docstring"""






import functools
from contextlib import contextmanager

import bert_score
from packaging import version

import datasets



@contextmanager
def SCREAMING_SNAKE_CASE__    (     )						->						Optional[int]:
   def filter_log(__UpperCAmelCase				):
      return False if "This IS expected if you are initializing" in record.msg else True

   lowercase__:     Optional[Any]      =   datasets.utils.logging.get_logger('''transformers.modeling_utils'''				)
   logger.addFilter(__UpperCAmelCase				)
   try:
      yield
   finally:
      logger.removeFilter(__UpperCAmelCase				)


__A         =				"\\n@inproceedings{bert-score,\n  title={BERTScore: Evaluating Text Generation with BERT},\n  author={Tianyi Zhang* and Varsha Kishore* and Felix Wu* and Kilian Q. Weinberger and Yoav Artzi},\n  booktitle={International Conference on Learning Representations},\n  year={2020},\n  url={https://openreview.net/forum?id=SkeHuCVFDr}\n}\n"

__A         =				"\\nBERTScore leverages the pre-trained contextual embeddings from BERT and matches words in candidate and reference\nsentences by cosine similarity.\nIt has been shown to correlate with human judgment on sentence-level and system-level evaluation.\nMoreover, BERTScore computes precision, recall, and F1 measure, which can be useful for evaluating different language\ngeneration tasks.\n\nSee the project's README at https://github.com/Tiiiger/bert_score#readme for more information.\n"

__A         =				"\nBERTScore Metrics with the hashcode from a source against one or more references.\n\nArgs:\n    predictions (list of str): Prediction/candidate sentences.\n    references (list of str or list of list of str): Reference sentences.\n    lang (str): Language of the sentences; required (e.g. 'en').\n    model_type (str): Bert specification, default using the suggested\n        model for the target language; has to specify at least one of\n        `model_type` or `lang`.\n    num_layers (int): The layer of representation to use,\n        default using the number of layers tuned on WMT16 correlation data.\n    verbose (bool): Turn on intermediate status update.\n    idf (bool or dict): Use idf weighting; can also be a precomputed idf_dict.\n    device (str): On which the contextual embedding model will be allocated on.\n        If this argument is None, the model lives on cuda:0 if cuda is available.\n    nthreads (int): Number of threads.\n    batch_size (int): Bert score processing batch size,\n        at least one of `model_type` or `lang`. `lang` needs to be\n        specified when `rescale_with_baseline` is True.\n    rescale_with_baseline (bool): Rescale bertscore with pre-computed baseline.\n    baseline_path (str): Customized baseline file.\n    use_fast_tokenizer (bool): `use_fast` parameter passed to HF tokenizer. New in version 0.3.10.\n\nReturns:\n    precision: Precision.\n    recall: Recall.\n    f1: F1 score.\n    hashcode: Hashcode of the library.\n\nExamples:\n\n    >>> predictions = [\"hello there\", \"general kenobi\"]\n    >>> references = [\"hello there\", \"general kenobi\"]\n    >>> bertscore = datasets.load_metric(\"bertscore\")\n    >>> results = bertscore.compute(predictions=predictions, references=references, lang=\"en\")\n    >>> print([round(v, 2) for v in results[\"f1\"]])\n    [1.0, 1.0]\n"




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



    """simple docstring"""

    def _snake_case    (     self							):
       return datasets.MetricInfo(
           description=_DESCRIPTION					,    citation=_CITATION					,    homepage='''https://github.com/Tiiiger/bert_score'''					,    inputs_description=_KWARGS_DESCRIPTION					,    features=datasets.Features(
               {
                   '''predictions''': datasets.Value('''string'''					,    id='''sequence'''							),
                   '''references''': datasets.Sequence(datasets.Value('''string'''					,    id='''sequence'''							)					,    id='''references'''							),
               }							)					,    codebase_urls=['''https://github.com/Tiiiger/bert_score''']					,    reference_urls=[
               '''https://github.com/Tiiiger/bert_score''',
               '''https://arxiv.org/abs/1904.09675''',
           ]					,    )






    def _snake_case    (     self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase=None					,    _UpperCAmelCase=None					,    _UpperCAmelCase=None					,    _UpperCAmelCase=False					,    _UpperCAmelCase=False					,    _UpperCAmelCase=None					,    _UpperCAmelCase=64					,    _UpperCAmelCase=4					,    _UpperCAmelCase=False					,    _UpperCAmelCase=False					,    _UpperCAmelCase=None					,    _UpperCAmelCase=False					,    ):
       lowercase__:     Optional[Any]      =   bert_score.utils.get_hash
       lowercase__:     Any      =   bert_score.BERTScorer

       if version.parse(bert_score.__version__							) >= version.parse('''0.3.10'''							):
          lowercase__:     Union[str, Any]      =   functools.partial(_UpperCAmelCase					,    use_fast_tokenizer=_UpperCAmelCase							)
          lowercase__:     int      =   functools.partial(_UpperCAmelCase					,    use_fast_tokenizer=_UpperCAmelCase							)
       elif use_fast_tokenizer:
          raise ImportWarning(
              '''To use a fast tokenizer, the module `bert-score>=0.3.10` is required, and the current version of `bert-score` doesn\'t match this condition.\n'''
              '''You can install it with `pip install "bert-score>=0.3.10"`.'''							)

       if model_type is None:
          assert lang is not None, "either lang or model_type should be specified"
          lowercase__:     Any      =   bert_score.utils.langamodel[lang.lower()]

       if num_layers is None:
          lowercase__:     Dict      =   bert_score.utils.modelalayers[model_type]

       lowercase__:     Any      =   get_hash(
           model=_UpperCAmelCase					,    num_layers=_UpperCAmelCase					,    idf=_UpperCAmelCase					,    rescale_with_baseline=_UpperCAmelCase					,    use_custom_baseline=baseline_path is not None					,    )

       with filter_logging_context():
          if not hasattr(self					,    '''cached_bertscorer'''							) or self.cached_bertscorer.hash != hashcode:
             lowercase__:     int      =   scorer(
                 model_type=_UpperCAmelCase					,    num_layers=_UpperCAmelCase					,    batch_size=_UpperCAmelCase					,    nthreads=_UpperCAmelCase					,    all_layers=_UpperCAmelCase					,    idf=_UpperCAmelCase					,    device=_UpperCAmelCase					,    lang=_UpperCAmelCase					,    rescale_with_baseline=_UpperCAmelCase					,    baseline_path=_UpperCAmelCase					,    )

       ((lowercase__),				(lowercase__),				(lowercase__)):     int      =   self.cached_bertscorer.score(
           cands=_UpperCAmelCase					,    refs=_UpperCAmelCase					,    verbose=_UpperCAmelCase					,    batch_size=_UpperCAmelCase					,    )
       lowercase__:     List[Any]      =   {
           '''precision''': P.tolist(),
           '''recall''': R.tolist(),
           '''f1''': F.tolist(),
           '''hashcode''': hashcode,
       }
       return output_dict






    def _snake_case    (     self					,    _UpperCAmelCase=None					,    _UpperCAmelCase=None					,    **_UpperCAmelCase							):
       # References can be strings or lists of strings
       # Let's change strings to lists of strings with one element
       if references is not None:
          lowercase__:     Tuple      =   [[ref] if isinstance(_UpperCAmelCase					,    _UpperCAmelCase							) else ref for ref in references]
       super().add_batch(predictions=_UpperCAmelCase					,    references=_UpperCAmelCase					,    **_UpperCAmelCase							)






    def _snake_case    (     self					,    _UpperCAmelCase=None					,    _UpperCAmelCase=None					,    **_UpperCAmelCase							):
       # References can be strings or lists of strings
       # Let's change strings to lists of strings with one element
       if isinstance(_UpperCAmelCase					,    _UpperCAmelCase							):
          lowercase__:     Dict      =   [reference]
       super().add(prediction=_UpperCAmelCase					,    reference=_UpperCAmelCase					,    **_UpperCAmelCase							)

"""simple docstring"""






import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
       from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


__A         =				get_tests_dir("fixtures/test_sentencepiece.model")


@require_torch
@require_torchaudio
@require_sentencepiece
class 					UpperCAmelCase		(unittest.TestCase						):



    """simple docstring"""

    def _snake_case    (     self							):
       lowercase__:     Dict      =   tempfile.mkdtemp()

       lowercase__:     str      =   ['''<s>''', '''<pad>''', '''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
       lowercase__:     Optional[Any]      =   dict(zip(_UpperCAmelCase					,    range(len(_UpperCAmelCase							)							)							)							)
       lowercase__:     List[Any]      =   Path(self.tmpdirname							)
       save_json(_UpperCAmelCase					,    save_dir / VOCAB_FILES_NAMES['''vocab_file''']							)
       if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
          copyfile(_UpperCAmelCase					,    save_dir / VOCAB_FILES_NAMES['''spm_file''']							)

       lowercase__:     Tuple      =   SpeechaTextTokenizer.from_pretrained(self.tmpdirname							)
       tokenizer.save_pretrained(self.tmpdirname							)

       lowercase__:     Optional[int]      =   {
           '''feature_size''': 24,
           '''num_mel_bins''': 24,
           '''padding_value''': 0.0,
           '''sampling_rate''': 16000,
           '''return_attention_mask''': False,
           '''do_normalize''': True,
       }
       save_json(_UpperCAmelCase					,    save_dir / FEATURE_EXTRACTOR_NAME							)






    def _snake_case    (     self					,    **_UpperCAmelCase							):
       return SpeechaTextTokenizer.from_pretrained(self.tmpdirname					,    **_UpperCAmelCase							)






    def _snake_case    (     self					,    **_UpperCAmelCase							):
       return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname					,    **_UpperCAmelCase							)






    def _snake_case    (     self							):
       shutil.rmtree(self.tmpdirname							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   self.get_tokenizer()
       lowercase__:     Optional[int]      =   self.get_feature_extractor()

       lowercase__:     int      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       processor.save_pretrained(self.tmpdirname							)
       lowercase__:     Union[str, Any]      =   SpeechaTextProcessor.from_pretrained(self.tmpdirname							)

       self.assertEqual(processor.tokenizer.get_vocab()					,    tokenizer.get_vocab()							)
       self.assertIsInstance(processor.tokenizer					,    _UpperCAmelCase							)

       self.assertEqual(processor.feature_extractor.to_json_string()					,    feature_extractor.to_json_string()							)
       self.assertIsInstance(processor.feature_extractor					,    _UpperCAmelCase							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   SpeechaTextProcessor(
           tokenizer=self.get_tokenizer()					,    feature_extractor=self.get_feature_extractor()							)
       processor.save_pretrained(self.tmpdirname							)

       lowercase__:     Any      =   self.get_tokenizer(bos_token='''(BOS)'''					,    eos_token='''(EOS)'''							)
       lowercase__:     Tuple      =   self.get_feature_extractor(do_normalize=_UpperCAmelCase					,    padding_value=1.0							)

       lowercase__:     List[str]      =   SpeechaTextProcessor.from_pretrained(
           self.tmpdirname					,    bos_token='''(BOS)'''					,    eos_token='''(EOS)'''					,    do_normalize=_UpperCAmelCase					,    padding_value=1.0							)

       self.assertEqual(processor.tokenizer.get_vocab()					,    tokenizer_add_kwargs.get_vocab()							)
       self.assertIsInstance(processor.tokenizer					,    _UpperCAmelCase							)

       self.assertEqual(processor.feature_extractor.to_json_string()					,    feature_extractor_add_kwargs.to_json_string()							)
       self.assertIsInstance(processor.feature_extractor					,    _UpperCAmelCase							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   self.get_feature_extractor()
       lowercase__:     List[Any]      =   self.get_tokenizer()

       lowercase__:     Any      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       lowercase__:     int      =   floats_list((3, 1000)							)

       lowercase__:     Optional[Any]      =   feature_extractor(_UpperCAmelCase					,    return_tensors='''np'''							)
       lowercase__:     Dict      =   processor(_UpperCAmelCase					,    return_tensors='''np'''							)

       for key in input_feat_extract.keys():
          self.assertAlmostEqual(input_feat_extract[key].sum()					,    input_processor[key].sum()					,    delta=1e-2							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   self.get_feature_extractor()
       lowercase__:     Dict      =   self.get_tokenizer()

       lowercase__:     Union[str, Any]      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       lowercase__:     Dict      =   '''This is a test string'''

       lowercase__:     int      =   processor(text=_UpperCAmelCase							)

       lowercase__:     Union[str, Any]      =   tokenizer(_UpperCAmelCase							)

       for key in encoded_tok.keys():
          self.assertListEqual(encoded_tok[key]					,    encoded_processor[key]							)






    def _snake_case    (     self							):
       lowercase__:     List[Any]      =   self.get_feature_extractor()
       lowercase__:     Union[str, Any]      =   self.get_tokenizer()

       lowercase__:     Tuple      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       lowercase__:     List[str]      =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       lowercase__:     List[Any]      =   processor.batch_decode(_UpperCAmelCase							)
       lowercase__:     Dict      =   tokenizer.batch_decode(_UpperCAmelCase							)

       self.assertListEqual(_UpperCAmelCase					,    _UpperCAmelCase							)






    def _snake_case    (     self							):
       lowercase__:     Optional[Any]      =   self.get_feature_extractor()
       lowercase__:     List[str]      =   self.get_tokenizer()

       lowercase__:     Union[str, Any]      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       self.assertListEqual(
           processor.model_input_names					,    feature_extractor.model_input_names					,    msg='''`processor` and `feature_extractor` model input names do not match'''					,    )
