





'''simple docstring'''




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


__SCREAMING_SNAKE_CASE     :Union[str, Any]       =				logging.get_logger(__name__)







class   A_	(  __lowerCAmelCase			):




 def __init__(					self       :    Union[str, Any]	,			**snake_case_       :    Tuple    ):
      requires_backends(self	,			["bs4"]    )
      super().__init__(**snake_case_    )




 def      lowercase	(					self       :    Dict	,			snake_case_       :    List[Any]    ):
      _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=snake_case_    )
           xpath_tags.append(child.name    )
           xpath_subscripts.append(
               0 if 1 == len(snake_case_    ) else next(i for i, s in enumerate(snake_case_	,			1    ) if s is child    )    )
           _UpperCAmelCase     							=   parent
      xpath_tags.reverse()
      xpath_subscripts.reverse()
      return xpath_tags, xpath_subscripts




 def      lowercase	(					self       :    List[Any]	,			snake_case_       :    Optional[Any]    ):
      _UpperCAmelCase     							=   BeautifulSoup(snake_case_	,			"html.parser"    )

      _UpperCAmelCase     							=   []
      _UpperCAmelCase     							=   []
      _UpperCAmelCase     							=   []

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

                _UpperCAmelCase     							=   html.unescape(snake_case_    ).strip()
                if not text_in_this_tag:
                     continue

                all_doc_strings.append(snake_case_    )

                _UpperCAmelCase     							=   self.xpath_soup(snake_case_    )
                stringaxtag_seq.append(snake_case_    )
                stringaxsubs_seq.append(snake_case_    )

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

      return all_doc_strings, stringaxtag_seq, stringaxsubs_seq




 def      lowercase	(					self       :    Optional[Any]	,			snake_case_       :    List[str]	,			snake_case_       :    Optional[int]    ):
      _UpperCAmelCase     							=   ''
      for tagname, subs in zip(snake_case_	,			snake_case_    ):
           xpath += f'/{tagname}'
           if subs != 0:
                xpath += f'[{subs}]'
      return xpath




 def __call__(					self       :    Dict	,			snake_case_       :    int    ):
      _UpperCAmelCase     							=   False

      # Check that strings has a valid type
      if isinstance(snake_case_	,			snake_case_    ):
           _UpperCAmelCase     							=   True
      elif isinstance(snake_case_	,			(list, tuple)    ):
           if len(snake_case_    ) == 0 or isinstance(html_strings[0]	,			snake_case_    ):
                _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(snake_case_    )}.'    )

      _UpperCAmelCase     							=   bool(isinstance(snake_case_	,			(list, tuple)    ) and (isinstance(html_strings[0]	,			snake_case_    ))    )

      if not is_batched:
           _UpperCAmelCase     							=   [html_strings]

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

      # return as Dict
      _UpperCAmelCase     							=   {'nodes': nodes, 'xpaths': xpaths}
      _UpperCAmelCase     							=   BatchFeature(data=snake_case_	,			tensor_type=snake_case_    )

      return encoded_inputs





"""simple docstring"""
def 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->int:
  while a != 0:
    a__							,    a__:     List[str]										=	b % a, a
  return b







def 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->int:
  if gcd(_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   ) != 1:
    a__:     Dict										=	F'mod inverse of {a!r} and {m!r} does not exist'
    raise ValueError(_SCREAMING_SNAKE_CASE   )
  a__							,    a__							,    a__:     Union[str, Any]										=	1, 0, a
  a__							,    a__							,    a__:     Any										=	0, 1, m
  while va != 0:
    a__:     int										=	ua // va
    a__							,    a__							,    a__							,    a__							,    a__							,    a__:     Any										=	(ua - q * va), (ua - q * va), (ua - q * va), va, va, va
  return ua % m


