



"""simple docstring"""

import os

import jsonlines
import numpy as np
from tqdm import tqdm


a_			   =					2048
a_			   =					4096
a_			   =					42
a_			   =					os.environ.pop('''PROCESS_TRAIN''', '''false''')
a_			   =					{'''null''': 0, '''short''': 1, '''long''': 2, '''yes''': 3, '''no''': 4}







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int			):



					"""simple docstring"""





					def choose_first(SCREAMING_SNAKE_CASE__ :  List[str]				,    SCREAMING_SNAKE_CASE__ :  Optional[int]=False			):
										assert isinstance(_snake_case				,    _snake_case			)
										if len(_snake_case			) == 1:
															snake_case_					:		List[Any] 					=					answer[0]
															return {k: [answer[k]] for k in answer} if is_long_answer else answer
										for a in answer:
															if is_long_answer:
																				snake_case_					:		List[Any] 					=					{k: [a[k]] for k in a}
															if len(a["""start_token"""]			) > 0:
																				break
										return a

					snake_case_					:		Any 					=					{"""id""": example["""id"""]}
					snake_case_					:		Tuple 					=					example["""annotations"""]
					snake_case_					:		Union[str, Any] 					=					annotation["""yes_no_answer"""]
					if 0 in yes_no_answer or 1 in yes_no_answer:
										snake_case_					:		str 					=					["""yes"""] if 1 in yes_no_answer else ["""no"""]
										snake_case_					:		Dict 					=					[]
										snake_case_					:		List[str] 					=					[]
										snake_case_					:		Union[str, Any] 					=					["""<cls>"""]
					else:
										snake_case_					:		List[Any] 					=					["""short"""]
										snake_case_					:		Any 					=					choose_first(annotation["""short_answers"""]			)
										if len(out["""start_token"""]			) == 0:
															# answer will be long if short is not available
															snake_case_					:		str 					=					["""long"""]
															snake_case_					:		str 					=					choose_first(annotation["""long_answer"""]				,    is_long_answer=_snake_case			)
															snake_case_					:		Tuple 					=					[]
										answer.update(_snake_case			)

					# disregard some samples
					if len(answer["""start_token"""]			) > 1 or answer["start_token"] == answer["end_token"]:
										snake_case_					:		Dict 					=					True
					else:
										snake_case_					:		Tuple 					=					False

					snake_case_					:		Tuple 					=					["""start_token""", """end_token""", """start_byte""", """end_byte""", """text"""]
					if not all(isinstance(answer[k]				,    _snake_case			) for k in cols			):
										raise ValueError("""Issue in ID"""				,    example["""id"""]			)

					return answer







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Union[str, Any]				,    SCREAMING_SNAKE_CASE__ :  Union[str, Any]=False			):



					"""simple docstring"""





					snake_case_					:		int 					=					_get_single_answer(_snake_case			)
					# bytes are of no use
					del answer["start_byte"]
					del answer["end_byte"]

					# handle yes_no answers explicitly
					if answer["category"][0] in ["yes", "no"]:  # category is list with one element
										snake_case_					:		Optional[int] 					=					example["""document"""]["""tokens"""]
										snake_case_					:		Tuple 					=					[]
										for i in range(len(doc["""token"""]			)			):
															if not doc["is_html"][i]:
																				context.append(doc["""token"""][i]			)
										return {
										    "context": " ".join(_snake_case			),
										    "answer": {
										        "start_token": -1_0_0,  # ignore index in cross-entropy
										        "end_token": -1_0_0,  # ignore index in cross-entropy
										        "category": answer["category"],
										        "span": answer["category"],  # extra
										    },
										}

					# later, help in removing all no answers
					if answer["start_token"] == [-1]:
										return {
										    "context": "None",
										    "answer": {
										        "start_token": -1,
										        "end_token": -1,
										        "category": "null",
										        "span": "None",  # extra
										    },
										}

					# handling normal samples

					snake_case_					:		Dict 					=					["""start_token""", """end_token"""]
					answer.update({k: answer[k][0] if len(answer[k]			) > 0 else answer[k] for k in cols}			)  # e.g. [10] == 10

					snake_case_					:		List[str] 					=					example["""document"""]["""tokens"""]
					snake_case_					:		Dict 					=					answer["""start_token"""]
					snake_case_					:		int 					=					answer["""end_token"""]

					snake_case_					:		Any 					=					[]
					for i in range(len(doc["""token"""]			)			):
										if not doc["is_html"][i]:
															context.append(doc["""token"""][i]			)
										else:
															if answer["start_token"] > i:
																				start_token -= 1
															if answer["end_token"] > i:
																				end_token -= 1
					snake_case_					:		Any 					=					""" """.join(context[start_token:end_token]			)

					# checking above code
					if assertion:
										snake_case_					:		str 					=					doc["""is_html"""][answer["""start_token"""] : answer["""end_token"""]]
										snake_case_					:		Dict 					=					doc["""token"""][answer["""start_token"""] : answer["""end_token"""]]
										snake_case_					:		Tuple 					=					""" """.join([old[i] for i in range(len(_snake_case			)			) if not is_html[i]]			)
										if new != old:
															print("""ID:"""				,    example["""id"""]			)
															print("""New:"""				,    _snake_case				,    end="""\n"""			)
															print("""Old:"""				,    _snake_case				,    end="""\n\n"""			)

					return {
					    "context": " ".join(_snake_case			),
					    "answer": {
					        "start_token": start_token,
					        "end_token": end_token - 1,  # this makes it inclusive
					        "category": answer["category"],  # either long or short
					        "span": new,  # extra
					    },
					}







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  List[str]				,    SCREAMING_SNAKE_CASE__ :  List[Any]=2_0_4_8				,    SCREAMING_SNAKE_CASE__ :  Tuple=4_0_9_6				,    SCREAMING_SNAKE_CASE__ :  Union[str, Any]=True			):



					"""simple docstring"""





					snake_case_					:		Any 					=					get_context_and_ans(_snake_case				,    assertion=_snake_case			)
					snake_case_					:		Any 					=					out["""answer"""]

					# later, removing these samples
					if answer["start_token"] == -1:
										return {
										    "example_id": example["id"],
										    "input_ids": [[-1]],
										    "labels": {
										        "start_token": [-1],
										        "end_token": [-1],
										        "category": ["null"],
										    },
										}

					snake_case_					:		Optional[Any] 					=					tokenizer(example["""question"""]["""text"""]				,    out["""context"""]			).input_ids
					snake_case_					:		List[str] 					=					input_ids.index(tokenizer.sep_token_id			) + 1

					# return yes/no
					if answer["category"][0] in ["yes", "no"]:  # category is list with one element
										snake_case_					:		int 					=					[]
										snake_case_					:		Union[str, Any] 					=					[]
										snake_case_					:		Tuple 					=					input_ids[:q_len]
										snake_case_					:		Union[str, Any] 					=					range(_snake_case				,    len(_snake_case			)				,    max_length - doc_stride			)
										for i in doc_start_indices:
															snake_case_					:		str 					=					i + max_length - q_len
															snake_case_					:		Dict 					=					input_ids[i:end_index]
															inputs.append(q_indices + slice			)
															category.append(answer["""category"""][0]			)
															if slice[-1] == tokenizer.sep_token_id:
																				break

										return {
										    "example_id": example["id"],
										    "input_ids": inputs,
										    "labels": {
										        "start_token": [-1_0_0] * len(_snake_case			),
										        "end_token": [-1_0_0] * len(_snake_case			),
										        "category": category,
										    },
										}

					snake_case_					:		Dict 					=					out["""context"""].split()
					snake_case_					:		Union[str, Any] 					=					splitted_context[answer["""end_token"""]]
					snake_case_					:		int 					=					len(
					    tokenizer(
					        """ """.join(splitted_context[: answer["""start_token"""]]			)				,    add_special_tokens=_snake_case				,    ).input_ids			)
					snake_case_					:		List[Any] 					=					len(
					    tokenizer(""" """.join(splitted_context[: answer["""end_token"""]]			)				,    add_special_tokens=_snake_case			).input_ids			)

					answer["start_token"] += q_len
					answer["end_token"] += q_len

					# fixing end token
					snake_case_					:		str 					=					len(tokenizer(_snake_case				,    add_special_tokens=_snake_case			).input_ids			)
					if num_sub_tokens > 1:
										answer["end_token"] += num_sub_tokens - 1

					snake_case_					:		List[str] 					=					input_ids[answer["""start_token"""] : answer["""end_token"""] + 1]  # right & left are inclusive
					snake_case_					:		str 					=					answer["""start_token"""]
					snake_case_					:		Any 					=					answer["""end_token"""]

					if assertion:
										snake_case_					:		Optional[Any] 					=					tokenizer.decode(_snake_case			)
										if answer["span"] != new:
															print("""ISSUE IN TOKENIZATION"""			)
															print("""OLD:"""				,    answer["""span"""]			)
															print("""NEW:"""				,    _snake_case				,    end="""\n\n"""			)

					if len(_snake_case			) <= max_length:
										return {
										    "example_id": example["id"],
										    "input_ids": [input_ids],
										    "labels": {
										        "start_token": [answer["start_token"]],
										        "end_token": [answer["end_token"]],
										        "category": answer["category"],
										    },
										}

					snake_case_					:		Optional[Any] 					=					input_ids[:q_len]
					snake_case_					:		Union[str, Any] 					=					range(_snake_case				,    len(_snake_case			)				,    max_length - doc_stride			)

					snake_case_					:		str 					=					[]
					snake_case_					:		Union[str, Any] 					=					[]
					snake_case_					:		Tuple 					=					[]
					snake_case_					:		Dict 					=					[]  # null, yes, no, long, short
					for i in doc_start_indices:
										snake_case_					:		List[Any] 					=					i + max_length - q_len
										snake_case_					:		str 					=					input_ids[i:end_index]
										inputs.append(q_indices + slice			)
										assert len(inputs[-1]			) <= max_length, "Issue in truncating length"

										if start_token >= i and end_token <= end_index - 1:
															snake_case_					:		List[str] 					=					start_token - i + q_len
															snake_case_					:		List[str] 					=					end_token - i + q_len
															answers_category.append(answer["""category"""][0]			)  # ["short"] -> "short"
										else:
															snake_case_					:		str 					=					-1_0_0
															snake_case_					:		List[Any] 					=					-1_0_0
															answers_category.append("""null"""			)
										snake_case_					:		Union[str, Any] 					=					inputs[-1][start_token : end_token + 1]

										answers_start_token.append(_snake_case			)
										answers_end_token.append(_snake_case			)
										if assertion:
															if new != old and new != [tokenizer.cls_token_id]:
																				print("""ISSUE in strided for ID:"""				,    example["""id"""]			)
																				print("""New:"""				,    tokenizer.decode(_snake_case			)			)
																				print("""Old:"""				,    tokenizer.decode(_snake_case			)				,    end="""\n\n"""			)
										if slice[-1] == tokenizer.sep_token_id:
															break

					return {
					    "example_id": example["id"],
					    "input_ids": inputs,
					    "labels": {
					        "start_token": answers_start_token,
					        "end_token": answers_end_token,
					        "category": answers_category,
					    },
					}







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  List[Any]				,    SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  Tuple=2_0_4_8				,    SCREAMING_SNAKE_CASE__ :  str=4_0_9_6				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]=False			):



					"""simple docstring"""





					snake_case_					:		Any 					=					get_strided_contexts_and_ans(
					    _snake_case				,    _snake_case				,    doc_stride=_snake_case				,    max_length=_snake_case				,    assertion=_snake_case				,    )

					return example







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  Optional[int]			):



					"""simple docstring"""





					with jsonlines.open(_snake_case				,    """a"""			) as writer:
										for example in tqdm(_snake_case				,    total=len(_snake_case			)				,    desc="""Saving samples ... """			):
															snake_case_					:		Tuple 					=					example["""labels"""]
															for ids, start, end, cat in zip(
															    example["""input_ids"""]				,    labels["""start_token"""]				,    labels["""end_token"""]				,    labels["""category"""]				,    ):
																				if start == -1 and end == -1:
																									continue  # leave waste samples with no answer
																				if cat == "null" and np.random.rand() < 0.6:
																									continue  # removing 50 % samples
																				writer.write(
																				    {
																				        """input_ids""": ids,
																				        """start_token""": start,
																				        """end_token""": end,
																				        """category""": CATEGORY_MAPPING[cat],
																				    }			)


if __name__ == "__main__":
			from datasets import load_dataset

			from transformers import BigBirdTokenizer

			a_			   =					load_dataset('''natural_questions''')
			a_			   =					BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''')

			a_			   =					data['''train''' if PROCESS_TRAIN == '''true''' else '''validation''']

			a_			   =					{
			    '''tokenizer''': tokenizer,
			    '''doc_stride''': DOC_STRIDE,
			    '''max_length''': MAX_LENGTH,
			    '''assertion''': False,
			}
			a_			   =					data.map(prepare_inputs, fn_kwargs=fn_kwargs)
			a_			   =					data.remove_columns(['''annotations''', '''document''', '''id''', '''question'''])
			print(data)

			np.random.seed(SEED)
			a_			   =					'''nq-training.jsonl''' if PROCESS_TRAIN == '''true''' else '''nq-validation.jsonl'''
			save_to_disk(data, file_name=cache_file_name)










"""simple docstring"""

import random







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int			):



					"""simple docstring"""





					snake_case_					:		Union[str, Any] 					=					num - 1
					snake_case_					:		List[str] 					=					0

					while s % 2 == 0:
										snake_case_					:		str 					=					s // 2
										t += 1

					for _ in range(5			):
										snake_case_					:		List[Any] 					=					random.randrange(2				,    num - 1			)
										snake_case_					:		Dict 					=					pow(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)
										if v != 1:
															snake_case_					:		int 					=					0
															while v != (num - 1):
																				if i == t - 1:
																									return False
																				else:
																									snake_case_					:		str 					=					i + 1
																									snake_case_					:		int 					=					(v**2) % num
					return True







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int			):



					"""simple docstring"""





					if num < 2:
										return False

					snake_case_					:		Dict 					=					[
					    2,
					    3,
					    5,
					    7,
					    1_1,
					    1_3,
					    1_7,
					    1_9,
					    2_3,
					    2_9,
					    3_1,
					    3_7,
					    4_1,
					    4_3,
					    4_7,
					    5_3,
					    5_9,
					    6_1,
					    6_7,
					    7_1,
					    7_3,
					    7_9,
					    8_3,
					    8_9,
					    9_7,
					    1_0_1,
					    1_0_3,
					    1_0_7,
					    1_0_9,
					    1_1_3,
					    1_2_7,
					    1_3_1,
					    1_3_7,
					    1_3_9,
					    1_4_9,
					    1_5_1,
					    1_5_7,
					    1_6_3,
					    1_6_7,
					    1_7_3,
					    1_7_9,
					    1_8_1,
					    1_9_1,
					    1_9_3,
					    1_9_7,
					    1_9_9,
					    2_1_1,
					    2_2_3,
					    2_2_7,
					    2_2_9,
					    2_3_3,
					    2_3_9,
					    2_4_1,
					    2_5_1,
					    2_5_7,
					    2_6_3,
					    2_6_9,
					    2_7_1,
					    2_7_7,
					    2_8_1,
					    2_8_3,
					    2_9_3,
					    3_0_7,
					    3_1_1,
					    3_1_3,
					    3_1_7,
					    3_3_1,
					    3_3_7,
					    3_4_7,
					    3_4_9,
					    3_5_3,
					    3_5_9,
					    3_6_7,
					    3_7_3,
					    3_7_9,
					    3_8_3,
					    3_8_9,
					    3_9_7,
					    4_0_1,
					    4_0_9,
					    4_1_9,
					    4_2_1,
					    4_3_1,
					    4_3_3,
					    4_3_9,
					    4_4_3,
					    4_4_9,
					    4_5_7,
					    4_6_1,
					    4_6_3,
					    4_6_7,
					    4_7_9,
					    4_8_7,
					    4_9_1,
					    4_9_9,
					    5_0_3,
					    5_0_9,
					    5_2_1,
					    5_2_3,
					    5_4_1,
					    5_4_7,
					    5_5_7,
					    5_6_3,
					    5_6_9,
					    5_7_1,
					    5_7_7,
					    5_8_7,
					    5_9_3,
					    5_9_9,
					    6_0_1,
					    6_0_7,
					    6_1_3,
					    6_1_7,
					    6_1_9,
					    6_3_1,
					    6_4_1,
					    6_4_3,
					    6_4_7,
					    6_5_3,
					    6_5_9,
					    6_6_1,
					    6_7_3,
					    6_7_7,
					    6_8_3,
					    6_9_1,
					    7_0_1,
					    7_0_9,
					    7_1_9,
					    7_2_7,
					    7_3_3,
					    7_3_9,
					    7_4_3,
					    7_5_1,
					    7_5_7,
					    7_6_1,
					    7_6_9,
					    7_7_3,
					    7_8_7,
					    7_9_7,
					    8_0_9,
					    8_1_1,
					    8_2_1,
					    8_2_3,
					    8_2_7,
					    8_2_9,
					    8_3_9,
					    8_5_3,
					    8_5_7,
					    8_5_9,
					    8_6_3,
					    8_7_7,
					    8_8_1,
					    8_8_3,
					    8_8_7,
					    9_0_7,
					    9_1_1,
					    9_1_9,
					    9_2_9,
					    9_3_7,
					    9_4_1,
					    9_4_7,
					    9_5_3,
					    9_6_7,
					    9_7_1,
					    9_7_7,
					    9_8_3,
					    9_9_1,
					    9_9_7,
					]

					if num in low_primes:
										return True

					for prime in low_primes:
										if (num % prime) == 0:
															return False

					return rabin_miller(SCREAMING_SNAKE_CASE__			)







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int = 1_0_2_4			):



					"""simple docstring"""





					while True:
										snake_case_					:		Tuple 					=					random.randrange(2 ** (keysize - 1)				,    2 ** (keysize)			)
										if is_prime_low_num(SCREAMING_SNAKE_CASE__			):
															return num


if __name__ == "__main__":
			a_			   =					generate_large_prime()
			print(('''Prime number:''', num))
			print(('''is_prime_low_num:''', is_prime_low_num(num)))




