





"""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__    :   Dict    						= logging.get_logger(__name__)



@dataclass
class    lowercase_ :
       snake_case__ :							str
       snake_case__ :							str
       snake_case__ :							Optional[str] 							=   None
       snake_case__ :							Optional[str] 							=   None

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

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



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

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

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



class    lowercase_ :

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        raise NotImplementedError()

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        raise NotImplementedError()

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        raise NotImplementedError()

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        raise NotImplementedError()

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        raise NotImplementedError()

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

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

       @classmethod
       def 						_SCREAMING_SNAKE_CASE(	cls      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=None   ):






        '''simple docstring'''

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



class    lowercase_ (   lowerCAmelCase_							):

       def __init__(	self      ,    __SCREAMING_SNAKE_CASE=None      ,    __SCREAMING_SNAKE_CASE=None      ,    __SCREAMING_SNAKE_CASE="classification"      ,    __SCREAMING_SNAKE_CASE=False   ):






        '''simple docstring'''

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

       def __len__(	self   ):






        '''simple docstring'''

        return len(self.examples   )

       def __getitem__(	self      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

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

       @classmethod
       def 						_SCREAMING_SNAKE_CASE(	cls      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=""      ,    __SCREAMING_SNAKE_CASE=0      ,    __SCREAMING_SNAKE_CASE=1      ,    __SCREAMING_SNAKE_CASE=None      ,    __SCREAMING_SNAKE_CASE=False      ,    **__SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

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

       @classmethod
       def 						_SCREAMING_SNAKE_CASE(	cls      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=None      ,    **__SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

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

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=""      ,    __SCREAMING_SNAKE_CASE=0      ,    __SCREAMING_SNAKE_CASE=1      ,    __SCREAMING_SNAKE_CASE=None      ,    __SCREAMING_SNAKE_CASE=False      ,    __SCREAMING_SNAKE_CASE=False      ,    __SCREAMING_SNAKE_CASE=False      ,    ):






        '''simple docstring'''

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

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

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=None      ,    __SCREAMING_SNAKE_CASE=None      ,    __SCREAMING_SNAKE_CASE=False      ,    __SCREAMING_SNAKE_CASE=False   ):






        '''simple docstring'''

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

        # Update examples
        if overwrite_examples:
         snake_case__										= examples
        else:
         self.examples.extend(__SCREAMING_SNAKE_CASE   )

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

        return self.examples

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=None      ,    __SCREAMING_SNAKE_CASE=False      ,    __SCREAMING_SNAKE_CASE=0      ,    __SCREAMING_SNAKE_CASE=True      ,    __SCREAMING_SNAKE_CASE=None      ,    ):






        '''simple docstring'''

        if max_length is None:
         snake_case__										= tokenizer.max_len

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

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

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

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

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

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

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

         if self.mode == "classification":
          snake_case__										= label_map[example.label]
         elif self.mode == "regression":
          snake_case__										= 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(__SCREAMING_SNAKE_CASE   ) for x in input_ids]   )}"   )
          logger.info(F"attention_mask: {' '.join([str(__SCREAMING_SNAKE_CASE   ) for x in attention_mask]   )}"   )
          logger.info(F"label: {example.label} (id = {label})"   )

         features.append(InputFeatures(input_ids=__SCREAMING_SNAKE_CASE      ,    attention_mask=__SCREAMING_SNAKE_CASE      ,    label=__SCREAMING_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)

         snake_case__										= tf.data.Dataset.from_generator(
             __SCREAMING_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

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

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








'''simple docstring'''







from collections import namedtuple

_a					: Any      =    namedtuple("""from_to""", """from_ to""")

_a					: Dict      =    {
    """millimeter""": """mm""",
    """centimeter""": """cm""",
    """meter""": """m""",
    """kilometer""": """km""",
    """inch""": """in""",
    """inche""": """in""",  # Trailing 's' has been stripped off
    """feet""": """ft""",
    """foot""": """ft""",
    """yard""": """yd""",
    """mile""": """mi""",
}

_a					: Optional[Any]      =    {
    """mm""": from_to(0.001, 1_0_0_0),
    """cm""": from_to(0.01, 1_0_0),
    """m""": from_to(1, 1),
    """km""": from_to(1_0_0_0, 0.001),
    """in""": from_to(0.0254, 39.3701),
    """ft""": from_to(0.3048, 3.28084),
    """yd""": from_to(0.9144, 1.09361),
    """mi""": from_to(1609.34, 0.000621371),
}





def    _lowerCAmelCase						(						lowercase					,	lowercase					,	lowercase	)					->					float:
			__lowerCAmelCase											=				from_type.lower().rstrip("""s"""	)
			__lowerCAmelCase											=				TYPE_CONVERSION.get(lowercase					,	lowercase	)
			__lowerCAmelCase											=				to_type.lower().rstrip("""s"""	)
			__lowerCAmelCase											=				TYPE_CONVERSION.get(lowercase					,	lowercase	)
			if new_from not in METRIC_CONVERSION:
						__lowerCAmelCase											=				(
						    f'Invalid \'from_type\' value: {from_type!r}.\n'
						    f'Conversion abbreviations are: {", ".join(lowercase	)}'
						)
						raise ValueError(lowercase	)
			if new_to not in METRIC_CONVERSION:
						__lowerCAmelCase											=				(
						    f'Invalid \'to_type\' value: {to_type!r}.\n'
						    f'Conversion abbreviations are: {", ".join(lowercase	)}'
						)
						raise ValueError(lowercase	)
			return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to


if __name__ == "__main__":
	import doctest

	doctest.testmod()

