

import functools
from contextlib import contextmanager

import bert_score
from packaging import version

import datasets






@contextmanager
def  UpperCamelCase_(							):




			"""simple docstring"""






			def filter_log(_snake_case     :						List[str]			):
						return False if "This IS expected if you are initializing" in record.msg else True

			__a						   =datasets.utils.logging.get_logger('transformers.modeling_utils'			)
			logger.addFilter(_snake_case			)
			try:
						yield
			finally:
						logger.removeFilter(_snake_case			)


_lowerCAmelCase      :    Union[str, Any]						       =    "\\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"

_lowerCAmelCase      :    Tuple						       =    "\\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"

_lowerCAmelCase      :    Tuple						       =    "\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 			__magic_name__					(    datasets.Metric   ):





			def 	__magic_name__					(	self      )     ->				Optional[int]:




						'''simple docstring'''
						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 	__magic_name__					(	self      ,						__snake_case      ,						__snake_case      ,						__snake_case=None      ,						__snake_case=None      ,						__snake_case=None      ,						__snake_case=False      ,						__snake_case=False      ,						__snake_case=None      ,						__snake_case=64      ,						__snake_case=4      ,						__snake_case=False      ,						__snake_case=False      ,						__snake_case=None      ,						__snake_case=False      ,						)     ->				Union[str, Any]:




						'''simple docstring'''
						__a						   =bert_score.utils.get_hash
						__a						   =bert_score.BERTScorer

						if version.parse(bert_score.__version__      ) >= version.parse('0.3.10'      ):
									__a						   =functools.partial(__snake_case      ,						use_fast_tokenizer=__snake_case      )
									__a						   =functools.partial(__snake_case      ,						use_fast_tokenizer=__snake_case      )
						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"
									__a						   =bert_score.utils.langamodel[lang.lower()]

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

						__a						   =get_hash(
						    model=__snake_case      ,						num_layers=__snake_case      ,						idf=__snake_case      ,						rescale_with_baseline=__snake_case      ,						use_custom_baseline=baseline_path is not None      ,						)

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

						((__a)   ,					(__a)   ,					(__a))						   =self.cached_bertscorer.score(
						    cands=__snake_case      ,						refs=__snake_case      ,						verbose=__snake_case      ,						batch_size=__snake_case      ,						)
						__a						   ={
						    'precision': P.tolist(),
						    'recall': R.tolist(),
						    'f1': F.tolist(),
						    'hashcode': hashcode,
						}
						return output_dict





			def 	__magic_name__					(	self      ,						__snake_case=None      ,						__snake_case=None      ,						**__snake_case      )     ->				str:




						'''simple docstring'''
						# References can be strings or lists of strings
						# Let's change strings to lists of strings with one element
						if references is not None:
									__a						   =[[ref] if isinstance(__snake_case      ,						__snake_case      ) else ref for ref in references]
						super().add_batch(predictions=__snake_case      ,						references=__snake_case      ,						**__snake_case      )





			def 	__magic_name__					(	self      ,						__snake_case=None      ,						__snake_case=None      ,						**__snake_case      )     ->				List[Any]:




						'''simple docstring'''
						# References can be strings or lists of strings
						# Let's change strings to lists of strings with one element
						if isinstance(__snake_case      ,						__snake_case      ):
									__a						   =[reference]
						super().add(prediction=__snake_case      ,						reference=__snake_case      ,						**__snake_case      )









# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
						import tensorflow as tf






class 			__magic_name__					(    TensorFormatter[Mapping, 'tf.Tensor', Mapping]   ):





			def __init__(	self      ,						__snake_case=None      ,						**__snake_case      )     ->				Union[str, Any]:




						'''simple docstring'''
						super().__init__(features=__snake_case      )
						__a						   =tf_tensor_kwargs
						import tensorflow as tf  # noqa: import tf at initialization





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				int:




						'''simple docstring'''
						import tensorflow as tf

						if isinstance(__snake_case      ,						__snake_case      ) and column:
									if all(
									    isinstance(__snake_case      ,						tf.Tensor      ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column      ):
												return tf.stack(__snake_case      )
									elif all(
									    isinstance(__snake_case      ,						(tf.Tensor, tf.RaggedTensor)      ) and x.ndim == 1 and x.dtype == column[0].dtype
									    for x in column      ):
												# only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
												return tf.ragged.stack(__snake_case      )

						return column





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Any:




						'''simple docstring'''
						import tensorflow as tf

						if value is None:
									return value

						__a						   ={}

						if isinstance(__snake_case      ,						(np.number, np.ndarray)      ) and np.issubdtype(value.dtype      ,						np.integer      ):
									__a						   ={'dtype': tf.intaa}
						elif isinstance(__snake_case      ,						(np.number, np.ndarray)      ) and np.issubdtype(value.dtype      ,						np.floating      ):
									__a						   ={'dtype': tf.floataa}
						elif config.PIL_AVAILABLE and "PIL" in sys.modules:
									import PIL.Image

									if isinstance(__snake_case      ,						PIL.Image.Image      ):
												__a						   =np.asarray(__snake_case      )

						return tf.convert_to_tensor(__snake_case      ,						**{**default_dtype, **self.tf_tensor_kwargs}      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Dict:




						'''simple docstring'''
						import tensorflow as tf

						# support for torch, tf, jax etc.
						if config.TORCH_AVAILABLE and "torch" in sys.modules:
									import torch

									if isinstance(__snake_case      ,						torch.Tensor      ):
												return self._tensorize(data_struct.detach().cpu().numpy()[()]      )
						if hasattr(__snake_case      ,						'__array__'      ) and not isinstance(__snake_case      ,						tf.Tensor      ):
									__a						   =data_struct.__array__()
						# support for nested types like struct of list of struct
						if isinstance(__snake_case      ,						np.ndarray      ):
									if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
												return self._consolidate([self.recursive_tensorize(__snake_case      ) for substruct in data_struct]      )
						elif isinstance(__snake_case      ,						(list, tuple)      ):
									return self._consolidate([self.recursive_tensorize(__snake_case      ) for substruct in data_struct]      )
						return self._tensorize(__snake_case      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Optional[Any]:




						'''simple docstring'''
						return map_nested(self._recursive_tensorize      ,						__snake_case      ,						map_list=__snake_case      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Mapping:




						'''simple docstring'''
						__a						   =self.numpy_arrow_extractor().extract_row(__snake_case      )
						__a						   =self.python_features_decoder.decode_row(__snake_case      )
						return self.recursive_tensorize(__snake_case      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				"tf.Tensor":




						'''simple docstring'''
						__a						   =self.numpy_arrow_extractor().extract_column(__snake_case      )
						__a						   =self.python_features_decoder.decode_column(__snake_case      ,						pa_table.column_names[0]      )
						__a						   =self.recursive_tensorize(__snake_case      )
						__a						   =self._consolidate(__snake_case      )
						return column





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Mapping:




						'''simple docstring'''
						__a						   =self.numpy_arrow_extractor().extract_batch(__snake_case      )
						__a						   =self.python_features_decoder.decode_batch(__snake_case      )
						__a						   =self.recursive_tensorize(__snake_case      )
						for column_name in batch:
									__a						   =self._consolidate(batch[column_name]      )
						return batch






