



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,
)


lowerCAmelCase__						:	List[str]					=logging.getLogger(__name__)

@dataclass(frozen=lowerCAmelCase__			)
class   UpperCAmelCase_    :

						'''simple docstring'''




						UpperCamelCase__     :					str
						UpperCamelCase__     :					str
						UpperCamelCase__     :					Optional[str]							    =    None
						UpperCamelCase__     :					Optional[str]							    =    None
						UpperCamelCase__     :					Optional[str]							    =    None







@dataclass(frozen=lowerCAmelCase__			)
class   UpperCAmelCase_    :

						'''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   UpperCAmelCase_    (  lowerCAmelCase__			):

									'''simple docstring'''




									UpperCamelCase__     :					List[InputFeatures]






									def __init__(       self ,				_A ,				_A ,				_A ,				_A = None ,				_A=False ,				_A = False ,				):







																'''simple docstring'''

																__SCREAMING_SNAKE_CASE										=   hans_processors[task]()

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

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

																														__SCREAMING_SNAKE_CASE										=   (
																														    processor.get_dev_examples(__a						) if evaluate else processor.get_train_examples(__a						)
																														)

																														logger.info('Training examples: %s' ,				len(__a						)						)
																														__SCREAMING_SNAKE_CASE										=   hans_convert_examples_to_features(__a ,				__a ,				__a ,				__a						)
																														logger.info('Saving features into cached file %s' ,				__a						)
																														torch.save(self.features ,				__a						)






									def __len__(       self						):







																'''simple docstring'''

																return len(self.features						)






									def __getitem__(       self ,				_A						):







																'''simple docstring'''

																return self.features[i]






									def 			_A      (       self						):







																'''simple docstring'''

																return self.label_list


if is_tf_available():
			import tensorflow as tf

			class   UpperCAmelCase_    :

									'''simple docstring'''




									UpperCamelCase__     :					List[InputFeatures]






									def __init__(       self ,				_A ,				_A ,				_A ,				_A = 128 ,				_A=False ,				_A = False ,				):







																'''simple docstring'''

																__SCREAMING_SNAKE_CASE										=   hans_processors[task]()
																__SCREAMING_SNAKE_CASE										=   processor.get_labels()
																if tokenizer.__class__ in (
																    RobertaTokenizer,
																    RobertaTokenizerFast,
																    XLMRobertaTokenizer,
																    BartTokenizer,
																    BartTokenizerFast,
																):
																							# HACK(label indices are swapped in RoBERTa pretrained model)
																							__SCREAMING_SNAKE_CASE				,			__SCREAMING_SNAKE_CASE										=   label_list[2], label_list[1]
																__SCREAMING_SNAKE_CASE										=   label_list

																__SCREAMING_SNAKE_CASE										=   processor.get_dev_examples(__a						) if evaluate else processor.get_train_examples(__a						)
																__SCREAMING_SNAKE_CASE										=   hans_convert_examples_to_features(__a ,				__a ,				__a ,				__a						)

																def gen():
																							for ex_index, ex in tqdm.tqdm(enumerate(self.features						) ,				desc='convert examples to features'						):
																														if ex_index % 10_000 == 0:
																																					logger.info('Writing example %d of %d' % (ex_index, len(__a						))						)

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

																__SCREAMING_SNAKE_CASE										=   tf.data.Dataset.from_generator(
																    __a ,				(
																        {
																            '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 			_A      (       self						):







																'''simple docstring'''

																return self.dataset






									def __len__(       self						):







																'''simple docstring'''

																return len(self.features						)






									def __getitem__(       self ,				_A						):







																'''simple docstring'''

																return self.features[i]






									def 			_A      (       self						):







																'''simple docstring'''

																return self.label_list

class   UpperCAmelCase_    (  lowerCAmelCase__			):

						'''simple docstring'''




						def 			_A      (       self ,				_A						):







													'''simple docstring'''

													return self._create_examples(self._read_tsv(os.path.join(__a ,				'heuristics_train_set.txt'						)						) ,				'train'						)






						def 			_A      (       self ,				_A						):







													'''simple docstring'''

													return self._create_examples(self._read_tsv(os.path.join(__a ,				'heuristics_evaluation_set.txt'						)						) ,				'dev'						)






						def 			_A      (       self						):







													'''simple docstring'''

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






						def 			_A      (       self ,				_A ,				_A						):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE										=   []
													for i, line in enumerate(__a						):
																				if i == 0:
																											continue
																				__SCREAMING_SNAKE_CASE										=   '%s-%s' % (set_type, line[0])
																				__SCREAMING_SNAKE_CASE										=   line[5]
																				__SCREAMING_SNAKE_CASE										=   line[6]
																				__SCREAMING_SNAKE_CASE										=   line[7][2:] if line[7].startswith('ex'						) else line[7]
																				__SCREAMING_SNAKE_CASE										=   line[0]
																				examples.append(InputExample(guid=__a ,				text_a=__a ,				text_b=__a ,				label=__a ,				pairID=__a						)						)
													return examples







def       __lowercase  (					a__				,  a__				,  a__				,  a__				,  )							->					Any:
							__SCREAMING_SNAKE_CASE										=   {label: i for i, label in enumerate(_UpperCamelCase  )}

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

														__SCREAMING_SNAKE_CASE										=   tokenizer(
														    example.text_a				,  example.text_b				,  add_special_tokens=_UpperCamelCase				,  max_length=_UpperCamelCase				,  padding='max_length'				,  truncation=_UpperCamelCase				,  return_overflowing_tokens=_UpperCamelCase				,  )

														__SCREAMING_SNAKE_CASE										=   label_map[example.label] if example.label in label_map else 0

														__SCREAMING_SNAKE_CASE										=   int(example.pairID  )

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

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

							return features


lowerCAmelCase__						:	Dict					={
    "hans": 3,
}

lowerCAmelCase__						:	List[Any]					={
    "hans": HansProcessor,
}










"""simple docstring"""


import heapq as hq
import math
from collections.abc import Iterator
class     _UpperCamelCase    :
							'''simple docstring'''







							def __init__(	self       ,  __a      ):
										__lowerCAmelCase							       =     str(id_      )
										__lowerCAmelCase							       =     None
										__lowerCAmelCase							       =     None
										__lowerCAmelCase							       =     []
										__lowerCAmelCase							       =     {}  # {vertex:distance}

							def __lt__(	self       ,  __a      ):
										return self.key < other.key

							def __repr__(	self      ):
										return self.id

							def      snake_case							(	self       ,  __a      ):
										self.neighbors.append(__a      )



							def      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     weight


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			graph[a - 1].add_neighbor(graph[b - 1] )
			graph[b - 1].add_neighbor(graph[a - 1] )
			# add the edges:
			graph[a - 1].add_edge(graph[b - 1]       ,					_UpperCamelCase )
			graph[b - 1].add_edge(graph[a - 1]       ,					_UpperCamelCase )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     []
			for u in graph:
						__lowerCAmelCase							       =     math.inf
						__lowerCAmelCase							       =     None
			__lowerCAmelCase							       =     0
			__lowerCAmelCase							       =     graph[:]
			while q:
						__lowerCAmelCase							       =     min(_UpperCamelCase )
						q.remove(_UpperCamelCase )
						for v in u.neighbors:
									if (v in q) and (u.edges[v.id] < v.key):
												__lowerCAmelCase							       =     u
												__lowerCAmelCase							       =     u.edges[v.id]
			for i in range(1       ,					len(_UpperCamelCase ) ):
						a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) )
			return a


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			for u in graph:
						__lowerCAmelCase							       =     math.inf
						__lowerCAmelCase							       =     None
			__lowerCAmelCase							       =     0

			__lowerCAmelCase							       =     list(_UpperCamelCase )
			hq.heapify(_UpperCamelCase )

			while h:
						__lowerCAmelCase							       =     hq.heappop(_UpperCamelCase )
						for v in u.neighbors:
									if (v in h) and (u.edges[v.id] < v.key):
												__lowerCAmelCase							       =     u
												__lowerCAmelCase							       =     u.edges[v.id]
												hq.heapify(_UpperCamelCase )

			for i in range(1       ,					len(_UpperCamelCase ) ):
						yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1)


def _lowerCamelCase      (						):

			'''simple docstring'''






if __name__ == "__main__":
	import doctest

	doctest.testmod()




