


'''simple docstring'''

import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


lowerCAmelCase :Optional[int]             =     re.compile(r'''\b(a|an|the)\b''', re.UNICODE)

lowerCAmelCase :Tuple             =     None





def lowerCamelCase     (     ):





	"""simple docstring"""







	__magic_name__			:       Optional[Any]	      =	argparse.ArgumentParser('Official evaluation script for SQuAD version 2.0.'						)
	parser.add_argument('data_file'						,						metavar='data.json'						,						help='Input data JSON file.'						)
	parser.add_argument('pred_file'						,						metavar='pred.json'						,						help='Model predictions.'						)
	parser.add_argument(
	    '--out-file'						,						'-o'						,						metavar='eval.json'						,						help='Write accuracy metrics to file (default is stdout).'						)
	parser.add_argument(
	    '--na-prob-file'						,						'-n'						,						metavar='na_prob.json'						,						help='Model estimates of probability of no answer.'						)
	parser.add_argument(
	    '--na-prob-thresh'						,						'-t'						,						type=__lowerCAmelCase						,						default=1.0						,						help='Predict \"\" if no-answer probability exceeds this (default = 1.0).'						,						)
	parser.add_argument(
	    '--out-image-dir'						,						'-p'						,						metavar='out_images'						,						default=__lowerCAmelCase						,						help='Save precision-recall curves to directory.'						)
	parser.add_argument('--verbose'						,						'-v'						,						action='store_true'						)
	if len(sys.argv						) == 1:
		parser.print_help()
		sys.exit(1						)
	return parser.parse_args()





def lowerCamelCase     (     lowerCAmelCase							:      str						):





	"""simple docstring"""







	__magic_name__			:       str	      =	{}
	for article in dataset:
		for p in article["paragraphs"]:
			for qa in p["qas"]:
				__magic_name__			:       Optional[Any]	      =	bool(qa['answers']['text']						)
	return qid_to_has_ans





def lowerCamelCase     (     lowerCAmelCase							:      str						):





	"""simple docstring"""







	def remove_articles(lowerCAmelCase							:      Optional[Any]						):
		return ARTICLES_REGEX.sub(' '						,						__lowerCAmelCase						)

	def white_space_fix(lowerCAmelCase							:      Any						):
		return " ".join(text.split()						)

	def remove_punc(lowerCAmelCase							:      Optional[Any]						):
		__magic_name__			:       int	      =	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 lowerCamelCase     (     lowerCAmelCase							:      Optional[Any]						):





	"""simple docstring"""







	if not s:
		return []
	return normalize_answer(__lowerCAmelCase						).split()





def lowerCamelCase     (     lowerCAmelCase							:      List[Any]						,						lowerCAmelCase							:      List[Any]						):





	"""simple docstring"""







	return int(normalize_answer(__lowerCAmelCase						) == normalize_answer(__lowerCAmelCase						)						)





def lowerCamelCase     (     lowerCAmelCase							:      Union[str, Any]						,						lowerCAmelCase							:      Union[str, Any]						):





	"""simple docstring"""







	__magic_name__			:       int	      =	get_tokens(__lowerCAmelCase						)
	__magic_name__			:       Union[str, Any]	      =	get_tokens(__lowerCAmelCase						)
	__magic_name__			:       Any	      =	collections.Counter(__lowerCAmelCase						) & collections.Counter(__lowerCAmelCase						)
	__magic_name__			:       Any	      =	sum(common.values()						)
	if len(__lowerCAmelCase						) == 0 or len(__lowerCAmelCase						) == 0:
		# If either is no-answer, then F1 is 1 if they agree, 0 otherwise
		return int(gold_toks == pred_toks						)
	if num_same == 0:
		return 0
	__magic_name__			:       List[str]	      =	1.0 * num_same / len(__lowerCAmelCase						)
	__magic_name__			:       Union[str, Any]	      =	1.0 * num_same / len(__lowerCAmelCase						)
	__magic_name__			:       int	      =	(2 * precision * recall) / (precision + recall)
	return fa





def lowerCamelCase     (     lowerCAmelCase							:      Tuple						,						lowerCAmelCase							:      List[str]						):





	"""simple docstring"""







	__magic_name__			:       int	      =	{}
	__magic_name__			:       Dict	      =	{}
	for article in dataset:
		for p in article["paragraphs"]:
			for qa in p["qas"]:
				__magic_name__			:       int	      =	qa['id']
				__magic_name__			:       int	      =	[t for t in qa['answers']['text'] if normalize_answer(__lowerCAmelCase						)]
				if not gold_answers:
					# For unanswerable questions, only correct answer is empty string
					__magic_name__			:       Optional[Any]	      =	['']
				if qid not in preds:
					print(f'Missing prediction for {qid}'						)
					continue
				__magic_name__			:       Optional[Any]	      =	preds[qid]
				# Take max over all gold answers
				__magic_name__			:       Union[str, Any]	      =	max(compute_exact(__lowerCAmelCase						,						__lowerCAmelCase						) for a in gold_answers						)
				__magic_name__			:       int	      =	max(compute_fa(__lowerCAmelCase						,						__lowerCAmelCase						) for a in gold_answers						)
	return exact_scores, fa_scores





def lowerCamelCase     (     lowerCAmelCase							:      Optional[int]						,						lowerCAmelCase							:      Optional[Any]						,						lowerCAmelCase							:      Optional[Any]						,						lowerCAmelCase							:      List[Any]						):





	"""simple docstring"""







	__magic_name__			:       Optional[int]	      =	{}
	for qid, s in scores.items():
		__magic_name__			:       str	      =	na_probs[qid] > na_prob_thresh
		if pred_na:
			__magic_name__			:       Optional[Any]	      =	float(not qid_to_has_ans[qid]						)
		else:
			__magic_name__			:       Union[str, Any]	      =	s
	return new_scores





def lowerCamelCase     (     lowerCAmelCase							:      Optional[int]						,						lowerCAmelCase							:      Any						,						lowerCAmelCase							:      Optional[Any]=None						):





	"""simple docstring"""







	if not qid_list:
		__magic_name__			:       str	      =	len(__lowerCAmelCase						)
		return collections.OrderedDict(
		    [
		        ('exact', 100.0 * sum(exact_scores.values()						) / total),
		        ('f1', 100.0 * sum(fa_scores.values()						) / total),
		        ('total', total),
		    ]						)
	else:
		__magic_name__			:       Union[str, Any]	      =	len(__lowerCAmelCase						)
		return collections.OrderedDict(
		    [
		        ('exact', 100.0 * sum(exact_scores[k] for k in qid_list						) / total),
		        ('f1', 100.0 * sum(fa_scores[k] for k in qid_list						) / total),
		        ('total', total),
		    ]						)





def lowerCamelCase     (     lowerCAmelCase							:      Union[str, Any]						,						lowerCAmelCase							:      Any						,						lowerCAmelCase							:      str						):





	"""simple docstring"""







	for k in new_eval:
		__magic_name__			:       Union[str, Any]	      =	new_eval[k]





def lowerCamelCase     (     lowerCAmelCase							:      Optional[int]						,						lowerCAmelCase							:      Any						,						lowerCAmelCase							:      Optional[Any]						,						lowerCAmelCase							:      str						):





	"""simple docstring"""







	plt.step(__lowerCAmelCase						,						__lowerCAmelCase						,						color='b'						,						alpha=0.2						,						where='post'						)
	plt.fill_between(__lowerCAmelCase						,						__lowerCAmelCase						,						step='post'						,						alpha=0.2						,						color='b'						)
	plt.xlabel('Recall'						)
	plt.ylabel('Precision'						)
	plt.xlim([0.0, 1.05]						)
	plt.ylim([0.0, 1.05]						)
	plt.title(__lowerCAmelCase						)
	plt.savefig(__lowerCAmelCase						)
	plt.clf()





def lowerCamelCase     (     lowerCAmelCase							:      Tuple						,						lowerCAmelCase							:      Optional[int]						,						lowerCAmelCase							:      Dict						,						lowerCAmelCase							:      List[Any]						,						lowerCAmelCase							:      List[Any]=None						,						lowerCAmelCase							:      Optional[int]=None						):





	"""simple docstring"""







	__magic_name__			:       Optional[int]	      =	sorted(__lowerCAmelCase						,						key=lambda lowerCAmelCase						: na_probs[k]						)
	__magic_name__			:       int	      =	0.0
	__magic_name__			:       Any	      =	1.0
	__magic_name__			:       Dict	      =	0.0
	__magic_name__			:       List[str]	      =	[1.0]
	__magic_name__			:       Dict	      =	[0.0]
	__magic_name__			:       List[Any]	      =	0.0
	for i, qid in enumerate(__lowerCAmelCase						):
		if qid_to_has_ans[qid]:
			true_pos += scores[qid]
		__magic_name__			:       str	      =	true_pos / float(i + 1						)
		__magic_name__			:       Tuple	      =	true_pos / float(__lowerCAmelCase						)
		if i == len(__lowerCAmelCase						) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
			# i.e., if we can put a threshold after this point
			avg_prec += cur_p * (cur_r - recalls[-1])
			precisions.append(__lowerCAmelCase						)
			recalls.append(__lowerCAmelCase						)
	if out_image:
		plot_pr_curve(__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						)
	return {"ap": 100.0 * avg_prec}





def lowerCamelCase     (     lowerCAmelCase							:      List[Any]						,						lowerCAmelCase							:      Optional[int]						,						lowerCAmelCase							:      Dict						,						lowerCAmelCase							:      List[Any]						,						lowerCAmelCase							:      List[Any]						,						lowerCAmelCase							:      str						):





	"""simple docstring"""







	if out_image_dir and not os.path.exists(__lowerCAmelCase						):
		os.makedirs(__lowerCAmelCase						)
	__magic_name__			:       str	      =	sum(1 for v in qid_to_has_ans.values() if v						)
	if num_true_pos == 0:
		return
	__magic_name__			:       Dict	      =	make_precision_recall_eval(
	    __lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						out_image=os.path.join(__lowerCAmelCase						,						'pr_exact.png'						)						,						title='Precision-Recall curve for Exact Match score'						,						)
	__magic_name__			:       int	      =	make_precision_recall_eval(
	    __lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						out_image=os.path.join(__lowerCAmelCase						,						'pr_f1.png'						)						,						title='Precision-Recall curve for F1 score'						,						)
	__magic_name__			:       List[Any]	      =	{k: float(__lowerCAmelCase						) for k, v in qid_to_has_ans.items()}
	__magic_name__			:       str	      =	make_precision_recall_eval(
	    __lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						out_image=os.path.join(__lowerCAmelCase						,						'pr_oracle.png'						)						,						title='Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)'						,						)
	merge_eval(__lowerCAmelCase						,						__lowerCAmelCase						,						'pr_exact'						)
	merge_eval(__lowerCAmelCase						,						__lowerCAmelCase						,						'pr_f1'						)
	merge_eval(__lowerCAmelCase						,						__lowerCAmelCase						,						'pr_oracle'						)





def lowerCamelCase     (     lowerCAmelCase							:      Optional[int]						,						lowerCAmelCase							:      List[str]						,						lowerCAmelCase							:      Any						,						lowerCAmelCase							:      Any						):





	"""simple docstring"""







	if not qid_list:
		return
	__magic_name__			:       Optional[int]	      =	[na_probs[k] for k in qid_list]
	__magic_name__			:       Optional[int]	      =	np.ones_like(__lowerCAmelCase						) / float(len(__lowerCAmelCase						)						)
	plt.hist(__lowerCAmelCase						,						weights=__lowerCAmelCase						,						bins=20						,						range=(0.0, 1.0)						)
	plt.xlabel('Model probability of no-answer'						)
	plt.ylabel('Proportion of dataset'						)
	plt.title(f'Histogram of no-answer probability: {name}'						)
	plt.savefig(os.path.join(__lowerCAmelCase						,						f'na_prob_hist_{name}.png'						)						)
	plt.clf()





def lowerCamelCase     (     lowerCAmelCase							:      Dict						,						lowerCAmelCase							:      Tuple						,						lowerCAmelCase							:      Tuple						,						lowerCAmelCase							:      Optional[int]						):





	"""simple docstring"""







	__magic_name__			:       Any	      =	sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]						)
	__magic_name__			:       str	      =	num_no_ans
	__magic_name__			:       Tuple	      =	cur_score
	__magic_name__			:       int	      =	0.0
	__magic_name__			:       List[str]	      =	sorted(__lowerCAmelCase						,						key=lambda lowerCAmelCase						: na_probs[k]						)
	for i, qid in enumerate(__lowerCAmelCase						):
		if qid not in scores:
			continue
		if qid_to_has_ans[qid]:
			__magic_name__			:       Any	      =	scores[qid]
		else:
			if preds[qid]:
				__magic_name__			:       Dict	      =	-1
			else:
				__magic_name__			:       Optional[Any]	      =	0
		cur_score += diff
		if cur_score > best_score:
			__magic_name__			:       int	      =	cur_score
			__magic_name__			:       Optional[Any]	      =	na_probs[qid]
	return 100.0 * best_score / len(__lowerCAmelCase						), best_thresh





def lowerCamelCase     (     lowerCAmelCase							:      Any						,						lowerCAmelCase							:      Any						,						lowerCAmelCase							:      List[str]						,						lowerCAmelCase							:      Optional[int]						,						lowerCAmelCase							:      Union[str, Any]						,						lowerCAmelCase							:      Tuple						):





	"""simple docstring"""







	__magic_name__      ,							__magic_name__			:       List[str]	      =	find_best_thresh(__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						)
	__magic_name__      ,							__magic_name__			:       Any	      =	find_best_thresh(__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						)
	__magic_name__			:       Optional[int]	      =	best_exact
	__magic_name__			:       int	      =	exact_thresh
	__magic_name__			:       Optional[int]	      =	best_fa
	__magic_name__			:       Any	      =	fa_thresh





def lowerCamelCase     (     ):





	"""simple docstring"""







	with open(OPTS.data_file						) as f:
		__magic_name__			:       str	      =	json.load(__lowerCAmelCase						)
		__magic_name__			:       Tuple	      =	dataset_json['data']
	with open(OPTS.pred_file						) as f:
		__magic_name__			:       Tuple	      =	json.load(__lowerCAmelCase						)
	if OPTS.na_prob_file:
		with open(OPTS.na_prob_file						) as f:
			__magic_name__			:       List[str]	      =	json.load(__lowerCAmelCase						)
	else:
		__magic_name__			:       Union[str, Any]	      =	{k: 0.0 for k in preds}
	__magic_name__			:       int	      =	make_qid_to_has_ans(__lowerCAmelCase						)  # maps qid to True/False
	__magic_name__			:       Any	      =	[k for k, v in qid_to_has_ans.items() if v]
	__magic_name__			:       Tuple	      =	[k for k, v in qid_to_has_ans.items() if not v]
	__magic_name__      ,							__magic_name__			:       Optional[Any]	      =	get_raw_scores(__lowerCAmelCase						,						__lowerCAmelCase						)
	__magic_name__			:       Optional[int]	      =	apply_no_ans_threshold(__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						OPTS.na_prob_thresh						)
	__magic_name__			:       int	      =	apply_no_ans_threshold(__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						OPTS.na_prob_thresh						)
	__magic_name__			:       Union[str, Any]	      =	make_eval_dict(__lowerCAmelCase						,						__lowerCAmelCase						)
	if has_ans_qids:
		__magic_name__			:       Dict	      =	make_eval_dict(__lowerCAmelCase						,						__lowerCAmelCase						,						qid_list=__lowerCAmelCase						)
		merge_eval(__lowerCAmelCase						,						__lowerCAmelCase						,						'HasAns'						)
	if no_ans_qids:
		__magic_name__			:       Tuple	      =	make_eval_dict(__lowerCAmelCase						,						__lowerCAmelCase						,						qid_list=__lowerCAmelCase						)
		merge_eval(__lowerCAmelCase						,						__lowerCAmelCase						,						'NoAns'						)
	if OPTS.na_prob_file:
		find_all_best_thresh(__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						)
	if OPTS.na_prob_file and OPTS.out_image_dir:
		run_precision_recall_analysis(__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						__lowerCAmelCase						,						OPTS.out_image_dir						)
		histogram_na_prob(__lowerCAmelCase						,						__lowerCAmelCase						,						OPTS.out_image_dir						,						'hasAns'						)
		histogram_na_prob(__lowerCAmelCase						,						__lowerCAmelCase						,						OPTS.out_image_dir						,						'noAns'						)
	if OPTS.out_file:
		with open(OPTS.out_file						,						'w'						) as f:
			json.dump(__lowerCAmelCase						,						__lowerCAmelCase						)
	else:
		print(json.dumps(__lowerCAmelCase						,						indent=2						)						)


if __name__ == "__main__":
							lowerCAmelCase :List[Any]             =     parse_args()
							if OPTS.out_image_dir:
														import matplotlib

														matplotlib.use('''Agg''')
														import matplotlib.pyplot as plt
							main()

from __future__ import annotations

import math





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase				):

    if 1 < number < 4:
        # 2 and 3 are primes
        return True
    elif number < 2 or number % 2 == 0 or number % 3 == 0:
        # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
        return False

    # All primes number are in format of 6k +/- 1
    for i in range(5     ,		int(math.sqrt(__lowerCAmelCase				) + 1				)     ,		6				):
        if number % i == 0 or number % (i + 2) == 0:
            return False
    return True





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase				):
    snake_case__    =					str(__lowerCAmelCase				)
    snake_case__    =					[n]
    for i in range(1     ,		len(__lowerCAmelCase				)				):
        list_nums.append(int(str_num[i:]				)				)
        list_nums.append(int(str_num[:-i]				)				)
    return list_nums





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase				):
    if len(str(__lowerCAmelCase				)				) > 3:
        if not is_prime(int(str(__lowerCAmelCase				)[-3:]				)				) or not is_prime(int(str(__lowerCAmelCase				)[:3]				)				):
            return False
    return True





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase = 11				):
    snake_case__    =					[]
    snake_case__    =					13
    while len(__lowerCAmelCase				) != count:
        if validate(__lowerCAmelCase				):
            snake_case__    =					list_truncated_nums(__lowerCAmelCase				)
            if all(is_prime(__lowerCAmelCase				) for i in list_nums				):
                list_truncated_primes.append(__lowerCAmelCase				)
        num += 2
    return list_truncated_primes





def 						SCREAMING_SNAKE_CASE__     (   ):
    return sum(compute_truncated_primes(11				)				)


if __name__ == "__main__":
     print(F'''{sum(compute_truncated_primes(11)) = }''')



