
import os

from ...utils import logging
from .utils import DataProcessor, InputExample


_UpperCAmelCase   :					Union[str, Any]    =			logging.get_logger(__name__)


class 							lowercase   (						_snake_case  ):



		def __init__(					self							, A_							, A_=None			)    ->   Any:
				"""simple docstring"""
				UpperCamelCase            =	language
				UpperCamelCase            =	train_language



		def __UpperCamelCase					(					self							, A_			)    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.language if self.train_language is None else self.train_language
				UpperCamelCase            =	self._read_tsv(os.path.join(lowerCAmelCase__							, F'''XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv'''			)			)
				UpperCamelCase            =	[]
				for i, line in enumerate(lowerCAmelCase__			):
						if i == 0:
								continue
						UpperCamelCase            =	F'''train-{i}'''
						UpperCamelCase            =	line[0]
						UpperCamelCase            =	line[1]
						UpperCamelCase            =	'contradiction' if line[2] == 'contradictory' else line[2]
						if not isinstance(lowerCAmelCase__							, lowerCAmelCase__			):
								raise ValueError(F'''Training input {text_a} is not a string'''			)
						if not isinstance(lowerCAmelCase__							, lowerCAmelCase__			):
								raise ValueError(F'''Training input {text_b} is not a string'''			)
						if not isinstance(lowerCAmelCase__							, lowerCAmelCase__			):
								raise ValueError(F'''Training label {label} is not a string'''			)
						examples.append(InputExample(guid=lowerCAmelCase__							, text_a=lowerCAmelCase__							, text_b=lowerCAmelCase__							, label=lowerCAmelCase__			)			)
				return examples



		def __UpperCamelCase					(					self							, A_			)    ->   Tuple:
				"""simple docstring"""
				UpperCamelCase            =	self._read_tsv(os.path.join(lowerCAmelCase__							, 'XNLI-1.0/xnli.test.tsv'			)			)
				UpperCamelCase            =	[]
				for i, line in enumerate(lowerCAmelCase__			):
						if i == 0:
								continue
						UpperCamelCase            =	line[0]
						if language != self.language:
								continue
						UpperCamelCase            =	F'''test-{i}'''
						UpperCamelCase            =	line[6]
						UpperCamelCase            =	line[7]
						UpperCamelCase            =	line[1]
						if not isinstance(lowerCAmelCase__							, lowerCAmelCase__			):
								raise ValueError(F'''Training input {text_a} is not a string'''			)
						if not isinstance(lowerCAmelCase__							, lowerCAmelCase__			):
								raise ValueError(F'''Training input {text_b} is not a string'''			)
						if not isinstance(lowerCAmelCase__							, lowerCAmelCase__			):
								raise ValueError(F'''Training label {label} is not a string'''			)
						examples.append(InputExample(guid=lowerCAmelCase__							, text_a=lowerCAmelCase__							, text_b=lowerCAmelCase__							, label=lowerCAmelCase__			)			)
				return examples



		def __UpperCamelCase					(					self			)    ->   int:
				"""simple docstring"""
				return ["contradiction", "entailment", "neutral"]






_UpperCAmelCase   :					Dict    =			{
    "xnli": XnliProcessor,
}

_UpperCAmelCase   :					List[Any]    =			{
    "xnli": "classification",
}

_UpperCAmelCase   :					List[str]    =			{
    "xnli": 3,
}



import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

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






class     __magic_name__     (							TensorFormatter[Mapping, np.ndarray, Mapping]     ):


 def __init__(    self		:			Optional[int]      ,    lowerCAmelCase__		:			str=None      ,    **lowerCAmelCase__		:			int     )     ->   Optional[Any]:
   super().__init__(features=lowerCAmelCase__     )
   UpperCAmelCase          =	np_array_kwargs


 def _UpperCamelCase						(    self		:			Any      ,    lowerCAmelCase__		:			Dict     )     ->   Union[str, Any]:
   if isinstance(lowerCAmelCase__      ,    lowerCAmelCase__     ):
     if column and all(
         isinstance(lowerCAmelCase__      ,    np.ndarray     ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column     ):
       return np.stack(lowerCAmelCase__     )
     else:
       # don't use np.array(column, dtype=object)
       # since it fails in certain cases
       # see https://stackoverflow.com/q/51005699
       UpperCAmelCase          =	np.empty(len(lowerCAmelCase__     )      ,    dtype=lowerCAmelCase__     )
       UpperCAmelCase          =	column
       return out
   return column


 def _UpperCamelCase						(    self		:			Optional[Any]      ,    lowerCAmelCase__		:			Union[str, Any]     )     ->   List[Any]:
   if isinstance(lowerCAmelCase__      ,    (str, bytes, type(lowerCAmelCase__     ))     ):
     return value
   elif isinstance(lowerCAmelCase__      ,    (np.character, np.ndarray)     ) and np.issubdtype(value.dtype      ,    np.character     ):
     return value
   elif isinstance(lowerCAmelCase__      ,    np.number     ):
     return value

   UpperCAmelCase          =	{}

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

     if isinstance(lowerCAmelCase__      ,    PIL.Image.Image     ):
       return np.asarray(lowerCAmelCase__      ,    **self.np_array_kwargs     )

   return np.asarray(lowerCAmelCase__      ,    **{**default_dtype, **self.np_array_kwargs}     )


 def _UpperCamelCase						(    self		:			List[Any]      ,    lowerCAmelCase__		:			str     )     ->   Optional[int]:
   # support for torch, tf, jax etc.
   if config.TORCH_AVAILABLE and "torch" in sys.modules:
     import torch

     if isinstance(lowerCAmelCase__      ,    torch.Tensor     ):
       return self._tensorize(data_struct.detach().cpu().numpy()[()]     )
   if hasattr(lowerCAmelCase__      ,    "__array__"     ) and not isinstance(lowerCAmelCase__      ,    (np.ndarray, np.character, np.number)     ):
     UpperCAmelCase          =	data_struct.__array__()
   # support for nested types like struct of list of struct
   if isinstance(lowerCAmelCase__      ,    np.ndarray     ):
     if data_struct.dtype == object:
       return self._consolidate([self.recursive_tensorize(lowerCAmelCase__     ) for substruct in data_struct]     )
   if isinstance(lowerCAmelCase__      ,    (list, tuple)     ):
     return self._consolidate([self.recursive_tensorize(lowerCAmelCase__     ) for substruct in data_struct]     )
   return self._tensorize(lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			Union[str, Any]      ,    lowerCAmelCase__		:			dict     )     ->   Union[str, Any]:
   return map_nested(self._recursive_tensorize      ,    lowerCAmelCase__      ,    map_list=lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			int      ,    lowerCAmelCase__		:			pa.Table     )     ->   Mapping:
   UpperCAmelCase          =	self.numpy_arrow_extractor().extract_row(lowerCAmelCase__     )
   UpperCAmelCase          =	self.python_features_decoder.decode_row(lowerCAmelCase__     )
   return self.recursive_tensorize(lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			List[str]      ,    lowerCAmelCase__		:			pa.Table     )     ->   np.ndarray:
   UpperCAmelCase          =	self.numpy_arrow_extractor().extract_column(lowerCAmelCase__     )
   UpperCAmelCase          =	self.python_features_decoder.decode_column(lowerCAmelCase__      ,    pa_table.column_names[0]     )
   UpperCAmelCase          =	self.recursive_tensorize(lowerCAmelCase__     )
   UpperCAmelCase          =	self._consolidate(lowerCAmelCase__     )
   return column


 def _UpperCamelCase						(    self		:			List[Any]      ,    lowerCAmelCase__		:			pa.Table     )     ->   Mapping:
   UpperCAmelCase          =	self.numpy_arrow_extractor().extract_batch(lowerCAmelCase__     )
   UpperCAmelCase          =	self.python_features_decoder.decode_batch(lowerCAmelCase__     )
   UpperCAmelCase          =	self.recursive_tensorize(lowerCAmelCase__     )
   for column_name in batch:
     UpperCAmelCase          =	self._consolidate(batch[column_name]     )
   return batch



