





'''simple docstring'''


import numpy as np
from scipy.spatial.distance import cdist
from sklearn.metrics import fa_score

import datasets


_SCREAMING_SNAKE_CASE	 =							"\\n    @inproceedings{kakwani2020indicnlpsuite,\n    title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n    author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n    year={2020},\n    booktitle={Findings of EMNLP},\n}\n"

_SCREAMING_SNAKE_CASE	 =							"\\n    IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n    variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n"

_SCREAMING_SNAKE_CASE	 =							"\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n    predictions: list of predictions to score (as int64),\n        except for 'cvit-mkb-clsr' where each prediction is a vector (of float32).\n    references: list of ground truth labels corresponding to the predictions (as int64),\n        except for 'cvit-mkb-clsr' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n    \"accuracy\": Accuracy\n    \"f1\": F1 score\n    \"precision\": Precision@10\nExamples:\n\n    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'wnli')  # 'wnli' or any of [\"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'wiki-ner')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0, 'f1': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'cvit-mkb-clsr')\n    >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'precision@10': 1.0}\n\n"


def 	__a(SCREAMING_SNAKE_CASE_  :					Optional[Any]		,       SCREAMING_SNAKE_CASE_  :					str    ):



    '''simple docstring'''





    return float((preds == labels).mean()    )


def 	__a(SCREAMING_SNAKE_CASE_  :					Tuple		,       SCREAMING_SNAKE_CASE_  :					Optional[int]    ):



    '''simple docstring'''





    _lowerCAmelCase				=      simple_accuracy(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      float(fa_score(y_true=SCREAMING_SNAKE_CASE_		,       y_pred=SCREAMING_SNAKE_CASE_    )    )
    return {
        "accuracy": acc,
        "f1": fa,
    }


def 	__a(SCREAMING_SNAKE_CASE_  :					str		,       SCREAMING_SNAKE_CASE_  :					List[str]    ):



    '''simple docstring'''





    _lowerCAmelCase				=      np.array(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      np.array(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      en_sentvecs.shape[0]

    # mean centering
    _lowerCAmelCase				=      en_sentvecs - np.mean(SCREAMING_SNAKE_CASE_		,       axis=0    )
    _lowerCAmelCase				=      in_sentvecs - np.mean(SCREAMING_SNAKE_CASE_		,       axis=0    )

    _lowerCAmelCase				=      cdist(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_		,       "cosine"    )
    _lowerCAmelCase				=      np.array(range(SCREAMING_SNAKE_CASE_    )    )
    _lowerCAmelCase				=      sim.argsort(axis=1    )[:, :10]
    _lowerCAmelCase				=      np.any(preds == actual[:, None]		,       axis=1    )
    return float(matches.mean()    )





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




 def 							_snake_case   (	self		)    ->							List[Any]:
     if self.config_name not in [
         "wnli",
         "copa",
         "sna",
         "csqa",
         "wstp",
         "inltkh",
         "bbca",
         "cvit-mkb-clsr",
         "iitp-mr",
         "iitp-pr",
         "actsa-sc",
         "md",
         "wiki-ner",
     ]:
         raise KeyError(
             "You should supply a configuration name selected in "
             "[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", "
             "\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", "
             "\"wiki-ner\"]"		)
     return datasets.MetricInfo(
         description=_DESCRIPTION			,     citation=_CITATION			,     inputs_description=_KWARGS_DESCRIPTION			,     features=datasets.Features(
             {
                 "predictions": datasets.Value("int64"		)
                 if self.config_name != "cvit-mkb-clsr"
                 else datasets.Sequence(datasets.Value("float32"		)		),
                 "references": datasets.Value("int64"		)
                 if self.config_name != "cvit-mkb-clsr"
                 else datasets.Sequence(datasets.Value("float32"		)		),
             }		)			,     codebase_urls=[]			,     reference_urls=[]			,     format="numpy" if self.config_name != "cvit-mkb-clsr" else None			,     )





 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							List[str]:
     if self.config_name == "cvit-mkb-clsr":
         return {"precision@10": precision_at_aa(_lowerCAmelCase			,     _lowerCAmelCase		)}
     elif self.config_name in ["wiki-ner"]:
         return acc_and_fa(_lowerCAmelCase			,     _lowerCAmelCase		)
     elif self.config_name in [
         "wnli",
         "copa",
         "sna",
         "csqa",
         "wstp",
         "inltkh",
         "bbca",
         "iitp-mr",
         "iitp-pr",
         "actsa-sc",
         "md",
     ]:
         return {"accuracy": simple_accuracy(_lowerCAmelCase			,     _lowerCAmelCase		)}
     else:
         raise KeyError(
             "You should supply a configuration name selected in "
             "[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", "
             "\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", "
             "\"wiki-ner\"]"		)









'''simple docstring'''


import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_SCREAMING_SNAKE_CASE	 =							logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE	 =							"▁"

_SCREAMING_SNAKE_CASE	 =							{"vocab_file": "sentencepiece.bpe.model"}

_SCREAMING_SNAKE_CASE	 =							{
    "vocab_file": {
        "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model",
    }
}

_SCREAMING_SNAKE_CASE	 =							{
    "facebook/xglm-564M": 20_48,
}

class    lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase						:							int         =					VOCAB_FILES_NAMES
 __lowerCamelCase						:							Optional[int]         =					PRETRAINED_VOCAB_FILES_MAP
 __lowerCamelCase						:							Tuple         =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __lowerCamelCase						:							str         =					["input_ids", "attention_mask"]




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase="<s>"			,     _lowerCAmelCase="</s>"			,     _lowerCAmelCase="</s>"			,     _lowerCAmelCase="<s>"			,     _lowerCAmelCase="<unk>"			,     _lowerCAmelCase="<pad>"			,     _lowerCAmelCase = None			,     **_lowerCAmelCase			,     )    ->							None:
     _lowerCAmelCase				=      {} if sp_model_kwargs is None else sp_model_kwargs

     # Compatibility with the original tokenizer
     _lowerCAmelCase				=      7
     _lowerCAmelCase				=      [f'''<madeupword{i}>''' for i in range(self.num_madeup_words		)]

     _lowerCAmelCase				=      kwargs.get("additional_special_tokens"			,     []		)
     kwargs["additional_special_tokens"] += [
         word for word in madeup_words if word not in kwargs["additional_special_tokens"]
     ]

     super().__init__(
         bos_token=_lowerCAmelCase			,     eos_token=_lowerCAmelCase			,     unk_token=_lowerCAmelCase			,     sep_token=_lowerCAmelCase			,     cls_token=_lowerCAmelCase			,     pad_token=_lowerCAmelCase			,     sp_model_kwargs=self.sp_model_kwargs			,     **_lowerCAmelCase			,     )

     _lowerCAmelCase				=      spm.SentencePieceProcessor(**self.sp_model_kwargs		)
     self.sp_model.Load(str(_lowerCAmelCase		)		)
     _lowerCAmelCase				=      vocab_file

     # Original fairseq vocab and spm vocab must be "aligned":
     # Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
     # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
     # fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
     # spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

     # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
     _lowerCAmelCase				=      1

     # Mimic fairseq token-to-id alignment for the first 4 token
     _lowerCAmelCase				=      {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3}

     _lowerCAmelCase				=      len(self.sp_model		)
     _lowerCAmelCase				=      {f'''<madeupword{i}>''': sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words		)}
     self.fairseq_tokens_to_ids.update(_lowerCAmelCase		)

     _lowerCAmelCase				=      {v: k for k, v in self.fairseq_tokens_to_ids.items()}




 def __getstate__(	self		)    ->							List[str]:
     _lowerCAmelCase				=      self.__dict__.copy()
     _lowerCAmelCase				=      None
     _lowerCAmelCase				=      self.sp_model.serialized_model_proto()
     return state




 def __setstate__(	self			,     _lowerCAmelCase		)    ->							Optional[int]:
     _lowerCAmelCase				=      d

     # for backward compatibility
     if not hasattr(self			,     "sp_model_kwargs"		):
         _lowerCAmelCase				=      {}

     _lowerCAmelCase				=      spm.SentencePieceProcessor(**self.sp_model_kwargs		)
     self.sp_model.LoadFromSerializedProto(self.sp_model_proto		)




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase = None		)    ->							List[int]:

     if token_ids_a is None:
         return [self.sep_token_id] + token_ids_a
     _lowerCAmelCase				=      [self.sep_token_id]
     return sep + token_ids_a + sep + sep + token_ids_a




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase = None			,     _lowerCAmelCase = 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		)

     if token_ids_a is None:
         return [1] + ([0] * len(_lowerCAmelCase		))
     return [1] + ([0] * len(_lowerCAmelCase		)) + [1, 1] + ([0] * len(_lowerCAmelCase		))




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase = None		)    ->							List[int]:
     _lowerCAmelCase				=      [self.sep_token_id]

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




 @property
 def 							_snake_case   (	self		)    ->							str:
     return len(self.sp_model		) + self.fairseq_offset + self.num_madeup_words




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      {self.convert_ids_to_tokens(_lowerCAmelCase		): i for i in range(self.vocab_size		)}
     vocab.update(self.added_tokens_encoder		)
     return vocab




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							List[str]:
     return self.sp_model.encode(_lowerCAmelCase			,     out_type=_lowerCAmelCase		)




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							List[str]:
     if token in self.fairseq_tokens_to_ids:
         return self.fairseq_tokens_to_ids[token]
     _lowerCAmelCase				=      self.sp_model.PieceToId(_lowerCAmelCase		)

     # Need to return unknown token if the SP model returned 0
     return spm_id + self.fairseq_offset if spm_id else self.unk_token_id




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							List[Any]:
     if index in self.fairseq_ids_to_tokens:
         return self.fairseq_ids_to_tokens[index]
     return self.sp_model.IdToPiece(index - self.fairseq_offset		)




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      "".join(_lowerCAmelCase		).replace(_lowerCAmelCase			,     " "		).strip()
     return out_string





 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase = None		)    ->							Tuple[str]:
     if not os.path.isdir(_lowerCAmelCase		):
         logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''		)
         return
     _lowerCAmelCase				=      os.path.join(
         _lowerCAmelCase			,     (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)

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

     return (out_vocab_file,)


