




"""simple docstring"""







import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


__A   :  Any					=      logging.getLogger(__name__)




@dataclass(frozen=lowerCAmelCase_    )
class       lowerCAmelCase__       :


				"""simple docstring"""





				__UpperCAmelCase :     str
				__UpperCAmelCase :     str
				__UpperCAmelCase :     Optional[str]       =      None
				__UpperCAmelCase :     Optional[str]       =      None
				__UpperCAmelCase :     Optional[str]       =      None





@dataclass(frozen=lowerCAmelCase_    )
class       lowerCAmelCase__       :


				"""simple docstring"""





				__UpperCAmelCase :     List[int]
				__UpperCAmelCase :     Optional[List[int]]       =      None
				__UpperCAmelCase :     Optional[List[int]]       =      None
				__UpperCAmelCase :     Optional[Union[int, float]]       =      None
				__UpperCAmelCase :     Optional[int]       =      None


if is_torch_available():
						import torch
						from torch.utils.data import Dataset




						class       lowerCAmelCase__       (			lowerCAmelCase_    ):


										"""simple docstring"""





										__UpperCAmelCase :     List[InputFeatures]



										def __init__(     self					:	Dict    ,   lowercase__					:	str    ,   lowercase__					:	PreTrainedTokenizer    ,   lowercase__					:	str    ,   lowercase__					:	Optional[int] = None    ,   lowercase__					:	Union[str, Any]=False    ,   lowercase__					:	bool = False    ,   ):
												__lowercase				:		Optional[int]												=				hans_processors[task]()

												__lowercase				:		int												=				os.path.join(
												    lowercase__    ,   "cached_{}_{}_{}_{}".format(
												        "dev" if evaluate else "train"    ,   tokenizer.__class__.__name__    ,   str(lowercase__						)    ,   lowercase__    ,   )    ,   )
												__lowercase				:		Tuple												=				processor.get_labels()
												if tokenizer.__class__ in (
												    RobertaTokenizer,
												    RobertaTokenizerFast,
												    XLMRobertaTokenizer,
												    BartTokenizer,
												    BartTokenizerFast,
												):
														# HACK(label indices are swapped in RoBERTa pretrained model)
														__lowercase						,__lowercase				:		Optional[Any]												=				label_list[2], label_list[1]
												__lowercase				:		Union[str, Any]												=				label_list

												# Make sure only the first process in distributed training processes the dataset,
												# and the others will use the cache.
												__lowercase				:		Optional[Any]												=				cached_features_file + ".lock"
												with FileLock(lowercase__						):
														if os.path.exists(lowercase__						) and not overwrite_cache:
																logger.info(f'Loading features from cached file {cached_features_file}'						)
																__lowercase				:		List[Any]												=				torch.load(lowercase__						)
														else:
																logger.info(f'Creating features from dataset file at {data_dir}'						)

																__lowercase				:		Dict												=				(
																    processor.get_dev_examples(lowercase__						) if evaluate else processor.get_train_examples(lowercase__						)
																)

																logger.info("Training examples: %s"    ,   len(lowercase__						)						)
																__lowercase				:		List[str]												=				hans_convert_examples_to_features(lowercase__    ,   lowercase__    ,   lowercase__    ,   lowercase__						)
																logger.info("Saving features into cached file %s"    ,   lowercase__						)
																torch.save(self.features    ,   lowercase__						)



										def __len__(     self					:	Union[str, Any]						):
												return len(self.features						)



										def __getitem__(     self					:	List[str]    ,   lowercase__					:	List[str]						):
												return self.features[i]



										def       snake_case    (     self					:	List[Any]						):
												return self.label_list


if is_tf_available():
						import tensorflow as tf




						class       lowerCAmelCase__       :


										"""simple docstring"""





										__UpperCAmelCase :     List[InputFeatures]



										def __init__(     self					:	Dict    ,   lowercase__					:	str    ,   lowercase__					:	PreTrainedTokenizer    ,   lowercase__					:	str    ,   lowercase__					:	Optional[int] = 1_2_8    ,   lowercase__					:	Any=False    ,   lowercase__					:	bool = False    ,   ):
												__lowercase				:		Optional[int]												=				hans_processors[task]()
												__lowercase				:		Union[str, Any]												=				processor.get_labels()
												if tokenizer.__class__ in (
												    RobertaTokenizer,
												    RobertaTokenizerFast,
												    XLMRobertaTokenizer,
												    BartTokenizer,
												    BartTokenizerFast,
												):
														# HACK(label indices are swapped in RoBERTa pretrained model)
														__lowercase						,__lowercase				:		int												=				label_list[2], label_list[1]
												__lowercase				:		Optional[int]												=				label_list

												__lowercase				:		Any												=				processor.get_dev_examples(lowercase__						) if evaluate else processor.get_train_examples(lowercase__						)
												__lowercase				:		Optional[int]												=				hans_convert_examples_to_features(lowercase__    ,   lowercase__    ,   lowercase__    ,   lowercase__						)

												def gen():
														for ex_index, ex in tqdm.tqdm(enumerate(self.features						)    ,   desc="convert examples to features"						):
																if ex_index % 1_0_0_0_0 == 0:
																		logger.info("Writing example %d of %d" % (ex_index, len(lowercase__						))						)

																yield (
																    {
																        "example_id": 0,
																        "input_ids": ex.input_ids,
																        "attention_mask": ex.attention_mask,
																        "token_type_ids": ex.token_type_ids,
																    },
																    ex.label,
																)

												__lowercase				:		List[Any]												=				tf.data.Dataset.from_generator(
												    lowercase__    ,   (
												        {
												            "example_id": tf.intaa,
												            "input_ids": tf.intaa,
												            "attention_mask": tf.intaa,
												            "token_type_ids": tf.intaa,
												        },
												        tf.intaa,
												    )    ,   (
												        {
												            "example_id": tf.TensorShape([]						),
												            "input_ids": tf.TensorShape([None, None]						),
												            "attention_mask": tf.TensorShape([None, None]						),
												            "token_type_ids": tf.TensorShape([None, None]						),
												        },
												        tf.TensorShape([]						),
												    )    ,   )



										def       snake_case    (     self					:	Optional[Any]						):
												return self.dataset



										def __len__(     self					:	Tuple						):
												return len(self.features						)



										def __getitem__(     self					:	str    ,   lowercase__					:	int						):
												return self.features[i]



										def       snake_case    (     self					:	Tuple						):
												return self.label_list




class       lowerCAmelCase__       (			lowerCAmelCase_    ):


				"""simple docstring"""





				def       snake_case    (     self					:	List[Any]    ,   lowercase__					:	Optional[int]						):
						return self._create_examples(self._read_tsv(os.path.join(lowercase__    ,   "heuristics_train_set.txt"						)						)    ,   "train"						)



				def       snake_case    (     self					:	List[Any]    ,   lowercase__					:	List[Any]						):
						return self._create_examples(self._read_tsv(os.path.join(lowercase__    ,   "heuristics_evaluation_set.txt"						)						)    ,   "dev"						)



				def       snake_case    (     self					:	List[Any]						):
						return ["contradiction", "entailment", "neutral"]



				def       snake_case    (     self					:	Dict    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Union[str, Any]						):
						__lowercase				:		Union[str, Any]												=				[]
						for i, line in enumerate(lowercase__						):
								if i == 0:
										continue
								__lowercase				:		Dict												=				"%s-%s" % (set_type, line[0])
								__lowercase				:		List[Any]												=				line[5]
								__lowercase				:		Dict												=				line[6]
								__lowercase				:		int												=				line[7][2:] if line[7].startswith("ex"						) else line[7]
								__lowercase				:		List[Any]												=				line[0]
								examples.append(InputExample(guid=lowercase__    ,   text_a=lowercase__    ,   text_b=lowercase__    ,   label=lowercase__    ,   pairID=lowercase__						)						)
						return examples





def 						snake_case__       (					_lowerCamelCase,  _lowerCamelCase,  _lowerCamelCase,  _lowerCamelCase,  )			->Tuple:






		"""simple docstring"""


		__lowercase				:		List[Any]												=				{label: i for i, label in enumerate(_lowerCamelCase				)}

		__lowercase				:		Dict												=				[]
		for ex_index, example in tqdm.tqdm(enumerate(_lowerCamelCase				),  desc="convert examples to features"				):
				if ex_index % 1_00_00 == 0:
						logger.info("Writing example %d" % (ex_index)				)

				__lowercase				:		Optional[int]												=				tokenizer(
				    example.text_a,  example.text_b,  add_special_tokens=_lowerCamelCase,  max_length=_lowerCamelCase,  padding="max_length",  truncation=_lowerCamelCase,  return_overflowing_tokens=_lowerCamelCase,  )

				__lowercase				:		Optional[int]												=				label_map[example.label] if example.label in label_map else 0

				__lowercase				:		Any												=				int(example.pairID				)

				features.append(InputFeatures(**_lowerCamelCase,  label=_lowerCamelCase,  pairID=_lowerCamelCase				)				)

		for i, example in enumerate(examples[:5]				):
				logger.info("*** Example ***"				)
				logger.info(F'guid: {example}'				)
				logger.info(F'features: {features[i]}'				)

		return features


__A   :  int					=      {
    'hans': 3,
}





__A   :  List[str]					=      {
    'hans': HansProcessor,
}






"""simple docstring"""







def 						snake_case__       (					_lowerCamelCase				)			->list:






		"""simple docstring"""


		__lowercase				:		Optional[int]												=				[0] * len(_lowerCamelCase				)

		for i in range(1,  len(_lowerCamelCase				)				):
				# use last results for better performance - dynamic programming
				__lowercase				:		Optional[Any]												=				prefix_result[i - 1]
				while j > 0 and input_string[i] != input_string[j]:
						__lowercase				:		List[str]												=				prefix_result[j - 1]

				if input_string[i] == input_string[j]:
						j += 1
				__lowercase				:		Any												=				j

		return prefix_result

def 						snake_case__       (					_lowerCamelCase				)			->int:






		"""simple docstring"""


		return max(prefix_function(_lowerCamelCase				)				)


if __name__ == "__main__":
						import doctest

						doctest.testmod()
