

"""simple docstring"""


import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
  import sqlitea

  import sqlalchemy

class      __a							(UpperCamelCase_):






     '''simple docstring'''

     def __init__(						self	,    _a	,    _a	,    _a = None	,    _a = None	,    _a = False	,    **_a	,    )  ->      Union[str, Any]:


           """simple docstring"""


           super().__init__(features=_a	,    cache_dir=_a	,    keep_in_memory=_a	,    **_a  )
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	Sql(
               cache_dir=_a	,    features=_a	,    sql=_a	,    con=_a	,    **_a	,    )





     def 					_a			(						self  )  ->      int:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	None
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	None
           SCREAMING_SNAKE_CASE__   :      Dict      		=	None
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	None

           self.builder.download_and_prepare(
               download_config=_a	,    download_mode=_a	,    verification_mode=_a	,    base_path=_a	,    )

           # Build dataset for splits
           SCREAMING_SNAKE_CASE__   :      str      		=	self.builder.as_dataset(
               split="""train"""	,    verification_mode=_a	,    in_memory=self.keep_in_memory  )
           return dataset





class      __a							:






     '''simple docstring'''

     def __init__(						self	,    _a	,    _a	,    _a	,    _a = None	,    _a = None	,    **_a	,    )  ->      Any:


           """simple docstring"""


           if num_proc is not None and num_proc <= 0:
                 raise ValueError(f'''num_proc {num_proc} must be an integer > 0.'''  )

           SCREAMING_SNAKE_CASE__   :      int      		=	dataset
           SCREAMING_SNAKE_CASE__   :      Any      		=	name
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	con
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
           SCREAMING_SNAKE_CASE__   :      int      		=	num_proc
           SCREAMING_SNAKE_CASE__   :      int      		=	to_sql_kwargs

     def 					_a			(						self  )  ->      int:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      str      		=	self.to_sql_kwargs.pop("""sql"""	,    _a  )
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	self.to_sql_kwargs.pop("""con"""	,    _a  )
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	self.to_sql_kwargs.pop("""index"""	,    _a  )

           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	self._write(index=_a	,    **self.to_sql_kwargs  )
           return written

     def 					_a			(						self	,    _a  )  ->      Union[str, Any]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	args
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	{**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs
           SCREAMING_SNAKE_CASE__   :      Any      		=	query_table(
               table=self.dataset.data	,    key=slice(_a	,    offset + self.batch_size  )	,    indices=self.dataset._indices	,    )
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	batch.to_pandas()
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	df.to_sql(self.name	,    self.con	,    index=_a	,    **_a  )
           return num_rows or len(_a  )





     def 					_a			(						self	,    _a	,    **_a  )  ->      int:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      List[str]      		=	0

           if self.num_proc is None or self.num_proc == 1:
                 for offset in logging.tqdm(
                     range(0	,    len(self.dataset  )	,    self.batch_size  )	,    unit="""ba"""	,    disable=not logging.is_progress_bar_enabled()	,    desc="""Creating SQL from Arrow format"""	,    ):
                       written += self._batch_sql((offset, index, to_sql_kwargs)  )
           else:
                 SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      str      		=	len(self.dataset  ), self.batch_size
                 with multiprocessing.Pool(self.num_proc  ) as pool:
                       for num_rows in logging.tqdm(
                           pool.imap(
                               self._batch_sql	,    [(offset, index, to_sql_kwargs) for offset in range(0	,    _a	,    _a  )]	,    )	,    total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size	,    unit="""ba"""	,    disable=not logging.is_progress_bar_enabled()	,    desc="""Creating SQL from Arrow format"""	,    ):
                             written += num_rows

           return written







"""simple docstring"""


import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
  from .tokenization_xlnet import XLNetTokenizer
else:
  a					:List[Any]    = None


a					:Optional[int]    = logging.get_logger(__name__)

a					:Union[str, Any]    = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

a					:Optional[int]    = {
    "vocab_file": {
        "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model",
        "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model",
    },
    "tokenizer_file": {
        "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json",
        "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json",
    },
}

a					:Dict    = {
    "xlnet-base-cased": None,
    "xlnet-large-cased": None,
}

a					:int    = "▁"

# Segments (not really needed)
a					:Dict    = 0
a					:Optional[int]    = 1
a					:Tuple    = 2
a					:List[str]    = 3
a					:Optional[Any]    = 4

class      __a							(UpperCamelCase_):






     '''simple docstring'''
     _SCREAMING_SNAKE_CASE						:Tuple       = VOCAB_FILES_NAMES
     _SCREAMING_SNAKE_CASE						:Union[str, Any]       = PRETRAINED_VOCAB_FILES_MAP
     _SCREAMING_SNAKE_CASE						:Tuple       = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     _SCREAMING_SNAKE_CASE						:str       = """left"""
     _SCREAMING_SNAKE_CASE						:Optional[Any]       = XLNetTokenizer

     def __init__(						self	,    _a=None	,    _a=None	,    _a=False	,    _a=True	,    _a=False	,    _a="<s>"	,    _a="</s>"	,    _a="<unk>"	,    _a="<sep>"	,    _a="<pad>"	,    _a="<cls>"	,    _a="<mask>"	,    _a=["<eop>", "<eod>"]	,    **_a	,    )  ->      Any:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Tuple      		=	AddedToken(_a	,    lstrip=_a	,    rstrip=_a  ) if isinstance(_a	,    _a  ) else mask_token

           super().__init__(
               vocab_file=_a	,    tokenizer_file=_a	,    do_lower_case=_a	,    remove_space=_a	,    keep_accents=_a	,    bos_token=_a	,    eos_token=_a	,    unk_token=_a	,    sep_token=_a	,    pad_token=_a	,    cls_token=_a	,    mask_token=_a	,    additional_special_tokens=_a	,    **_a	,    )

           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	3
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	do_lower_case
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	remove_space
           SCREAMING_SNAKE_CASE__   :      int      		=	keep_accents
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	vocab_file
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	False if not self.vocab_file else True

     def 					_a			(						self	,    _a	,    _a = None  )  ->      List[int]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      str      		=	[self.sep_token_id]
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	[self.cls_token_id]
           if token_ids_a is None:
                 return token_ids_a + sep + cls
           return token_ids_a + sep + token_ids_a + sep + cls

     def 					_a			(						self	,    _a	,    _a = None  )  ->      List[int]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Any      		=	[self.sep_token_id]
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	[2]

           if token_ids_a is None:
                 return len(token_ids_a + sep  ) * [0] + cls_segment_id
           return len(token_ids_a + sep  ) * [0] + len(token_ids_a + sep  ) * [1] + cls_segment_id





     def 					_a			(						self	,    _a	,    _a = None  )  ->      Tuple[str]:


           """simple docstring"""


           if not self.can_save_slow_tokenizer:
                 raise ValueError(
                     """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
                     """tokenizer."""  )

           if not os.path.isdir(_a  ):
                 logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''  )
                 return
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	os.path.join(
               _a	,    (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]  )

           if os.path.abspath(self.vocab_file  ) != os.path.abspath(_a  ):
                 copyfile(self.vocab_file	,    _a  )

           return (out_vocab_file,)




