


"""simple docstring"""







from __future__ import annotations
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       List[str]    ,							UpperCAmelCase_					:       List[Any] = None    ,							UpperCAmelCase_					:       List[str] = None    ,							UpperCAmelCase_					:       Optional[int] = False    ,							)							->    Union[str, Any]:

	'''simple docstring'''





	__snake_case    :							List[str]   	=				cipher_alphabet or [chr(UpperCAmelCase_						) for i in range(97    ,							1_23						)]

	# If the argument is None or the user provided an empty dictionary
	if not frequencies_dict:
		# Frequencies of letters in the english language (how much they show up)
		__snake_case    :							Optional[int]   	=				{
		    'a': 0.08_497,
		    'b': 0.01_492,
		    'c': 0.02_202,
		    'd': 0.04_253,
		    'e': 0.11_162,
		    'f': 0.02_228,
		    'g': 0.02_015,
		    'h': 0.06_094,
		    'i': 0.07_546,
		    'j': 0.00_153,
		    'k': 0.01_292,
		    'l': 0.04_025,
		    'm': 0.02_406,
		    'n': 0.06_749,
		    'o': 0.07_507,
		    'p': 0.01_929,
		    'q': 0.00_095,
		    'r': 0.07_587,
		    's': 0.06_327,
		    't': 0.09_356,
		    'u': 0.02_758,
		    'v': 0.00_978,
		    'w': 0.02_560,
		    'x': 0.00_150,
		    'y': 0.01_994,
		    'z': 0.00_077,
		}
	else:
		# Custom frequencies dictionary
		__snake_case    :							Optional[Any]   	=				frequencies_dict

	if not case_sensitive:
		__snake_case    :							Union[str, Any]   	=				ciphertext.lower()

	# Chi squared statistic values
	__snake_case    :							Optional[Any]   	=				{}

	# cycle through all of the shifts
	for shift in range(len(UpperCAmelCase_						)						):
		__snake_case    :							Tuple   	=				''

		# decrypt the message with the shift
		for letter in ciphertext:
			try:
				# Try to index the letter in the alphabet
				__snake_case    :							int   	=				(alphabet_letters.index(letter.lower()						) - shift) % len(
				    UpperCAmelCase_						)
				decrypted_with_shift += (
				    alphabet_letters[new_key].upper()
				    if case_sensitive and letter.isupper()
				    else alphabet_letters[new_key]
				)
			except ValueError:
				# Append the character if it isn't in the alphabet
				decrypted_with_shift += letter

		__snake_case    :							int   	=				0.0

		# Loop through each letter in the decoded message with the shift
		for letter in decrypted_with_shift:
			if case_sensitive:
				__snake_case    :							Any   	=				letter.lower()
				if letter in frequencies:
					# Get the amount of times the letter occurs in the message
					__snake_case    :							str   	=				decrypted_with_shift.lower().count(UpperCAmelCase_						)

					# Get the excepcted amount of times the letter should appear based
					# on letter frequencies
					__snake_case    :							Dict   	=				frequencies[letter] * occurrences

					# Complete the chi squared statistic formula
					__snake_case    :							Optional[int]   	=				((occurrences - expected) ** 2) / expected

					# Add the margin of error to the total chi squared statistic
					chi_squared_statistic += chi_letter_value
			else:
				if letter.lower() in frequencies:
					# Get the amount of times the letter occurs in the message
					__snake_case    :							Union[str, Any]   	=				decrypted_with_shift.count(UpperCAmelCase_						)

					# Get the excepcted amount of times the letter should appear based
					# on letter frequencies
					__snake_case    :							Tuple   	=				frequencies[letter] * occurrences

					# Complete the chi squared statistic formula
					__snake_case    :							Tuple   	=				((occurrences - expected) ** 2) / expected

					# Add the margin of error to the total chi squared statistic
					chi_squared_statistic += chi_letter_value

        # Add the data to the chi_squared_statistic_values dictionary
		__snake_case    :							Dict   	=				(
		    chi_squared_statistic,
		    decrypted_with_shift,
		)

	# Get the most likely cipher by finding the cipher with the smallest chi squared
	# statistic
	def chi_squared_statistic_values_sorting_key(UpperCAmelCase_					:       Any						) -> tuple[float, str]:
		return chi_squared_statistic_values[key]

	__snake_case    :							Tuple   	=				min(
	    UpperCAmelCase_    ,							key=UpperCAmelCase_    ,							)

	# Get all the data from the most likely cipher (key, decoded message)
	(
	    (
	    __snake_case						
	)	,       (
	    __snake_case						
	)	,       						
	)    :							str   	=				chi_squared_statistic_values[most_likely_cipher]

	# Return the data on the most likely shift
	return (
	    most_likely_cipher,
	    most_likely_cipher_chi_squared_value,
	    decoded_most_likely_cipher,
	)






"""simple docstring"""







import math

import tensorflow as tf
from packaging import version
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Union[str, Any]						)							->    Any:

	'''simple docstring'''





	__snake_case    :							List[str]   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							List[str]   	=				0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)    ,							x.dtype						)						))

	return x * cdf
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    Optional[Any]:

	'''simple docstring'''





	__snake_case    :							Optional[int]   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							Any   	=				tf.cast(math.pi    ,							x.dtype						)
	__snake_case    :							List[Any]   	=				tf.cast(0.044_715    ,							x.dtype						)
	__snake_case    :							Union[str, Any]   	=				0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(UpperCAmelCase_    ,							3						))						))

	return x * cdf
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    List[Any]:

	'''simple docstring'''





	__snake_case    :							Optional[int]   	=				tf.convert_to_tensor(UpperCAmelCase_						)

	return x * tf.tanh(tf.math.softplus(UpperCAmelCase_						)						)
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    List[str]:

	'''simple docstring'''





	__snake_case    :							Union[str, Any]   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							Dict   	=				tf.cast(0.044_715    ,							x.dtype						)
	__snake_case    :							int   	=				tf.cast(0.7_978_845_608    ,							x.dtype						)

	return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       int						)							->    Dict:

	'''simple docstring'''





	__snake_case    :							Any   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							Dict   	=				tf.cast(1.702    ,							x.dtype						)
	return x * tf.math.sigmoid(coeff * x						)
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Union[str, Any]						)							->    List[Any]:

	'''simple docstring'''





	return tf.clip_by_value(_gelu(UpperCAmelCase_						)    ,							-10    ,							10						)
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Union[str, Any]    ,							UpperCAmelCase_					:       Any=-1						)							->    int:

	'''simple docstring'''





	__snake_case	,       __snake_case    :							str   	=				tf.split(UpperCAmelCase_    ,							2    ,							axis=UpperCAmelCase_						)
	return a * tf.math.sigmoid(UpperCAmelCase_						)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):
	def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    int:

		'''simple docstring'''





		return tf.keras.activations.gelu(UpperCAmelCase_    ,							approximate=UpperCAmelCase_						)

	_a  :  Tuple=				tf.keras.activations.gelu

	_a  :  Tuple=				approximate_gelu_wrap
else:
	_a  :  Any=				_gelu
	_a  :  str=				_gelu_new


_a  :  List[str]=				{
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}

def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Optional[Any]						)							->    List[Any]:

	'''simple docstring'''





	if activation_string in ACTaFN:
		return ACTaFN[activation_string]
	else:
		raise KeyError(F"function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}"						)





