


import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer



class     snake_case__  (      tf.keras.layers.Layer  ):
				def __init__(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase = None     ,      _UpperCAmelCase = None     ,      _UpperCAmelCase = None     ,      _UpperCAmelCase = "longest"     ,      _UpperCAmelCase = True     ,      _UpperCAmelCase = 512     ,      _UpperCAmelCase = None     ,      _UpperCAmelCase = True     ,      _UpperCAmelCase = True     ,      _UpperCAmelCase = True     ,      **_UpperCAmelCase     ,      ):




										"""simple docstring"""




										super().__init__()
										if use_fast_bert_tokenizer:
																__magic_name__				:							int				=      FastBertTokenizer(
																    _UpperCAmelCase     ,      token_out_type=tf.intaa     ,      lower_case_nfd_strip_accents=_UpperCAmelCase     ,      **_UpperCAmelCase      )
										else:
																__magic_name__				:							List[Any]				=      tf.lookup.StaticVocabularyTable(
																    tf.lookup.KeyValueTensorInitializer(
																        keys=_UpperCAmelCase     ,      key_dtype=tf.string     ,      values=tf.range(tf.size(_UpperCAmelCase     ,      out_type=tf.intaa      )     ,      dtype=tf.intaa      )     ,      value_dtype=tf.intaa     ,      )     ,      num_oov_buckets=1     ,      )
																__magic_name__				:							List[Any]				=      BertTokenizerLayer(
																    _UpperCAmelCase     ,      token_out_type=tf.intaa     ,      lower_case=_UpperCAmelCase     ,      **_UpperCAmelCase      )

										__magic_name__				:							Dict				=      vocab_list
										__magic_name__				:							int				=      do_lower_case
										__magic_name__				:							int				=      cls_token_id or vocab_list.index('[CLS]'      )
										__magic_name__				:							Optional[Any]				=      sep_token_id or vocab_list.index('[SEP]'      )
										__magic_name__				:							Tuple				=      pad_token_id or vocab_list.index('[PAD]'      )
										__magic_name__				:							Dict				=      ShrinkLongestTrimmer(max_length - 3     ,      axis=1      )  # Allow room for special tokens
										__magic_name__				:							int				=      max_length
										__magic_name__				:							int				=      padding
										__magic_name__				:							int				=      truncation
										__magic_name__				:							int				=      pad_to_multiple_of
										__magic_name__				:							Optional[Any]				=      return_token_type_ids
										__magic_name__				:							Optional[Any]				=      return_attention_mask
				@classmethod
				def  UpperCamelCase_						(  cls     ,      _UpperCAmelCase     ,      **_UpperCAmelCase      ):  # noqa: F821




										"""simple docstring"""




										__magic_name__				:							str				=      kwargs.pop('do_lower_case'     ,      _UpperCAmelCase      )
										__magic_name__				:							Tuple				=      tokenizer.do_lower_case if do_lower_case is None else do_lower_case
										__magic_name__				:							int				=      kwargs.pop('cls_token_id'     ,      _UpperCAmelCase      )
										__magic_name__				:							Any				=      tokenizer.cls_token_id if cls_token_id is None else cls_token_id
										__magic_name__				:							str				=      kwargs.pop('sep_token_id'     ,      _UpperCAmelCase      )
										__magic_name__				:							Tuple				=      tokenizer.sep_token_id if sep_token_id is None else sep_token_id
										__magic_name__				:							Any				=      kwargs.pop('pad_token_id'     ,      _UpperCAmelCase      )
										__magic_name__				:							Any				=      tokenizer.pad_token_id if pad_token_id is None else pad_token_id

										__magic_name__				:							int				=      tokenizer.get_vocab()
										__magic_name__				:							Union[str, Any]				=      sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]     ,      key=lambda _UpperCAmelCase      : x[1]      )
										__magic_name__				:							int				=      [entry[0] for entry in vocab]
										return cls(
										    vocab_list=_UpperCAmelCase     ,      do_lower_case=_UpperCAmelCase     ,      cls_token_id=_UpperCAmelCase     ,      sep_token_id=_UpperCAmelCase     ,      pad_token_id=_UpperCAmelCase     ,      **_UpperCAmelCase     ,      )
				@classmethod
				def  UpperCamelCase_						(  cls     ,      _UpperCAmelCase     ,      *_UpperCAmelCase     ,      **_UpperCAmelCase      ):




										"""simple docstring"""




										try:
																__magic_name__				:							Dict				=      BertTokenizer.from_pretrained(_UpperCAmelCase     ,      *_UpperCAmelCase     ,      **_UpperCAmelCase      )
										except:  # noqa: E722
																from .tokenization_bert_fast import BertTokenizerFast

																__magic_name__				:							List[str]				=      BertTokenizerFast.from_pretrained(_UpperCAmelCase     ,      *_UpperCAmelCase     ,      **_UpperCAmelCase      )
										return cls.from_tokenizer(_UpperCAmelCase     ,      **_UpperCAmelCase      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										if self.do_lower_case:
																__magic_name__				:							List[str]				=      case_fold_utfa(_UpperCAmelCase      )
										__magic_name__				:							List[Any]				=      self.tf_tokenizer.tokenize(_UpperCAmelCase      )
										return tokens.merge_dims(1     ,      -1      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase=None     ,      _UpperCAmelCase=None     ,      _UpperCAmelCase=None     ,      _UpperCAmelCase=None     ,      _UpperCAmelCase=None     ,      _UpperCAmelCase=None     ,      _UpperCAmelCase=None     ,      ):




										"""simple docstring"""




										if padding is None:
																__magic_name__				:							Optional[Any]				=      self.padding
										if padding not in ("longest", "max_length"):
																raise ValueError('Padding must be either \'longest\' or \'max_length\'!'      )
										if max_length is not None and text_pair is not None:
																# Because we have to instantiate a Trimmer to do it properly
																raise ValueError('max_length cannot be overridden at call time when truncating paired texts!'      )
										if max_length is None:
																__magic_name__				:							Union[str, Any]				=      self.max_length
										if truncation is None:
																__magic_name__				:							int				=      self.truncation
										if pad_to_multiple_of is None:
																__magic_name__				:							Optional[int]				=      self.pad_to_multiple_of
										if return_token_type_ids is None:
																__magic_name__				:							Tuple				=      self.return_token_type_ids
										if return_attention_mask is None:
																__magic_name__				:							Any				=      self.return_attention_mask
										if not isinstance(_UpperCAmelCase     ,      tf.Tensor      ):
																__magic_name__				:							Dict				=      tf.convert_to_tensor(_UpperCAmelCase      )
										if text_pair is not None and not isinstance(_UpperCAmelCase     ,      tf.Tensor      ):
																__magic_name__				:							str				=      tf.convert_to_tensor(_UpperCAmelCase      )
										if text_pair is not None:
																if text.shape.rank > 1:
																						raise ValueError('text argument should not be multidimensional when a text pair is supplied!'      )
																if text_pair.shape.rank > 1:
																						raise ValueError('text_pair should not be multidimensional!'      )
										if text.shape.rank == 2:
																__magic_name__		, __magic_name__				:							Optional[int]				=      text[:, 0], text[:, 1]
										__magic_name__				:							str				=      self.unpaired_tokenize(_UpperCAmelCase      )
										if text_pair is None:  # Unpaired text
																if truncation:
																						__magic_name__				:							Optional[Any]				=      text[:, : max_length - 2]  # Allow room for special tokens
																__magic_name__		, __magic_name__				:							int				=      combine_segments(
																    (text,)     ,      start_of_sequence_id=self.cls_token_id     ,      end_of_segment_id=self.sep_token_id      )
										else:  # Paired text
																__magic_name__				:							Tuple				=      self.unpaired_tokenize(_UpperCAmelCase      )
																if truncation:
																						__magic_name__		, __magic_name__				:							Union[str, Any]				=      self.paired_trimmer.trim([text, text_pair]      )
																__magic_name__		, __magic_name__				:							int				=      combine_segments(
																    (text, text_pair)     ,      start_of_sequence_id=self.cls_token_id     ,      end_of_segment_id=self.sep_token_id      )
										if padding == "longest":
																__magic_name__				:							str				=      input_ids.bounding_shape(axis=1      )
																if pad_to_multiple_of is not None:
																						# No ceiling division in tensorflow, so we negate floordiv instead
																						__magic_name__				:							Union[str, Any]				=      pad_to_multiple_of * (-tf.math.floordiv(-pad_length     ,      _UpperCAmelCase      ))
										else:
																__magic_name__				:							Optional[Any]				=      max_length

										__magic_name__		, __magic_name__				:							Tuple				=      pad_model_inputs(_UpperCAmelCase     ,      max_seq_length=_UpperCAmelCase     ,      pad_value=self.pad_token_id      )
										__magic_name__				:							str				=      {'input_ids': input_ids}
										if return_attention_mask:
																__magic_name__				:							List[str]				=      attention_mask
										if return_token_type_ids:
																__magic_name__		, __magic_name__				:							Optional[Any]				=      pad_model_inputs(
																    _UpperCAmelCase     ,      max_seq_length=_UpperCAmelCase     ,      pad_value=self.pad_token_id      )
																__magic_name__				:							Tuple				=      token_type_ids
										return output





				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										return {
										    "vocab_list": self.vocab_list,
										    "do_lower_case": self.do_lower_case,
										    "cls_token_id": self.cls_token_id,
										    "sep_token_id": self.sep_token_id,
										    "pad_token_id": self.pad_token_id,
										}






import dataclasses
import enum
import functools
import math
import re

# The following script is adapted from the script of TaPas.
# Original: https://github.com/google-research/tapas/master/wikisql_utils.py
from typing import Any, List, Text


snake_case__     							= 'none'
snake_case__     							= 'none'
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :List[str]      ,						lowercase__  :Optional[Any]				)       ->  List[str]:
						__magic_name__				:							Dict				=      value.split(lowercase__				)
						return len(lowercase__				) > 1 and any((len(lowercase__				) == 3 for x in split)				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :List[Any]				)       ->  List[str]:
						if isinstance(lowercase__      ,						lowercase__				):
												return value
						if isinstance(lowercase__      ,						lowercase__				):
												return float(lowercase__				)
						if not isinstance(lowercase__      ,						lowercase__				):
												raise ValueError('Argument value is not a string. Can\'t parse it as float'				)
						__magic_name__				:							List[str]				=      value

						try:
												# Example: 1,000.7
												if "." in sanitized and "," in sanitized:
																		return float(sanitized.replace(','      ,						''				)				)
												# 1,000
												if "," in sanitized and _split_thousands(','      ,						lowercase__				):
																		return float(sanitized.replace(','      ,						''				)				)
												# 5,5556
												if "," in sanitized and sanitized.count(','				) == 1 and not _split_thousands(','      ,						lowercase__				):
																		return float(sanitized.replace(','      ,						'.'				)				)
												# 0.0.0.1
												if sanitized.count('.'				) > 1:
																		return float(sanitized.replace('.'      ,						''				)				)
												# 0,0,0,1
												if sanitized.count(','				) > 1:
																		return float(sanitized.replace(','      ,						''				)				)
												return float(lowercase__				)
						except ValueError:
												# Avoid adding the sanitized value in the error message.
												raise ValueError('Unable to convert value to float'				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Tuple				)       ->  Tuple:
						if answer is None:
												return None
						try:
												__magic_name__				:							Any				=      convert_to_float(lowercase__				)
												if isinstance(lowercase__      ,						lowercase__				) and math.isnan(lowercase__				):
																		return None
												return value
						except ValueError:
												return answer.lower()


snake_case__     							= {
    'text': lambda x: x,
    'real': convert_to_float,
}



class     snake_case__  (      enum.Enum  ):
				__lowerCAmelCase		=      0
				__lowerCAmelCase		=      1
				__lowerCAmelCase		=      2
				__lowerCAmelCase		=      3
				__lowerCAmelCase		=      4
				__lowerCAmelCase		=      5






class     snake_case__  (      enum.Enum  ):
				__lowerCAmelCase		=      0
				__lowerCAmelCase		=      1
				__lowerCAmelCase		=      2






@dataclasses.dataclass
class     snake_case__  :
				__lowerCAmelCase		=      42
				__lowerCAmelCase		=      42
				__lowerCAmelCase		=      42






snake_case__     							= re.compile(R'\w+|[^\w\s]+', re.UNICODE | re.MULTILINE | re.DOTALL)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Any				)       ->  str:
						return list(_TOKENIZER.findall(x.lower()				)				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :str      ,						lowercase__  :str      ,						lowercase__  :Dict				)       ->  Dict:
						if operator == _Operator.EQUALS:
												return src == tgt
						elif operator == _Operator.GREATER:
												return src > tgt
						elif operator == _Operator.LESSER:
												return src < tgt
						raise ValueError(f"Unknown operator: {operator}"				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :List[Any]      ,						lowercase__  :Tuple      ,						lowercase__  :Union[str, Any]				)       ->  Optional[int]:
						__magic_name__				:							str				=      table['types']
						return _TYPE_CONVERTER[types[column]](lowercase__				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Tuple				)       ->  Union[str, Any]:
						return isinstance(lowercase__      ,						lowercase__				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :List[Any]      ,						lowercase__  :List[str]      ,						lowercase__  :int				)       ->  str:
						for cond in conditions:
												__magic_name__				:							Optional[Any]				=      row[cond.column]

												__magic_name__				:							List[str]				=      _parse_value(lowercase__      ,						cond.column      ,						cond.cmp_value				)

												if _is_string(lowercase__				) and _is_string(lowercase__				):
																		__magic_name__				:							str				=      _normalize_for_match(lowercase__				)
																		__magic_name__				:							List[str]				=      _normalize_for_match(lowercase__				)

												if not isinstance(lowercase__      ,						type(lowercase__				)				):
																		raise ValueError('Type difference {} != {}'.format(type(lowercase__				)      ,						type(lowercase__				)				)				)

												if not _compare(cond.operator      ,						lowercase__      ,						lowercase__				):
																		return False
						return True
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :str      ,						lowercase__  :List[Any]      ,						lowercase__  :List[Any]				)       ->  int:
						if not answer_coordinates:
												if aggregation_op == _Aggregation.COUNT:
																		return 0.0
												else:
																		return EMPTY_ANSWER_AGG

    # Count can support non numeric answers.
						if aggregation_op == _Aggregation.COUNT:
												return float(len(lowercase__				)				)

						# If we have just one answer, if float returns it or try a conversion.
						__magic_name__				:							Any				=      [table['rows'][i][j] for (i, j) in answer_coordinates]
						if len(lowercase__				) == 1:
												try:
																		return convert_to_float(values[0]				)
												except ValueError as e:
																		if aggregation_op != _Aggregation.NONE:
																								raise e

						if aggregation_op == _Aggregation.NONE:
												return None

						# Other aggregation only support numeric values. Bail out if we have strings.
						if not all((isinstance(lowercase__      ,						(int, float)				) for v in values)				):
												return None

						if aggregation_op == _Aggregation.SUM:
												return float(sum(lowercase__				)				)
						elif aggregation_op == _Aggregation.AVERAGE:
												return sum(lowercase__				) / len(lowercase__				)
						else:
												raise ValueError(f"Unknown aggregation: {aggregation_op}"				)
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Tuple      ,						lowercase__  :Any				)       ->  Optional[int]:
						__magic_name__				:							Optional[Any]				=      sql_query['agg']
						if aggregation_op_index >= 3:
												__magic_name__				:							Any				=      _Aggregation(lowercase__				)
						else:
												__magic_name__				:							int				=      _Aggregation.NONE

						__magic_name__				:							str				=      sql_query['sel']
						__magic_name__				:							Any				=      [
						    _Condition(lowercase__      ,						_Operator(lowercase__				)      ,						lowercase__				)
						    for column, operator, cmp_value in zip(
						        sql_query['conds']['column_index']      ,						sql_query['conds']['operator_index']      ,						sql_query['conds']['condition']				)
						]

						__magic_name__				:							Any				=      []
						for row in range(len(table['rows']				)				):
												if _respect_conditions(lowercase__      ,						table['rows'][row]      ,						lowercase__				):
																		indices.append((row, target_column)				)

						if not indices:
												return [], aggregation_op

						if len(lowercase__				) == 1:
												return indices, aggregation_op

						# Parsing of MIN/MAX.
						if aggregation_op_index in (1, 2):
												__magic_name__				:							str				=      {2: min, 1: max}
												__magic_name__				:							Optional[int]				=      [(table['rows'][i][j], index) for index, (i, j) in enumerate(lowercase__				)]
												__magic_name__				:							str				=      functools.reduce(operators[sql_query['agg']]      ,						lowercase__				)

												__magic_name__				:							List[Any]				=      [indices[reduced[1]]]
												return ret, _Aggregation.NONE

						return indices, aggregation_op
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Tuple      ,						lowercase__  :Any      ,						lowercase__  :Optional[int]				)       ->  List[Any]:
						if float_answer is not None:
												return [str(lowercase__				)]
						return [str(table['real_rows'][r][c]				) for r, c in answer_coordinates]

def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Optional[Any]      ,						lowercase__  :Union[str, Any]				)       ->  List:
						__magic_name__		, __magic_name__				:							List[Any]				=      _get_answer_coordinates(lowercase__      ,						lowercase__				)
						__magic_name__				:							List[Any]				=      _get_float_answer(lowercase__      ,						lowercase__      ,						lowercase__				)
						__magic_name__				:							Any				=      _get_answer_text(lowercase__      ,						lowercase__      ,						lowercase__				)
						# keep the original data the same with TaPas
						if len(lowercase__				) == 0:
												__magic_name__				:							int				=      [EMPTY_ANSWER]
						return answer_text


