



'''simple docstring'''

import datasets

from .nmt_bleu import compute_bleu  # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py


SCREAMING_SNAKE_CASE__     :						Tuple	=  '''\
@INPROCEEDINGS{Papineni02bleu:a,
    author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu},
    title = {BLEU: a Method for Automatic Evaluation of Machine Translation},
    booktitle = {},
    year = {2002},
    pages = {311--318}
}
@inproceedings{lin-och-2004-orange,
    title = "{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation",
    author = "Lin, Chin-Yew  and
      Och, Franz Josef",
    booktitle = "{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics",
    month = "aug 23{--}aug 27",
    year = "2004",
    address = "Geneva, Switzerland",
    publisher = "COLING",
    url = "https://www.aclweb.org/anthology/C04-1072",
    pages = "501--507",
}
'''

SCREAMING_SNAKE_CASE__     :						int	=  '''\
BLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another.
Quality is considered to be the correspondence between a machine\'s output and that of a human: "the closer a machine translation is to a professional human translation,
the better it is" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and
remains one of the most popular automated and inexpensive metrics.

Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations.
Those scores are then averaged over the whole corpus to reach an estimate of the translation\'s overall quality. Intelligibility or grammatical correctness
are not taken into account[citation needed].

BLEU\'s output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1
representing more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the
reference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional
reference translations will increase the BLEU score.
'''

SCREAMING_SNAKE_CASE__     :						Tuple	=  '''
Computes BLEU score of translated segments against one or more references.
Args:
    predictions: list of translations to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
    max_order: Maximum n-gram order to use when computing BLEU score.
    smooth: Whether or not to apply Lin et al. 2004 smoothing.
Returns:
    \'bleu\': bleu score,
    \'precisions\': geometric mean of n-gram precisions,
    \'brevity_penalty\': brevity penalty,
    \'length_ratio\': ratio of lengths,
    \'translation_length\': translation_length,
    \'reference_length\': reference_length
Examples:

    >>> predictions = [
    ...     ["hello", "there", "general", "kenobi"],                             # tokenized prediction of the first sample
    ...     ["foo", "bar", "foobar"]                                             # tokenized prediction of the second sample
    ... ]
    >>> references = [
    ...     [["hello", "there", "general", "kenobi"], ["hello", "there", "!"]],  # tokenized references for the first sample (2 references)
    ...     [["foo", "bar", "foobar"]]                                           # tokenized references for the second sample (1 reference)
    ... ]
    >>> bleu = datasets.load_metric("bleu")
    >>> results = bleu.compute(predictions=predictions, references=references)
    >>> print(results["bleu"])
    1.0
'''

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






			def 						_lowercase  (     self						)							->							str:
					return datasets.MetricInfo(
					    description=_DESCRIPTION					,   citation=_CITATION					,   inputs_description=_KWARGS_DESCRIPTION					,   features=datasets.Features(
					        {
					            'predictions': datasets.Sequence(datasets.Value('string'					,   id='token'						)					,   id='sequence'						),
					            'references': datasets.Sequence(
					                datasets.Sequence(datasets.Value('string'					,   id='token'						)					,   id='sequence'						)					,   id='references'						),
					        }						)					,   codebase_urls=['https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py']					,   reference_urls=[
					        'https://en.wikipedia.org/wiki/BLEU',
					        'https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213',
					    ]					,   )






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase=4					,   _UpperCAmelCase=False						)							->							int:
					snake_case__	     =compute_bleu(
					    reference_corpus=_UpperCAmelCase					,   translation_corpus=_UpperCAmelCase					,   max_order=_UpperCAmelCase					,   smooth=_UpperCAmelCase						)
					((snake_case__)     ,	(snake_case__)     ,	(snake_case__)     ,	(snake_case__)     ,	(snake_case__)     ,	(snake_case__))	     =score
					return {
					    "bleu": bleu,
					    "precisions": precisions,
					    "brevity_penalty": bp,
					    "length_ratio": ratio,
					    "translation_length": translation_length,
					    "reference_length": reference_length,
					}









'''simple docstring'''

import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bart import BartTokenizer


SCREAMING_SNAKE_CASE__     :						List[str]	=  logging.get_logger(__name__)


SCREAMING_SNAKE_CASE__     :						Optional[Any]	=  {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

# See all BART models at https://huggingface.co/models?filter=bart
SCREAMING_SNAKE_CASE__     :						Dict	=  {
    '''vocab_file''': {
        '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/vocab.json''',
        '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/vocab.json''',
        '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json''',
        '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json''',
        '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json''',
        '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/merges.txt''',
        '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/merges.txt''',
        '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt''',
        '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt''',
        '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt''',
        '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt''',
    },
    '''tokenizer_file''': {
        '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json''',
        '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json''',
        '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json''',
        '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json''',
        '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json''',
        '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json''',
    },
}

SCREAMING_SNAKE_CASE__     :						Optional[int]	=  {
    '''facebook/bart-base''': 1_0_2_4,
    '''facebook/bart-large''': 1_0_2_4,
    '''facebook/bart-large-mnli''': 1_0_2_4,
    '''facebook/bart-large-cnn''': 1_0_2_4,
    '''facebook/bart-large-xsum''': 1_0_2_4,
    '''yjernite/bart_eli5''': 1_0_2_4,
}

class 							a__( snake_case__     ):
			a_	: Dict									=      VOCAB_FILES_NAMES
			a_	: Tuple									=      PRETRAINED_VOCAB_FILES_MAP
			a_	: Tuple									=      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			a_	: List[Any]									=      ['''input_ids''', '''attention_mask''']
			a_	: Union[str, Any]									=      BartTokenizer






			def __init__(     self					,   _UpperCAmelCase=None					,   _UpperCAmelCase=None					,   _UpperCAmelCase=None					,   _UpperCAmelCase="replace"					,   _UpperCAmelCase="<s>"					,   _UpperCAmelCase="</s>"					,   _UpperCAmelCase="</s>"					,   _UpperCAmelCase="<s>"					,   _UpperCAmelCase="<unk>"					,   _UpperCAmelCase="<pad>"					,   _UpperCAmelCase="<mask>"					,   _UpperCAmelCase=False					,   _UpperCAmelCase=True					,   **_UpperCAmelCase					,   )							->							int:
					super().__init__(
					    _UpperCAmelCase					,   _UpperCAmelCase					,   tokenizer_file=_UpperCAmelCase					,   errors=_UpperCAmelCase					,   bos_token=_UpperCAmelCase					,   eos_token=_UpperCAmelCase					,   sep_token=_UpperCAmelCase					,   cls_token=_UpperCAmelCase					,   unk_token=_UpperCAmelCase					,   pad_token=_UpperCAmelCase					,   mask_token=_UpperCAmelCase					,   add_prefix_space=_UpperCAmelCase					,   trim_offsets=_UpperCAmelCase					,   **_UpperCAmelCase					,   )

					snake_case__	     =json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()						)
					if pre_tok_state.get('add_prefix_space'					,   _UpperCAmelCase						) != add_prefix_space:
							snake_case__	     =getattr(_UpperCAmelCase					,   pre_tok_state.pop('type'						)						)
							snake_case__	     =add_prefix_space
							snake_case__	     =pre_tok_class(**_UpperCAmelCase						)

					snake_case__	     =add_prefix_space

					# the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
					snake_case__	     ='post_processor'
					snake_case__	     =getattr(self.backend_tokenizer					,   _UpperCAmelCase					,   _UpperCAmelCase						)
					if tokenizer_component_instance:
							snake_case__	     =json.loads(tokenizer_component_instance.__getstate__()						)

							# The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
							if "sep" in state:
									snake_case__	     =tuple(state['sep']						)
							if "cls" in state:
									snake_case__	     =tuple(state['cls']						)

							snake_case__	     =False

							if state.get('add_prefix_space'					,   _UpperCAmelCase						) != add_prefix_space:
									snake_case__	     =add_prefix_space
									snake_case__	     =True

							if state.get('trim_offsets'					,   _UpperCAmelCase						) != trim_offsets:
									snake_case__	     =trim_offsets
									snake_case__	     =True

							if changes_to_apply:
									snake_case__	     =getattr(_UpperCAmelCase					,   state.pop('type'						)						)
									snake_case__	     =component_class(**_UpperCAmelCase						)
									setattr(self.backend_tokenizer					,   _UpperCAmelCase					,   _UpperCAmelCase						)






			@property
			def 						_lowercase  (     self						)							->							str:
					if self._mask_token is None:
							if self.verbose:
									logger.error('Using mask_token, but it is not set yet.'						)
							return None
					return str(self._mask_token						)






			@mask_token.setter
			def 						_lowercase  (     self					,   _UpperCAmelCase						)							->							int:
					snake_case__	     =AddedToken(_UpperCAmelCase					,   lstrip=_UpperCAmelCase					,   rstrip=_UpperCAmelCase						) if isinstance(_UpperCAmelCase					,   _UpperCAmelCase						) else value
					snake_case__	     =value






			def 						_lowercase  (     self					,   *_UpperCAmelCase					,   **_UpperCAmelCase						)							->							BatchEncoding:
					snake_case__	     =kwargs.get('is_split_into_words'					,   _UpperCAmelCase						)

					if is_split_into_words and not self.add_prefix_space:
							raise ValueError(
							    f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
							    'to use it with pretokenized inputs.'						)

					return super()._batch_encode_plus(*_UpperCAmelCase					,   **_UpperCAmelCase						)






			def 						_lowercase  (     self					,   *_UpperCAmelCase					,   **_UpperCAmelCase						)							->							BatchEncoding:
					snake_case__	     =kwargs.get('is_split_into_words'					,   _UpperCAmelCase						)

					if is_split_into_words and not self.add_prefix_space:
							raise ValueError(
							    f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
							    'to use it with pretokenized inputs.'						)

					return super()._encode_plus(*_UpperCAmelCase					,   **_UpperCAmelCase						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase = None						)							->							Tuple[str]:
					snake_case__	     =self._tokenizer.model.save(_UpperCAmelCase					,   name=_UpperCAmelCase						)
					return tuple(_UpperCAmelCase						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase=None						)							->							Optional[int]:
					snake_case__	     =[self.bos_token_id] + token_ids_a + [self.eos_token_id]
					if token_ids_a is None:
							return output

					return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase = None						)							->							List[int]:
					snake_case__	     =[self.sep_token_id]
					snake_case__	     =[self.cls_token_id]

					if token_ids_a is None:
							return len(cls + token_ids_a + sep						) * [0]
					return len(cls + token_ids_a + sep + sep + token_ids_a + sep						) * [0]




