





'''simple docstring'''




import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


__UpperCAmelCase			    =    logging.get_logger(__name__)



def 						__A						(   lowerCamelCase_     ):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       R"""\w+[.]\d+"""
		SCREAMING_SNAKE_CASE      :							int						=       re.findall(lowerCamelCase_       ,       lowerCamelCase_     )
		for pat in pats:
				SCREAMING_SNAKE_CASE      :							List[str]						=       key.replace(lowerCamelCase_       ,       """_""".join(pat.split("""."""     )     )     )
		return key



def 						__A						(   lowerCamelCase_       ,       lowerCamelCase_       ,       lowerCamelCase_     ):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE      :							Optional[Any]						=       pt_tuple_key[:-1] + ("""scale""",)
		if (
		    any("""norm""" in str_ for str_ in pt_tuple_key     )
		    and (pt_tuple_key[-1] == "bias")
		    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
		    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
		):
				SCREAMING_SNAKE_CASE      :							List[str]						=       pt_tuple_key[:-1] + ("""scale""",)
				return renamed_pt_tuple_key, pt_tensor
		elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
				SCREAMING_SNAKE_CASE      :							Optional[Any]						=       pt_tuple_key[:-1] + ("""scale""",)
				return renamed_pt_tuple_key, pt_tensor

		# embedding
		if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
				SCREAMING_SNAKE_CASE      :							Optional[Any]						=       pt_tuple_key[:-1] + ("""embedding""",)
				return renamed_pt_tuple_key, pt_tensor

		# conv layer
		SCREAMING_SNAKE_CASE      :							Dict						=       pt_tuple_key[:-1] + ("""kernel""",)
		if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
				SCREAMING_SNAKE_CASE      :							Any						=       pt_tensor.transpose(2       ,       3       ,       1       ,       0     )
				return renamed_pt_tuple_key, pt_tensor

		# linear layer
		SCREAMING_SNAKE_CASE      :							Dict						=       pt_tuple_key[:-1] + ("""kernel""",)
		if pt_tuple_key[-1] == "weight":
				SCREAMING_SNAKE_CASE      :							Optional[Any]						=       pt_tensor.T
				return renamed_pt_tuple_key, pt_tensor

		# old PyTorch layer norm weight
		SCREAMING_SNAKE_CASE      :							str						=       pt_tuple_key[:-1] + ("""weight""",)
		if pt_tuple_key[-1] == "gamma":
				return renamed_pt_tuple_key, pt_tensor

		# old PyTorch layer norm bias
		SCREAMING_SNAKE_CASE      :							Optional[int]						=       pt_tuple_key[:-1] + ("""bias""",)
		if pt_tuple_key[-1] == "beta":
				return renamed_pt_tuple_key, pt_tensor

		return pt_tuple_key, pt_tensor



def 						__A						(   lowerCamelCase_       ,       lowerCamelCase_       ,       lowerCamelCase_=42     ):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE      :							Any						=       {k: v.numpy() for k, v in pt_state_dict.items()}

		# Step 2: Since the model is stateless, get random Flax params
		SCREAMING_SNAKE_CASE      :							Tuple						=       flax_model.init_weights(PRNGKey(lowerCamelCase_     )     )

		SCREAMING_SNAKE_CASE      :							Any						=       flatten_dict(lowerCamelCase_     )
		SCREAMING_SNAKE_CASE      :							Optional[int]						=       {}

		# Need to change some parameters name to match Flax names
		for pt_key, pt_tensor in pt_state_dict.items():
				SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       rename_key(lowerCamelCase_     )
				SCREAMING_SNAKE_CASE      :							Optional[Any]						=       tuple(renamed_pt_key.split("""."""     )     )

				# Correctly rename weight parameters
				SCREAMING_SNAKE_CASE      :							Tuple						=       rename_key_and_reshape_tensor(lowerCamelCase_       ,       lowerCamelCase_       ,       lowerCamelCase_     )

				if flax_key in random_flax_state_dict:
						if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
								raise ValueError(
								    f'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape '''
								    f'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.'''     )

        # also add unexpected weight so that warning is thrown
				SCREAMING_SNAKE_CASE      :							List[Any]						=       jnp.asarray(lowerCamelCase_     )

		return unflatten_dict(lowerCamelCase_     )











import html

from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from ...utils import is_bsa_available, logging, requires_backends


if is_bsa_available():
	import bsa
	from bsa import BeautifulSoup


UpperCAmelCase_ :						Any      =      logging.get_logger(__name__)

class     _SCREAMING_SNAKE_CASE       (			_a      ):





	def __init__(		self				:					Optional[int] ,  **__lowerCamelCase				:					Optional[int]	):
		requires_backends(self ,  ["""bs4"""]	)
		super().__init__(**__lowerCamelCase	)





	def 						_A (		self				:					List[str] ,  __lowerCamelCase				:					Any	):
		UpperCamelCase     :Optional[int]          =	[]
		UpperCamelCase     :List[str]          =	[]
		UpperCamelCase     :Union[str, Any]          =	element if element.name else element.parent
		for parent in child.parents:  # type: bs4.element.Tag
			UpperCamelCase     :Optional[Any]          =	parent.find_all(child.name ,  recursive=__lowerCamelCase	)
			xpath_tags.append(child.name	)
			xpath_subscripts.append(
			    0 if 1 == len(__lowerCamelCase	) else next(i for i, s in enumerate(__lowerCamelCase ,  1	) if s is child	)	)
			UpperCamelCase     :Any          =	parent
		xpath_tags.reverse()
		xpath_subscripts.reverse()
		return xpath_tags, xpath_subscripts





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					Tuple	):
		UpperCamelCase     :Any          =	BeautifulSoup(__lowerCamelCase ,  """html.parser"""	)

		UpperCamelCase     :Union[str, Any]          =	[]
		UpperCamelCase     :Tuple          =	[]
		UpperCamelCase     :Tuple          =	[]

		for element in html_code.descendants:
			if type(__lowerCamelCase	) == bsa.element.NavigableString:
				if type(element.parent	) != bsa.element.Tag:
					continue

				UpperCamelCase     :Any          =	html.unescape(__lowerCamelCase	).strip()
				if not text_in_this_tag:
					continue

				all_doc_strings.append(__lowerCamelCase	)

				UpperCamelCase     ,      UpperCamelCase     :Optional[Any]          =	self.xpath_soup(__lowerCamelCase	)
				stringaxtag_seq.append(__lowerCamelCase	)
				stringaxsubs_seq.append(__lowerCamelCase	)

		if len(__lowerCamelCase	) != len(__lowerCamelCase	):
			raise ValueError("""Number of doc strings and xtags does not correspond"""	)
		if len(__lowerCamelCase	) != len(__lowerCamelCase	):
			raise ValueError("""Number of doc strings and xsubs does not correspond"""	)

		return all_doc_strings, stringaxtag_seq, stringaxsubs_seq





	def 						_A (		self				:					int ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					List[str]	):
		UpperCamelCase     :Tuple          =	""""""
		for tagname, subs in zip(__lowerCamelCase ,  __lowerCamelCase	):
			xpath += F"""/{tagname}"""
			if subs != 0:
				xpath += F"""[{subs}]"""
		return xpath





	def __call__(		self				:					Any ,  __lowerCamelCase				:					Dict	):
		UpperCamelCase     :Any          =	False

		# Check that strings has a valid type
		if isinstance(__lowerCamelCase ,  __lowerCamelCase	):
			UpperCamelCase     :List[Any]          =	True
		elif isinstance(__lowerCamelCase ,  (list, tuple)	):
			if len(__lowerCamelCase	) == 0 or isinstance(html_strings[0] ,  __lowerCamelCase	):
				UpperCamelCase     :Any          =	True

		if not valid_strings:
			raise ValueError(
			    """HTML strings must of type `str`, `List[str]` (batch of examples), """
			    F"""but is of type {type(__lowerCamelCase	)}."""	)

		UpperCamelCase     :str          =	bool(isinstance(__lowerCamelCase ,  (list, tuple)	) and (isinstance(html_strings[0] ,  __lowerCamelCase	))	)

		if not is_batched:
			UpperCamelCase     :Any          =	[html_strings]

		# Get nodes + xpaths
		UpperCamelCase     :Union[str, Any]          =	[]
		UpperCamelCase     :str          =	[]
		for html_string in html_strings:
			UpperCamelCase     ,      UpperCamelCase     ,      UpperCamelCase     :int          =	self.get_three_from_single(__lowerCamelCase	)
			nodes.append(__lowerCamelCase	)
			UpperCamelCase     :int          =	[]
			for node, tag_list, sub_list in zip(__lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase	):
				UpperCamelCase     :str          =	self.construct_xpath(__lowerCamelCase ,  __lowerCamelCase	)
				xpath_strings.append(__lowerCamelCase	)
			xpaths.append(__lowerCamelCase	)

		# return as Dict
		UpperCamelCase     :Optional[int]          =	{"""nodes""": nodes, """xpaths""": xpaths}
		UpperCamelCase     :Any          =	BatchFeature(data=__lowerCamelCase ,  tensor_type=__lowerCamelCase	)

		return encoded_inputs
