






"""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


_lowerCAmelCase  :  Optional[Any]   =			logging.get_logger(__name__)




@dataclass
class  lowerCAmelCase__ :
   SCREAMING_SNAKE_CASE_         =42
   SCREAMING_SNAKE_CASE_         =42
   SCREAMING_SNAKE_CASE_         =None
   SCREAMING_SNAKE_CASE_         =None
   def 			__a       (		self			:  Union[str, Any]       ):


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




@dataclass(frozen=__magic_name__      )
class  lowerCAmelCase__ :
   SCREAMING_SNAKE_CASE_         =42
   SCREAMING_SNAKE_CASE_         =None
   SCREAMING_SNAKE_CASE_         =None
   SCREAMING_SNAKE_CASE_         =None
   def 			__a       (		self			:  Optional[Any]       ):


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




class  lowerCAmelCase__ :
   def 			__a       (		self			:  Tuple       , snake_case__			:  Union[str, Any]       ):


        '''simple docstring'''
        raise NotImplementedError()
   def 			__a       (		self			:  Optional[int]       , snake_case__			:  str       ):


        '''simple docstring'''
        raise NotImplementedError()
   def 			__a       (		self			:  Any       , snake_case__			:  Dict       ):


        '''simple docstring'''
        raise NotImplementedError()
   def 			__a       (		self			:  Optional[int]       , snake_case__			:  Optional[Any]       ):


        '''simple docstring'''
        raise NotImplementedError()
   def 			__a       (		self			:  Tuple       ):


        '''simple docstring'''
        raise NotImplementedError()
   def 			__a       (		self			:  str       , snake_case__			:  int       ):


        '''simple docstring'''
        if len(self.get_labels()       ) > 1:
             UpperCAmelCase__	:						Any       =       self.get_labels()[int(example.label       )]
        return example




   @classmethod
   def 			__a       (		cls			:  int       , snake_case__			:  Dict       , snake_case__			:  Dict=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  lowerCAmelCase__ (						__magic_name__      ):
   def __init__(		self			:  int       , snake_case__			:  Union[str, Any]=None       , snake_case__			:  List[str]=None       , snake_case__			:  Dict="classification"       , snake_case__			:  Optional[Any]=False       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Any       =       [] if labels is None else labels
        UpperCAmelCase__	:						Tuple       =       [] if examples is None else examples
        UpperCAmelCase__	:						Dict       =       mode
        UpperCAmelCase__	:						List[str]       =       verbose
   def __len__(		self			:  Optional[Any]       ):


        '''simple docstring'''
        return len(self.examples       )
   def __getitem__(		self			:  Optional[int]       , snake_case__			:  Optional[Any]       ):


        '''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			:  List[str]       , snake_case__			:  Any       , snake_case__			:  int=""       , snake_case__			:  Tuple=0       , snake_case__			:  List[Any]=1       , snake_case__			:  List[Any]=None       , snake_case__			:  Union[str, Any]=False       , **snake_case__			:  Tuple       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Tuple       =       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			:  Tuple       , snake_case__			:  int       , snake_case__			:  List[Any]=None       , **snake_case__			:  Tuple       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[Any]       =       cls(**snake_case__       )
        processor.add_examples(snake_case__       , labels=snake_case__       )
        return processor
   def 			__a       (		self			:  List[str]       , snake_case__			:  List[Any]       , snake_case__			:  Union[str, Any]=""       , snake_case__			:  Optional[int]=0       , snake_case__			:  Tuple=1       , snake_case__			:  str=None       , snake_case__			:  List[str]=False       , snake_case__			:  Dict=False       , snake_case__			:  List[Any]=False       , ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       self._read_tsv(snake_case__       )
        if skip_first_row:
             UpperCAmelCase__	:						Dict       =       lines[1:]
        UpperCAmelCase__	:						List[str]       =       []
        UpperCAmelCase__	:						List[Any]       =       []
        UpperCAmelCase__	:						Any       =       []
        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:
                  UpperCAmelCase__	:						List[Any]       =       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__			:  str       , snake_case__			:  Dict=None       , snake_case__			:  List[Any]=None       , snake_case__			:  List[str]=False       , snake_case__			:  Optional[Any]=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:
             UpperCAmelCase__	:						Optional[int]       =       [None] * len(snake_case__       )
        if labels is None:
             UpperCAmelCase__	:						Tuple       =       [None] * len(snake_case__       )
        UpperCAmelCase__	:						Union[str, Any]       =       []
        UpperCAmelCase__	:						List[Any]       =       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:
                  UpperCAmelCase__       ,							UpperCAmelCase__	:						Tuple       =       text_or_text_and_label
             else:
                  UpperCAmelCase__	:						int       =       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:
             UpperCAmelCase__	:						Optional[int]       =       examples
        else:
             self.examples.extend(snake_case__       )

        # Update labels
        if overwrite_labels:
             UpperCAmelCase__	:						Dict       =       list(snake_case__       )
        else:
             UpperCAmelCase__	:						List[Any]       =       list(set(self.labels       ).union(snake_case__       )       )

        return self.examples




   def 			__a       (		self			:  Tuple       , snake_case__			:  int       , snake_case__			:  List[Any]=None       , snake_case__			:  List[Any]=False       , snake_case__			:  Optional[Any]=0       , snake_case__			:  Optional[int]=True       , snake_case__			:  str=None       , ):


        '''simple docstring'''
        if max_length is None:
             UpperCAmelCase__	:						str       =       tokenizer.max_len

        UpperCAmelCase__	:						List[Any]       =       {label: i for i, label in enumerate(self.labels       )}

        UpperCAmelCase__	:						Dict       =       []
        for ex_index, example in enumerate(self.examples       ):
             if ex_index % 1_0_0_0_0 == 0:
                  logger.info(f'Tokenizing example {ex_index}'       )

             UpperCAmelCase__	:						List[Any]       =       tokenizer.encode(
                 example.text_a       , add_special_tokens=snake_case__       , max_length=min(snake_case__       , tokenizer.max_len       )       , )
             all_input_ids.append(snake_case__       )

        UpperCAmelCase__	:						Optional[int]       =       max(len(snake_case__       ) for input_ids in all_input_ids       )

        UpperCAmelCase__	:						List[Any]       =       []
        for ex_index, (input_ids, example) in enumerate(zip(snake_case__       , self.examples       )       ):
             if ex_index % 1_0_0_0_0 == 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.
             UpperCAmelCase__	:						Optional[int]       =       [1 if mask_padding_with_zero else 0] * len(snake_case__       )

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

             UpperCAmelCase__	:						Optional[Any]       =       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

             UpperCAmelCase__	:						Union[str, Any]       =       torch.tensor([f.input_ids for f in features]       , dtype=torch.long       )
             UpperCAmelCase__	:						List[Any]       =       torch.tensor([f.attention_mask for f in features]       , dtype=torch.long       )
             if self.mode == "classification":
                  UpperCAmelCase__	:						Dict       =       torch.tensor([f.label for f in features]       , dtype=torch.long       )
             elif self.mode == "regression":
                  UpperCAmelCase__	:						List[Any]       =       torch.tensor([f.label for f in features]       , dtype=torch.float       )

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










"""simple docstring"""



# Lint as: python3

import hashlib
from typing import Union


def   SCREAMING_SNAKE_CASE__					(     snake_case					:   Union[str, int, bytes]							)->		bytes:






     '''simple docstring'''



     if isinstance(snake_case ,				snake_case							):
          # Data already in bytes, returns as it as
          return hash_data
     elif isinstance(snake_case ,				snake_case							):
          # We keep the data as it as for it ot be later encoded to UTF-8
          # However replace `\\` with `/` for Windows compatibility
          UpperCAmelCase__	:						Union[str, Any]       =       hash_data.replace("\\" ,				"/"							)
     elif isinstance(snake_case ,				snake_case							):
          UpperCAmelCase__	:						int       =       str(snake_case							)
     else:
          # If data is not of the required type, raise error
          raise InvalidKeyError(snake_case							)

     return hash_data.encode("utf-8"							)




class  lowerCAmelCase__ (						__magic_name__      ):
   def __init__(		self			:  List[str]       , snake_case__			:  Union[str, Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Tuple       =       "\nFAILURE TO GENERATE DATASET: Invalid key type detected"
        UpperCAmelCase__	:						str       =       f'\nFound Key {hash_data} of type {type(snake_case__       )}'
        UpperCAmelCase__	:						Union[str, Any]       =       "\nKeys should be either str, int or bytes type"
        super().__init__(f'{self.prefix}{self.err_msg}{self.suffix}'       )




class  lowerCAmelCase__ (						__magic_name__      ):
   def __init__(		self			:  List[str]       , snake_case__			:  Dict       , snake_case__			:  Optional[int]       , snake_case__			:  List[str]=""       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       key
        UpperCAmelCase__	:						Optional[Any]       =       duplicate_key_indices
        UpperCAmelCase__	:						str       =       fix_msg
        UpperCAmelCase__	:						Any       =       "Found multiple examples generated with the same key"
        if len(snake_case__       ) <= 2_0:
             UpperCAmelCase__	:						int       =       f'\nThe examples at index {", ".join(snake_case__       )} have the key {key}'
        else:
             UpperCAmelCase__	:						str       =       f'\nThe examples at index {", ".join(duplicate_key_indices[:2_0]       )}... ({len(snake_case__       ) - 2_0} more) have the key {key}'
        UpperCAmelCase__	:						Any       =       "\n" + fix_msg if fix_msg else ""
        super().__init__(f'{self.prefix}{self.err_msg}{self.suffix}'       )




class  lowerCAmelCase__ :
   def __init__(		self			:  Dict       , snake_case__			:  str       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       hashlib.mda(_as_bytes(snake_case__       )       )




   def 			__a       (		self			:  int       , snake_case__			:  Union[str, int, bytes]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						int       =       self._split_mda.copy()
        UpperCAmelCase__	:						List[str]       =       _as_bytes(snake_case__       )
        mda.update(snake_case__       )
        # Convert to integer with hexadecimal conversion
        return int(mda.hexdigest()       , 1_6       )


