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


lowerCAmelCase__      		=					logging.get_logger(__name__)
@dataclass
class 							a_					:






 '''simple docstring'''






 UpperCAmelCase_      					=   42
 UpperCAmelCase_      					=   42
 UpperCAmelCase_      					=   None
 UpperCAmelCase_      					=   None




 def 						__snake_case		(     self   :    Any):







        '''simple docstring'''



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






@dataclass(frozen=SCREAMING_SNAKE_CASE		)
class 							a_					:






 '''simple docstring'''






 UpperCAmelCase_      					=   42
 UpperCAmelCase_      					=   None
 UpperCAmelCase_      					=   None
 UpperCAmelCase_      					=   None




 def 						__snake_case		(     self   :    Dict):







        '''simple docstring'''



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






class 							a_					:






 '''simple docstring'''






 def 						__snake_case		(     self   :    List[str]     ,       lowercase__   :    Tuple):







        '''simple docstring'''



        raise NotImplementedError()




 def 						__snake_case		(     self   :    Union[str, Any]     ,       lowercase__   :    Dict):







        '''simple docstring'''



        raise NotImplementedError()




 def 						__snake_case		(     self   :    Dict     ,       lowercase__   :    List[Any]):







        '''simple docstring'''



        raise NotImplementedError()




 def 						__snake_case		(     self   :    Dict     ,       lowercase__   :    Optional[int]):







        '''simple docstring'''



        raise NotImplementedError()




 def 						__snake_case		(     self   :    Union[str, Any]):







        '''simple docstring'''



        raise NotImplementedError()




 def 						__snake_case		(     self   :    Dict     ,       lowercase__   :    Any):







        '''simple docstring'''



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




 @classmethod
 def 						__snake_case		(     cls   :    Tuple     ,       lowercase__   :    Optional[int]     ,       lowercase__   :    List[Any]=None):







        '''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 							a_					(		SCREAMING_SNAKE_CASE		):






 '''simple docstring'''






 def __init__(     self   :    Optional[int]     ,       lowercase__   :    str=None     ,       lowercase__   :    Optional[int]=None     ,       lowercase__   :    Union[str, Any]="classification"     ,       lowercase__   :    List[Any]=False):







        '''simple docstring'''



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




 def __len__(     self   :    List[Any]):







        '''simple docstring'''



        return len(self.examples)




 def __getitem__(     self   :    List[Any]     ,       lowercase__   :    str):







        '''simple docstring'''



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




 @classmethod
 def 						__snake_case		(     cls   :    Optional[int]     ,       lowercase__   :    Optional[Any]     ,       lowercase__   :    Union[str, Any]=""     ,       lowercase__   :    str=0     ,       lowercase__   :    Tuple=1     ,       lowercase__   :    List[str]=None     ,       lowercase__   :    List[Any]=False     ,       **lowercase__   :    str):







        '''simple docstring'''



        lowerCAmelCase__	      =   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 						__snake_case		(     cls   :    Dict     ,       lowercase__   :    str     ,       lowercase__   :    Tuple=None     ,       **lowercase__   :    int):







        '''simple docstring'''



        lowerCAmelCase__	      =   cls(**snake_case__)
        processor.add_examples(snake_case__     ,       labels=snake_case__)
        return processor




 def 						__snake_case		(     self   :    List[str]     ,       lowercase__   :    Union[str, Any]     ,       lowercase__   :    Optional[Any]=""     ,       lowercase__   :    int=0     ,       lowercase__   :    Union[str, Any]=1     ,       lowercase__   :    Tuple=None     ,       lowercase__   :    Tuple=False     ,       lowercase__   :    List[str]=False     ,       lowercase__   :    Tuple=False     ,       ):







        '''simple docstring'''



        lowerCAmelCase__	      =   self._read_tsv(snake_case__)
        if skip_first_row:
               lowerCAmelCase__	      =   lines[1:]
        lowerCAmelCase__	      =   []
        lowerCAmelCase__	      =   []
        lowerCAmelCase__	      =   []
        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:
                      lowerCAmelCase__	      =   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 						__snake_case		(     self   :    Optional[int]     ,       lowercase__   :    Dict     ,       lowercase__   :    List[Any]=None     ,       lowercase__   :    Optional[Any]=None     ,       lowercase__   :    List[str]=False     ,       lowercase__   :    Tuple=False):







        '''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:
               lowerCAmelCase__	      =   [None] * len(snake_case__)
        if labels is None:
               lowerCAmelCase__	      =   [None] * len(snake_case__)
        lowerCAmelCase__	      =   []
        lowerCAmelCase__	      =   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:
                      lowerCAmelCase__	      =   text_or_text_and_label
               else:
                      lowerCAmelCase__	      =   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:
               lowerCAmelCase__	      =   examples
        else:
               self.examples.extend(snake_case__)

        # Update labels
        if overwrite_labels:
               lowerCAmelCase__	      =   list(snake_case__)
        else:
               lowerCAmelCase__	      =   list(set(self.labels).union(snake_case__))

        return self.examples




 def 						__snake_case		(     self   :    str     ,       lowercase__   :    Optional[int]     ,       lowercase__   :    Union[str, Any]=None     ,       lowercase__   :    Tuple=False     ,       lowercase__   :    Dict=0     ,       lowercase__   :    List[str]=True     ,       lowercase__   :    Tuple=None     ,       ):







        '''simple docstring'''



        if max_length is None:
               lowerCAmelCase__	      =   tokenizer.max_len

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

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

               lowerCAmelCase__	      =   tokenizer.encode(
                   example.text_a     ,       add_special_tokens=snake_case__     ,       max_length=min(snake_case__     ,       tokenizer.max_len)     ,       )
               all_input_ids.append(snake_case__)

        lowerCAmelCase__	      =   max(len(snake_case__) for input_ids in all_input_ids)

        lowerCAmelCase__	      =   []
        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.
               lowerCAmelCase__	      =   [1 if mask_padding_with_zero else 0] * len(snake_case__)

               # Zero-pad up to the sequence length.
               lowerCAmelCase__	      =   batch_length - len(snake_case__)
               if pad_on_left:
                      lowerCAmelCase__	      =   ([pad_token] * padding_length) + input_ids
                      lowerCAmelCase__	      =   ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
               else:
                      lowerCAmelCase__	      =   input_ids + ([pad_token] * padding_length)
                      lowerCAmelCase__	      =   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":
                      lowerCAmelCase__	      =   label_map[example.label]
               elif self.mode == "regression":
                      lowerCAmelCase__	      =   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)

               lowerCAmelCase__	      =   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

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

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




'''simple docstring'''




from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available

def       __UpperCamelCase     (     _A     :					List[str]					)     ->   Any:




	"""simple docstring"""





	if is_torch_available():
		import torch

		if isinstance(_A       , torch.Tensor					):
			return True

	return isinstance(_A       , np.ndarray					)






class 			lowerCAmelCase					(					a				):



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     List[str]   				=			fields(self     )

		# Safety and consistency checks
		if not len(snake_case__     ):
			raise ValueError(f"{self.__class__.__name__} has no fields."     )

		lowerCAmelCase       :     List[str]   				=			getattr(self			,       class_fields[0].name     )
		lowerCAmelCase       :     List[Any]   				=			all(getattr(self			,       field.name     ) is None for field in class_fields[1:]     )

		if other_fields_are_none and isinstance(snake_case__			,       snake_case__     ):
			for key, value in first_field.items():
				lowerCAmelCase       :     Optional[int]   				=			value
		else:
			for field in class_fields:
				lowerCAmelCase       :     str   				=			getattr(self			,       field.name     )
				if v is not None:
					lowerCAmelCase       :     Optional[Any]   				=			v



	def __delitem__(       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."     )



	def 					lowercase  (       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``setdefault`` on a {self.__class__.__name__} instance."     )



	def 					lowercase  (       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``pop`` on a {self.__class__.__name__} instance."     )



	def 					lowercase  (       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``update`` on a {self.__class__.__name__} instance."     )



	def __getitem__(       self			,       snake_case__     ):
		if isinstance(snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     Dict   				=			dict(self.items()     )
			return inner_dict[k]
		else:
			return self.to_tuple()[k]



	def __setattr__(       self			,       snake_case__			,       snake_case__     ):
		if name in self.keys() and value is not None:
			# Don't call self.__setitem__ to avoid recursion errors
			super().__setitem__(snake_case__			,       snake_case__     )
		super().__setattr__(snake_case__			,       snake_case__     )



	def __setitem__(       self			,       snake_case__			,       snake_case__     ):
		# Will raise a KeyException if needed
		super().__setitem__(snake_case__			,       snake_case__     )
		# Don't call self.__setattr__ to avoid recursion errors
		super().__setattr__(snake_case__			,       snake_case__     )



	def 					lowercase  (       self     ):
		return tuple(self[k] for k in self.keys()     )
