



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


__lowerCamelCase     :							Optional[Any]  							=				logging.get_logger(__name__)





class  __magic_name__   (							__snake_case						):






		def __init__(						self      :     Optional[Any]				,		**UpperCamelCase__      :     Optional[Any]      )				->					Dict:
								'''simple docstring'''







								requires_backends(self				,		["bs4"]      )
								super().__init__(**__UpperCamelCase      )






		def SCREAMING_SNAKE_CASE_			(						self      :     int				,		UpperCamelCase__      :     Tuple      )				->					str:
								'''simple docstring'''







								UpperCAmelCase         =     []
								UpperCAmelCase         =     []
								UpperCAmelCase         =     element if element.name else element.parent
								for parent in child.parents:  # type: bs4.element.Tag
														UpperCAmelCase         =     parent.find_all(child.name				,		recursive=__UpperCamelCase      )
														xpath_tags.append(child.name      )
														xpath_subscripts.append(
														    0 if 1 == len(__UpperCamelCase      ) else next(i for i, s in enumerate(__UpperCamelCase				,		1      ) if s is child      )      )
														UpperCAmelCase         =     parent
								xpath_tags.reverse()
								xpath_subscripts.reverse()
								return xpath_tags, xpath_subscripts






		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]				,		UpperCamelCase__      :     Union[str, Any]      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =     BeautifulSoup(__UpperCamelCase				,		"html.parser"      )

								UpperCAmelCase         =     []
								UpperCAmelCase         =     []
								UpperCAmelCase         =     []

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

																				UpperCAmelCase         =     html.unescape(__UpperCamelCase      ).strip()
																				if not text_in_this_tag:
																										continue

																				all_doc_strings.append(__UpperCamelCase      )

																				UpperCAmelCase       ,		UpperCAmelCase         =     self.xpath_soup(__UpperCamelCase      )
																				stringaxtag_seq.append(__UpperCamelCase      )
																				stringaxsubs_seq.append(__UpperCamelCase      )

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

								return all_doc_strings, stringaxtag_seq, stringaxsubs_seq






		def SCREAMING_SNAKE_CASE_			(						self      :     Any				,		UpperCamelCase__      :     List[Any]				,		UpperCamelCase__      :     Dict      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     ""
								for tagname, subs in zip(__UpperCamelCase				,		__UpperCamelCase      ):
														xpath += F'/{tagname}'
														if subs != 0:
																				xpath += F'[{subs}]'
								return xpath







		def __call__(						self      :     List[str]				,		UpperCamelCase__      :     Any      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     False

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

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

								UpperCAmelCase         =     bool(isinstance(__UpperCamelCase				,		(list, tuple)      ) and (isinstance(html_strings[0]				,		__UpperCamelCase      ))      )

								if not is_batched:
														UpperCAmelCase         =     [html_strings]

								# Get nodes + xpaths
								UpperCAmelCase         =     []
								UpperCAmelCase         =     []
								for html_string in html_strings:
														UpperCAmelCase       ,		UpperCAmelCase       ,		UpperCAmelCase         =     self.get_three_from_single(__UpperCamelCase      )
														nodes.append(__UpperCamelCase      )
														UpperCAmelCase         =     []
														for node, tag_list, sub_list in zip(__UpperCamelCase				,		__UpperCamelCase				,		__UpperCamelCase      ):
																				UpperCAmelCase         =     self.construct_xpath(__UpperCamelCase				,		__UpperCamelCase      )
																				xpath_strings.append(__UpperCamelCase      )
														xpaths.append(__UpperCamelCase      )

								# return as Dict
								UpperCAmelCase         =     {"nodes": nodes, "xpaths": xpaths}
								UpperCAmelCase         =     BatchFeature(data=__UpperCamelCase				,		tensor_type=__UpperCamelCase      )

								return encoded_inputs








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__lowerCamelCase     :							Any  							=				{
    "configuration_mega": ["MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP", "MegaConfig", "MegaOnnxConfig"],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCamelCase     :							Optional[int]  							=				[
		    "MEGA_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "MegaForCausalLM",
		    "MegaForMaskedLM",
		    "MegaForMultipleChoice",
		    "MegaForQuestionAnswering",
		    "MegaForSequenceClassification",
		    "MegaForTokenClassification",
		    "MegaModel",
		    "MegaPreTrainedModel",
		]

if TYPE_CHECKING:
		from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mega import (
				    MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MegaForCausalLM,
				    MegaForMaskedLM,
				    MegaForMultipleChoice,
				    MegaForQuestionAnswering,
				    MegaForSequenceClassification,
				    MegaForTokenClassification,
				    MegaModel,
				    MegaPreTrainedModel,
				)

else:
		import sys

		__lowerCamelCase     :							int  							=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



