



"""simple docstring"""







import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


A     =      logging.get_logger(__name__)






@dataclass
class   UpperCAmelCase__		:
		lowerCAmelCase_							:						str
		lowerCAmelCase_							:						str
		lowerCAmelCase_							:						Optional[str]							=  None
		lowerCAmelCase_							:						Optional[str]							=  None



		def        A_		(			self	:     List[str]					)      ->    List[str]:






									'''simple docstring'''

									return json.dumps(dataclasses.asdict(self					)   ,				indent=2					) + "\n"






@dataclass(frozen=UpperCamelCase  )
class   UpperCAmelCase__		:
		lowerCAmelCase_							:						List[int]
		lowerCAmelCase_							:						Optional[List[int]]							=  None
		lowerCAmelCase_							:						Optional[List[int]]							=  None
		lowerCAmelCase_							:						Optional[Union[int, float]]							=  None



		def        A_		(			self	:     Any					)      ->    List[Any]:






									'''simple docstring'''

									return json.dumps(dataclasses.asdict(self					)					) + "\n"






class   UpperCAmelCase__		:



		def        A_		(			self	:     Optional[int]   ,				snake_case	:     Tuple					)      ->    Union[str, Any]:






									'''simple docstring'''

									raise NotImplementedError()



		def        A_		(			self	:     int   ,				snake_case	:     Tuple					)      ->    str:






									'''simple docstring'''

									raise NotImplementedError()



		def        A_		(			self	:     Optional[int]   ,				snake_case	:     Union[str, Any]					)      ->    Tuple:






									'''simple docstring'''

									raise NotImplementedError()



		def        A_		(			self	:     Dict   ,				snake_case	:     List[str]					)      ->    Optional[int]:






									'''simple docstring'''

									raise NotImplementedError()



		def        A_		(			self	:     Any					)      ->    int:






									'''simple docstring'''

									raise NotImplementedError()



		def        A_		(			self	:     Optional[Any]   ,				snake_case	:     List[str]					)      ->    Any:






									'''simple docstring'''

									if len(self.get_labels()					) > 1:
																A    =       self.get_labels()[int(example.label					)]
									return example



		@classmethod
		def        A_		(			cls	:     str   ,				snake_case	:     str   ,				snake_case	:     Union[str, Any]=None					)      ->    List[Any]:






									'''simple docstring'''

									with open(snake_case   ,				'r'   ,				encoding='utf-8-sig'					) as f:
																return list(csv.reader(snake_case   ,				delimiter='\t'   ,				quotechar=snake_case					)					)






class   UpperCAmelCase__		(   UpperCamelCase  ):



		def __init__(			self	:     str   ,				snake_case	:     int=None   ,				snake_case	:     List[Any]=None   ,				snake_case	:     str="classification"   ,				snake_case	:     Tuple=False					)      ->    Dict:






									'''simple docstring'''

									A    =       [] if labels is None else labels
									A    =       [] if examples is None else examples
									A    =       mode
									A    =       verbose



		def __len__(			self	:     Any					)      ->    Optional[Any]:






									'''simple docstring'''

									return len(self.examples					)



		def __getitem__(			self	:     Optional[int]   ,				snake_case	:     List[str]					)      ->    Tuple:






									'''simple docstring'''

									if isinstance(snake_case   ,				snake_case					):
																return SingleSentenceClassificationProcessor(labels=self.labels   ,				examples=self.examples[idx]					)
									return self.examples[idx]



		@classmethod
		def        A_		(			cls	:     Tuple   ,				snake_case	:     Optional[Any]   ,				snake_case	:     List[str]=""   ,				snake_case	:     Any=0   ,				snake_case	:     List[Any]=1   ,				snake_case	:     Optional[int]=None   ,				snake_case	:     Tuple=False   ,				**snake_case	:     Dict					)      ->    Any:






									'''simple docstring'''

									A    =       cls(**snake_case					)
									processor.add_examples_from_csv(
									    snake_case   ,				split_name=snake_case   ,				column_label=snake_case   ,				column_text=snake_case   ,				column_id=snake_case   ,				skip_first_row=snake_case   ,				overwrite_labels=snake_case   ,				overwrite_examples=snake_case   ,				)
									return processor



		@classmethod
		def        A_		(			cls	:     Optional[int]   ,				snake_case	:     Optional[int]   ,				snake_case	:     int=None   ,				**snake_case	:     Optional[Any]					)      ->    Dict:






									'''simple docstring'''

									A    =       cls(**snake_case					)
									processor.add_examples(snake_case   ,				labels=snake_case					)
									return processor



		def        A_		(			self	:     Any   ,				snake_case	:     Any   ,				snake_case	:     List[Any]=""   ,				snake_case	:     Optional[Any]=0   ,				snake_case	:     int=1   ,				snake_case	:     Optional[int]=None   ,				snake_case	:     int=False   ,				snake_case	:     List[str]=False   ,				snake_case	:     str=False   ,				)      ->    List[Any]:






									'''simple docstring'''

									A    =       self._read_tsv(snake_case					)
									if skip_first_row:
																A    =       lines[1:]
									A    =       []
									A    =       []
									A    =       []
									for i, line in enumerate(snake_case					):
																texts.append(line[column_text]					)
																labels.append(line[column_label]					)
																if column_id is not None:
																							ids.append(line[column_id]					)
																else:
																							A    =       f"""{split_name}-{i}""" if split_name else str(snake_case					)
																							ids.append(snake_case					)

									return self.add_examples(
									    snake_case   ,				snake_case   ,				snake_case   ,				overwrite_labels=snake_case   ,				overwrite_examples=snake_case					)



		def        A_		(			self	:     Any   ,				snake_case	:     Any   ,				snake_case	:     int=None   ,				snake_case	:     int=None   ,				snake_case	:     int=False   ,				snake_case	:     Optional[Any]=False					)      ->    Optional[Any]:






									'''simple docstring'''

									if labels is not None and len(snake_case					) != len(snake_case					):
																raise ValueError(
																    f"""Text and labels have mismatched lengths {len(snake_case					)} and {len(snake_case					)}"""					)
									if ids is not None and len(snake_case					) != len(snake_case					):
																raise ValueError(f"""Text and ids have mismatched lengths {len(snake_case					)} and {len(snake_case					)}"""					)
									if ids is None:
																A    =       [None] * len(snake_case					)
									if labels is None:
																A    =       [None] * len(snake_case					)
									A    =       []
									A    =       set()
									for text_or_text_and_label, label, guid in zip(snake_case   ,				snake_case   ,				snake_case					):
																if isinstance(snake_case   ,				(tuple, list)					) and label is None:
																							A   ,	A    =       text_or_text_and_label
																else:
																							A    =       text_or_text_and_label
																added_labels.add(snake_case					)
																examples.append(InputExample(guid=snake_case   ,				text_a=snake_case   ,				text_b=snake_case   ,				label=snake_case					)					)

									# Update examples
									if overwrite_examples:
																A    =       examples
									else:
																self.examples.extend(snake_case					)

									# Update labels
									if overwrite_labels:
																A    =       list(snake_case					)
									else:
																A    =       list(set(self.labels					).union(snake_case					)					)

									return self.examples



		def        A_		(			self	:     Optional[int]   ,				snake_case	:     List[Any]   ,				snake_case	:     Optional[int]=None   ,				snake_case	:     Union[str, Any]=False   ,				snake_case	:     Any=0   ,				snake_case	:     Optional[Any]=True   ,				snake_case	:     Tuple=None   ,				)      ->    int:






									'''simple docstring'''

									if max_length is None:
																A    =       tokenizer.max_len

									A    =       {label: i for i, label in enumerate(self.labels					)}

									A    =       []
									for ex_index, example in enumerate(self.examples					):
																if ex_index % 10_000 == 0:
																							logger.info(f"""Tokenizing example {ex_index}"""					)

																A    =       tokenizer.encode(
																    example.text_a   ,				add_special_tokens=snake_case   ,				max_length=min(snake_case   ,				tokenizer.max_len					)   ,				)
																all_input_ids.append(snake_case					)

									A    =       max(len(snake_case					) for input_ids in all_input_ids					)

									A    =       []
									for ex_index, (input_ids, example) in enumerate(zip(snake_case   ,				self.examples					)					):
																if ex_index % 10_000 == 0:
																							logger.info(f"""Writing example {ex_index}/{len(self.examples					)}"""					)
																# The mask has 1 for real tokens and 0 for padding tokens. Only real
																# tokens are attended to.
																A    =       [1 if mask_padding_with_zero else 0] * len(snake_case					)

																# Zero-pad up to the sequence length.
																A    =       batch_length - len(snake_case					)
																if pad_on_left:
																							A    =       ([pad_token] * padding_length) + input_ids
																							A    =       ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
																else:
																							A    =       input_ids + ([pad_token] * padding_length)
																							A    =       attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

																if len(snake_case					) != batch_length:
																							raise ValueError(f"""Error with input length {len(snake_case					)} vs {batch_length}"""					)
																if len(snake_case					) != batch_length:
																							raise ValueError(f"""Error with input length {len(snake_case					)} vs {batch_length}"""					)

																if self.mode == "classification":
																							A    =       label_map[example.label]
																elif self.mode == "regression":
																							A    =       float(example.label					)
																else:
																							raise ValueError(self.mode					)

																if ex_index < 5 and self.verbose:
																							logger.info('*** Example ***'					)
																							logger.info(f"""guid: {example.guid}"""					)
																							logger.info(f"""input_ids: {' '.join([str(snake_case					) for x in input_ids]					)}"""					)
																							logger.info(f"""attention_mask: {' '.join([str(snake_case					) for x in attention_mask]					)}"""					)
																							logger.info(f"""label: {example.label} (id = {label})"""					)

																features.append(InputFeatures(input_ids=snake_case   ,				attention_mask=snake_case   ,				label=snake_case					)					)

									if return_tensors is None:
																return features
									elif return_tensors == "tf":
																if not is_tf_available():
																							raise RuntimeError('return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported'					)
																import tensorflow as tf

																def gen():
																							for ex in features:
																														yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

																A    =       tf.data.Dataset.from_generator(
																    snake_case   ,				({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa)   ,				({'input_ids': tf.TensorShape([None]					), 'attention_mask': tf.TensorShape([None]					)}, tf.TensorShape([]					))   ,				)
																return dataset
									elif return_tensors == "pt":
																if not is_torch_available():
																							raise RuntimeError('return_tensors set to \'pt\' but PyTorch can\'t be imported'					)
																import torch
																from torch.utils.data import TensorDataset

																A    =       torch.tensor([f.input_ids for f in features]   ,				dtype=torch.long					)
																A    =       torch.tensor([f.attention_mask for f in features]   ,				dtype=torch.long					)
																if self.mode == "classification":
																							A    =       torch.tensor([f.label for f in features]   ,				dtype=torch.long					)
																elif self.mode == "regression":
																							A    =       torch.tensor([f.label for f in features]   ,				dtype=torch.float					)

																A    =       TensorDataset(snake_case   ,				snake_case   ,				snake_case					)
																return dataset
									else:
																raise ValueError('return_tensors should be one of \'tf\' or \'pt\''					)








"""simple docstring"""







import os

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


A     =      logging.get_logger(__name__)






class   UpperCAmelCase__		(   UpperCamelCase  ):



		def __init__(			self	:     List[str]   ,				snake_case	:     List[Any]   ,				snake_case	:     str=None					)      ->    Tuple:






									'''simple docstring'''

									A    =       language
									A    =       train_language



		def        A_		(			self	:     Optional[int]   ,				snake_case	:     Any					)      ->    Union[str, Any]:






									'''simple docstring'''

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



		def        A_		(			self	:     int   ,				snake_case	:     int					)      ->    Optional[int]:






									'''simple docstring'''

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



		def        A_		(			self	:     Any					)      ->    List[Any]:






									'''simple docstring'''

									return ["contradiction", "entailment", "neutral"]



A     =      {
    'xnli': XnliProcessor,
}

A     =      {
    'xnli': 'classification',
}

A     =      {
    'xnli': 3,
}



