



'''simple docstring'''
import argparse
import json
import re
import string
import sys
from collections import Counter


def   lowercase_	(   lowerCAmelCase__	:  List[Any] ):

			"""simple docstring"""





			def remove_articles(lowerCAmelCase__	:  Optional[int] ):
						return re.sub(r"""\b(a|an|the)\b"""			,							""" """			,							lowerCAmelCase__ )

			def white_space_fix(lowerCAmelCase__	:  str ):
						return " ".join(text.split() )

			def remove_punc(lowerCAmelCase__	:  str ):
						__UpperCAmelCase   :      Any										=     set(string.punctuation )
						return "".join(ch for ch in text if ch not in exclude )

			def lower(lowerCAmelCase__	:  Union[str, Any] ):
						return text.lower()

			return white_space_fix(remove_articles(remove_punc(lower(lowerCAmelCase__ ) ) ) )


def   lowercase_	(   lowerCAmelCase__	:  Optional[Any]			,							lowerCAmelCase__	:  int ):

			"""simple docstring"""





			__UpperCAmelCase   :      Tuple										=     normalize_answer(lowerCAmelCase__ ).split()
			__UpperCAmelCase   :      str										=     normalize_answer(lowerCAmelCase__ ).split()
			__UpperCAmelCase   :      Dict										=     Counter(lowerCAmelCase__ ) & Counter(lowerCAmelCase__ )
			__UpperCAmelCase   :      List[str]										=     sum(common.values() )
			if num_same == 0:
						return 0
			__UpperCAmelCase   :      List[str]										=     1.0 * num_same / len(lowerCAmelCase__ )
			__UpperCAmelCase   :      Union[str, Any]										=     1.0 * num_same / len(lowerCAmelCase__ )
			__UpperCAmelCase   :      str										=     (2 * precision * recall) / (precision + recall)
			return fa


def   lowercase_	(   lowerCAmelCase__	:  int			,							lowerCAmelCase__	:  Optional[Any] ):

			"""simple docstring"""





			return normalize_answer(lowerCAmelCase__ ) == normalize_answer(lowerCAmelCase__ )


def   lowercase_	(   lowerCAmelCase__	:  Tuple			,							lowerCAmelCase__	:  Optional[Any]			,							lowerCAmelCase__	:  Tuple ):

			"""simple docstring"""





			__UpperCAmelCase   :      str										=     []
			for ground_truth in ground_truths:
						__UpperCAmelCase   :      Any										=     metric_fn(lowerCAmelCase__			,							lowerCAmelCase__ )
						scores_for_ground_truths.append(lowerCAmelCase__ )
			return max(lowerCAmelCase__ )


def   lowercase_	(   lowerCAmelCase__	:  str			,							lowerCAmelCase__	:  Union[str, Any] ):

			"""simple docstring"""





			__UpperCAmelCase   :      Any										=     0
			for article in dataset:
						for paragraph in article["paragraphs"]:
									for qa in paragraph["qas"]:
												total += 1
												if qa["id"] not in predictions:
															__UpperCAmelCase   :      Tuple										=     """Unanswered question """ + qa["""id"""] + """ will receive score 0."""
															print(lowerCAmelCase__			,							file=sys.stderr )
															continue
												__UpperCAmelCase   :      List[Any]										=     [x["""text"""] for x in qa["""answers"""]]
												__UpperCAmelCase   :      List[Any]										=     predictions[qa["""id"""]]
												exact_match += metric_max_over_ground_truths(lowerCAmelCase__			,							lowerCAmelCase__			,							lowerCAmelCase__ )
												fa += metric_max_over_ground_truths(lowerCAmelCase__			,							lowerCAmelCase__			,							lowerCAmelCase__ )

			__UpperCAmelCase   :      List[Any]										=     100.0 * exact_match / total
			__UpperCAmelCase   :      Any										=     100.0 * fa / total

			return {"exact_match": exact_match, "f1": fa}


if __name__ == "__main__":
					_UpperCamelCase											=			'''1.1'''
					_UpperCamelCase											=			argparse.ArgumentParser(description='''Evaluation for SQuAD ''' + expected_version)
					parser.add_argument('''dataset_file''', help='''Dataset file''')
					parser.add_argument('''prediction_file''', help='''Prediction File''')
					_UpperCamelCase											=			parser.parse_args()
					with open(args.dataset_file) as dataset_file:
										_UpperCamelCase											=			json.load(dataset_file)
										if dataset_json["version"] != expected_version:
															print(
															    '''Evaluation expects v-''' + expected_version + ''', but got dataset with v-''' + dataset_json['''version'''],
															    file=sys.stderr,
															)
										_UpperCamelCase											=			dataset_json['''data''']
					with open(args.prediction_file) as prediction_file:
										_UpperCamelCase											=			json.load(prediction_file)
					print(json.dumps(evaluate(dataset, predictions)))









'''simple docstring'''
import torch

from transformers import AutoTokenizer



class _A  (    __SCREAMING_SNAKE_CASE    ):





		def __init__(     self ,       __UpperCAmelCase				)       ->     Union[str, Any]:

					'''simple docstring'''
					__UpperCAmelCase   :      Any										=     AutoTokenizer.from_pretrained(__UpperCAmelCase				)





		def 							__A       (     self ,       __UpperCAmelCase				)       ->     Any:

					'''simple docstring'''

					if isinstance(__UpperCAmelCase ,       __UpperCAmelCase				) and all(isinstance(_x ,       __UpperCAmelCase				) for _x in x				):
								__UpperCAmelCase   :      Dict										=     None
								for l in x:
											__UpperCAmelCase   :      Dict										=     self.tokenizer(
											    __UpperCAmelCase ,       padding="""max_length""" ,       max_length=384 ,       truncation=__UpperCAmelCase ,       return_tensors="""pt""" ,       )
											__UpperCAmelCase   :      Union[str, Any]										=     torch.tensor([len(__UpperCAmelCase				)]				)
											if d is not None:
														for k in d.keys():
																	__UpperCAmelCase   :      Dict										=     torch.cat((d[k], t[k]) ,       0				)
											else:
														__UpperCAmelCase   :      List[str]										=     t

								__UpperCAmelCase   :      Union[str, Any]										=     torch.tensor(self.tokenizer.convert_tokens_to_ids("""[E]"""				)				)
								__UpperCAmelCase   :      Any										=     torch.tensor(self.tokenizer.convert_tokens_to_ids("""[/E]"""				)				)

					elif isinstance(__UpperCAmelCase ,       __UpperCAmelCase				) and all(isinstance(_x ,       __UpperCAmelCase				) for _x in x				):
								__UpperCAmelCase   :      int										=     self.tokenizer(
								    __UpperCAmelCase ,       padding="""max_length""" ,       max_length=384 ,       truncation=__UpperCAmelCase ,       return_tensors="""pt""" ,       )

					else:
								raise Exception(
								    """Type of parameter x was not recognized! Only `list of strings` for query or `list of lists of"""
								    """ strings` for supports are supported."""				)

					return d





		def 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase=0.70				)       ->     Dict:

					'''simple docstring'''
					__UpperCAmelCase   :      Tuple										=     []
					for idx in range(len(W_query["""input_ids"""]				)				):
								__UpperCAmelCase   :      Optional[int]										=     range(p_start.shape[1]				)

								__UpperCAmelCase   :      Union[str, Any]										=     []
								for start_id in start_indexes:
											for end_id in end_indexes:
														if start_id < end_id:
																	output.append(
																	    (
																	        start_id,
																	        end_id,
																	        p_start[idx][start_id].item(),
																	        p_end[idx][end_id].item(),
																	    )				)

								output.sort(key=lambda __UpperCAmelCase				: (tup[2] * tup[3]) ,       reverse=__UpperCAmelCase				)
								__UpperCAmelCase   :      Optional[Any]										=     []
								for k in range(len(__UpperCAmelCase				)				):
											if output[k][2] * output[k][3] >= thresh:
														__UpperCAmelCase				,  __UpperCAmelCase   :      Tuple										=     output[k][0], output[k][1]
														__UpperCAmelCase   :      int										=     self.tokenizer.decode(W_query["""input_ids"""][idx][c_start_pos:c_end_pos]				)
														temp.append((decoded, output[k][2] * output[k][3])				)

								final_outputs.append(__UpperCAmelCase				)

					return final_outputs




