



import json
import os
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer
from ...utils import logging


__SCREAMING_SNAKE_CASE           =  logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE           =  """▁"""

__SCREAMING_SNAKE_CASE           =  {
    """vocab_file""": """vocab.json""",
    """spm_file""": """sentencepiece.bpe.model""",
    """tokenizer_config_file""": """tokenizer_config.json""",
}

__SCREAMING_SNAKE_CASE           =  {
    """vocab_file""": {
        """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json""",
        """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json""",
    },
    """spm_file""": {
        """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model""",
        """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model""",
    },
    """tokenizer_config_file""": {
        """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json""",
        """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json""",
    },
}

__SCREAMING_SNAKE_CASE           =  {
    """facebook/m2m100_418M""": 1024,
}

# fmt: off
__SCREAMING_SNAKE_CASE           =  {
    """m2m100""": ["""af""", """am""", """ar""", """ast""", """az""", """ba""", """be""", """bg""", """bn""", """br""", """bs""", """ca""", """ceb""", """cs""", """cy""", """da""", """de""", """el""", """en""", """es""", """et""", """fa""", """ff""", """fi""", """fr""", """fy""", """ga""", """gd""", """gl""", """gu""", """ha""", """he""", """hi""", """hr""", """ht""", """hu""", """hy""", """id""", """ig""", """ilo""", """is""", """it""", """ja""", """jv""", """ka""", """kk""", """km""", """kn""", """ko""", """lb""", """lg""", """ln""", """lo""", """lt""", """lv""", """mg""", """mk""", """ml""", """mn""", """mr""", """ms""", """my""", """ne""", """nl""", """no""", """ns""", """oc""", """or""", """pa""", """pl""", """ps""", """pt""", """ro""", """ru""", """sd""", """si""", """sk""", """sl""", """so""", """sq""", """sr""", """ss""", """su""", """sv""", """sw""", """ta""", """th""", """tl""", """tn""", """tr""", """uk""", """ur""", """uz""", """vi""", """wo""", """xh""", """yi""", """yo""", """zh""", """zu"""],
    """wmt21""": ["""en""", """ha""", """is""", """ja""", """cs""", """ru""", """zh""", """de"""]
}

class 	lowerCamelCase_    ( _A						):






						'''simple docstring'''
						a__   =  VOCAB_FILES_NAMES
						a__   =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						a__   =  PRETRAINED_VOCAB_FILES_MAP
						a__   =  ["input_ids", "attention_mask"]

						a__   =  []
						a__   =  []







						def __init__(	self      :			Dict	,	__lowerCamelCase      :			Optional[Any]	,	__lowerCamelCase      :			Optional[int]	,	__lowerCamelCase      :			Tuple=None	,	__lowerCamelCase      :			int=None	,	__lowerCamelCase      :			Union[str, Any]="<s>"	,	__lowerCamelCase      :			int="</s>"	,	__lowerCamelCase      :			str="</s>"	,	__lowerCamelCase      :			int="<pad>"	,	__lowerCamelCase      :			List[str]="<unk>"	,	__lowerCamelCase      :			str="m2m100"	,	__lowerCamelCase      :			Optional[Dict[str, Any]] = None	,	__lowerCamelCase      :			int=8	,	**__lowerCamelCase      :			List[Any]	,	)      ->				None:
									A							:					List[Any]              =						{} if sp_model_kwargs is None else sp_model_kwargs

									A							:					Optional[Any]              =						language_codes
									A							:					Union[str, Any]              =						FAIRSEQ_LANGUAGE_CODES[language_codes]
									A							:					Optional[Any]              =						{lang_code: F"""__{lang_code}__""" for lang_code in fairseq_language_code}

									A							:					Union[str, Any]              =						kwargs.get("additional_special_tokens"	,	[] )
									kwargs["additional_special_tokens"] += [
									    self.get_lang_token(__lowerCamelCase )
									    for lang_code in fairseq_language_code
									    if self.get_lang_token(__lowerCamelCase ) not in kwargs["additional_special_tokens"]
									]

									super().__init__(
									    src_lang=__lowerCamelCase	,	tgt_lang=__lowerCamelCase	,	bos_token=__lowerCamelCase	,	eos_token=__lowerCamelCase	,	sep_token=__lowerCamelCase	,	unk_token=__lowerCamelCase	,	pad_token=__lowerCamelCase	,	language_codes=__lowerCamelCase	,	sp_model_kwargs=self.sp_model_kwargs	,	num_madeup_words=__lowerCamelCase	,	**__lowerCamelCase	,	)

									A							:					Dict              =						vocab_file
									A							:					List[str]              =						load_json(__lowerCamelCase )
									A							:					Optional[Any]              =						{v: k for k, v in self.encoder.items()}
									A							:					Any              =						spm_file
									A							:					List[Any]              =						load_spm(__lowerCamelCase	,	self.sp_model_kwargs )

									A							:					str              =						len(self.encoder )

									A							:					List[str]              =						{
									    self.get_lang_token(__lowerCamelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCamelCase )
									}
									A							:					List[str]              =						{lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCamelCase )}
									A							:					Optional[Any]              =						{v: k for k, v in self.lang_token_to_id.items()}

									A							:					List[str]              =						src_lang if src_lang is not None else "en"
									A							:					Union[str, Any]              =						tgt_lang
									A							:					Union[str, Any]              =						self.get_lang_id(self._src_lang )
									self.set_src_lang_special_tokens(self._src_lang )

									A							:					str              =						num_madeup_words







						@property
						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				int:
									return len(self.encoder ) + len(self.lang_token_to_id )







						@property
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				str:
									return self._src_lang







						@src_lang.setter
						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str]	,	__lowerCamelCase      :			str )      ->				None:
									A							:					str              =						new_src_lang
									self.set_src_lang_special_tokens(self._src_lang )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			str )      ->				List[str]:
									return self.sp_model.encode(__lowerCamelCase	,	out_type=__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str]	,	__lowerCamelCase      :			str )      ->				Tuple:
									if token in self.lang_token_to_id:
												return self.lang_token_to_id[token]
									return self.encoder.get(__lowerCamelCase	,	self.encoder[self.unk_token] )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			int )      ->				str:
									if index in self.id_to_lang_token:
												return self.id_to_lang_token[index]
									return self.decoder.get(__lowerCamelCase	,	self.unk_token )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int]	,	__lowerCamelCase      :			Optional[Any] )      ->				List[Any]:
									A							:					Any              =						[]
									A							:					Union[str, Any]              =						""
									for token in tokens:
												# make sure that special tokens are not decoded using sentencepiece model
												if token in self.all_special_tokens:
															out_string += self.sp_model.decode(__lowerCamelCase ) + token
															A							:					Optional[int]              =						[]
												else:
															current_sub_tokens.append(__lowerCamelCase )
									out_string += self.sp_model.decode(__lowerCamelCase )
									return out_string.strip()







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int	,	__lowerCamelCase      :			List[int]	,	__lowerCamelCase      :			Optional[List[int]] = None	,	__lowerCamelCase      :			bool = False )      ->				List[int]:

									if already_has_special_tokens:
												return super().get_special_tokens_mask(
												    token_ids_a=__lowerCamelCase	,	token_ids_a=__lowerCamelCase	,	already_has_special_tokens=__lowerCamelCase )

									A							:					str              =						[1] * len(self.prefix_tokens )
									A							:					Dict              =						[1] * len(self.suffix_tokens )
									if token_ids_a is None:
												return prefix_ones + ([0] * len(__lowerCamelCase )) + suffix_ones
									return prefix_ones + ([0] * len(__lowerCamelCase )) + ([0] * len(__lowerCamelCase )) + suffix_ones







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			List[int]	,	__lowerCamelCase      :			Optional[List[int]] = None )      ->				List[int]:
									if token_ids_a is None:
												return self.prefix_tokens + token_ids_a + self.suffix_tokens
									# We don't expect to process pairs, but leave the pair logic for API consistency
									return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				Dict:
									A							:					List[Any]              =						{self.convert_ids_to_tokens(__lowerCamelCase ): i for i in range(self.vocab_size )}
									vocab.update(self.added_tokens_encoder )
									return vocab







						def __getstate__(	self      :			Optional[int] )      ->				Dict:
									A							:					Optional[Any]              =						self.__dict__.copy()
									A							:					List[Any]              =						None
									return state







						def __setstate__(	self      :			str	,	__lowerCamelCase      :			Dict )      ->				None:
									A							:					List[Any]              =						d

									# for backward compatibility
									if not hasattr(self	,	"sp_model_kwargs" ):
												A							:					List[str]              =						{}

									A							:					List[Any]              =						load_spm(self.spm_file	,	self.sp_model_kwargs )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str]	,	__lowerCamelCase      :			str	,	__lowerCamelCase      :			Optional[str] = None )      ->				Tuple[str]:
									A							:					List[str]              =						Path(__lowerCamelCase )
									if not save_dir.is_dir():
												raise OSError(F"""{save_directory} should be a directory""" )
									A							:					int              =						save_dir / (
									    (filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["vocab_file"]
									)
									A							:					Optional[int]              =						save_dir / (
									    (filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["spm_file"]
									)

									save_json(self.encoder	,	__lowerCamelCase )

									if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCamelCase ) and os.path.isfile(self.spm_file ):
												copyfile(self.spm_file	,	__lowerCamelCase )
									elif not os.path.isfile(self.spm_file ):
												with open(__lowerCamelCase	,	"wb" ) as fi:
															A							:					Dict              =						self.sp_model.serialized_model_proto()
															fi.write(__lowerCamelCase )

									return (str(__lowerCamelCase ), str(__lowerCamelCase ))







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			List[str]	,	__lowerCamelCase      :			str = "en"	,	__lowerCamelCase      :			Optional[List[str]] = None	,	__lowerCamelCase      :			str = "ro"	,	**__lowerCamelCase      :			Tuple	,	)      ->				BatchEncoding:
									A							:					Dict              =						src_lang
									A							:					int              =						tgt_lang
									self.set_src_lang_special_tokens(self.src_lang )
									return super().prepare_seqaseq_batch(__lowerCamelCase	,	__lowerCamelCase	,	**__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			Dict	,	__lowerCamelCase      :			Optional[str]	,	__lowerCamelCase      :			Optional[str]	,	**__lowerCamelCase      :			Dict )      ->				Optional[int]:
									if src_lang is None or tgt_lang is None:
												raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model" )
									A							:					Any              =						src_lang
									A							:					Optional[int]              =						self(__lowerCamelCase	,	add_special_tokens=__lowerCamelCase	,	**__lowerCamelCase )
									A							:					int              =						self.get_lang_id(__lowerCamelCase )
									A							:					Any              =						tgt_lang_id
									return inputs







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				Optional[int]:
									self.set_src_lang_special_tokens(self.src_lang )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				Optional[int]:
									self.set_tgt_lang_special_tokens(self.tgt_lang )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple	,	__lowerCamelCase      :			str )      ->				None:
									A							:					Optional[int]              =						self.get_lang_token(__lowerCamelCase )
									A							:					List[str]              =						self.lang_token_to_id[lang_token]
									A							:					str              =						[self.cur_lang_id]
									A							:					Tuple              =						[self.eos_token_id]







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple	,	__lowerCamelCase      :			str )      ->				None:
									A							:					List[Any]              =						self.get_lang_token(__lowerCamelCase )
									A							:					int              =						self.lang_token_to_id[lang_token]
									A							:					List[Any]              =						[self.cur_lang_id]
									A							:					Tuple              =						[self.eos_token_id]







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any]	,	__lowerCamelCase      :			str )      ->				str:
									return self.lang_code_to_token[lang]







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str]	,	__lowerCamelCase      :			str )      ->				int:
									A							:					Optional[Any]              =						self.get_lang_token(__lowerCamelCase )
									return self.lang_token_to_id[lang_token]







def 	UpperCAmelCase   (						_lowerCamelCase ,    _lowerCamelCase    ):
			A							:					Dict              =						sentencepiece.SentencePieceProcessor(**_lowerCamelCase    )
			spm.Load(str(_lowerCamelCase    )    )
			return spm




def 	UpperCAmelCase   (						_lowerCamelCase    ):
			with open(_lowerCamelCase ,    "r"    ) as f:
						return json.load(_lowerCamelCase    )






def 	UpperCAmelCase   (						_lowerCamelCase ,    _lowerCamelCase    ):
			with open(_lowerCamelCase ,    "w"    ) as f:
						json.dump(_lowerCamelCase ,    _lowerCamelCase ,    indent=2    )




from sklearn.metrics import recall_score

import datasets


__SCREAMING_SNAKE_CASE           =  """
Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:
Recall = TP / (TP + FN)
Where TP is the true positives and FN is the false negatives.
"""


__SCREAMING_SNAKE_CASE           =  """
Args:
- **predictions** (`list` of `int`): The predicted labels.
- **references** (`list` of `int`): The ground truth labels.
- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.
- **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`.
- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.
    - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.
    - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.
    - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
    - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.
    - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).
- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.
- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .
    - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised.
    - `0`: If there is a zero division, the return value is `0`.
    - `1`: If there is a zero division, the return value is `1`.

Returns:
- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.

Examples:

    Example 1-A simple example with some errors
        >>> recall_metric = datasets.load_metric('recall')
        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])
        >>> print(results)
        {'recall': 0.6666666666666666}

    Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.
        >>> recall_metric = datasets.load_metric('recall')
        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)
        >>> print(results)
        {'recall': 0.5}

    Example 3-The same example as Example 1, but with `sample_weight` included.
        >>> recall_metric = datasets.load_metric('recall')
        >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]
        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)
        >>> print(results)
        {'recall': 0.55}

    Example 4-A multiclass example, using different averages.
        >>> recall_metric = datasets.load_metric('recall')
        >>> predictions = [0, 2, 1, 0, 0, 1]
        >>> references = [0, 1, 2, 0, 1, 2]
        >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro')
        >>> print(results)
        {'recall': 0.3333333333333333}
        >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro')
        >>> print(results)
        {'recall': 0.3333333333333333}
        >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted')
        >>> print(results)
        {'recall': 0.3333333333333333}
        >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)
        >>> print(results)
        {'recall': array([1., 0., 0.])}
"""


__SCREAMING_SNAKE_CASE           =  """
@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}
"""

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






						'''simple docstring'''







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				str:
									return datasets.MetricInfo(
									    description=_DESCRIPTION	,	citation=_CITATION	,	inputs_description=_KWARGS_DESCRIPTION	,	features=datasets.Features(
									        {
									            "predictions": datasets.Sequence(datasets.Value("int32" ) ),
									            "references": datasets.Sequence(datasets.Value("int32" ) ),
									        }
									        if self.config_name == "multilabel"
									        else {
									            "predictions": datasets.Value("int32" ),
									            "references": datasets.Value("int32" ),
									        } )	,	reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"]	,	)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any	,	__lowerCamelCase      :			int	,	__lowerCamelCase      :			Dict	,	__lowerCamelCase      :			str=None	,	__lowerCamelCase      :			List[Any]=1	,	__lowerCamelCase      :			Tuple="binary"	,	__lowerCamelCase      :			Tuple=None	,	__lowerCamelCase      :			Tuple="warn"	,	)      ->				Optional[Any]:
									A							:					str              =						recall_score(
									    __lowerCamelCase	,	__lowerCamelCase	,	labels=__lowerCamelCase	,	pos_label=__lowerCamelCase	,	average=__lowerCamelCase	,	sample_weight=__lowerCamelCase	,	zero_division=__lowerCamelCase	,	)
									return {"recall": float(__lowerCamelCase ) if score.size == 1 else score}