


'''simple docstring'''



import os
from itertools import chain
from random import randrange, shuffle

import pytest

from .sola import PokerHand

__A		=(
    '4S 3H 2C 7S 5H',
    '9D 8H 2C 6S 7H',
    '2D 6D 9D TH 7D',
    'TC 8C 2S JH 6C',
    'JH 8S TH AH QH',
    'TS KS 5S 9S AC',
    'KD 6S 9D TH AD',
    'KS 8D 4D 9S 4S',  # pair
    '8C 4S KH JS 4D',  # pair
    'QH 8H KD JH 8S',  # pair
    'KC 4H KS 2H 8D',  # pair
    'KD 4S KC 3H 8S',  # pair
    'AH 8S AS KC JH',  # pair
    '3H 4C 4H 3S 2H',  # 2 pairs
    '5S 5D 2C KH KH',  # 2 pairs
    '3C KH 5D 5S KH',  # 2 pairs
    'AS 3C KH AD KH',  # 2 pairs
    '7C 7S 3S 7H 5S',  # 3 of a kind
    '7C 7S KH 2H 7H',  # 3 of a kind
    'AC KH QH AH AS',  # 3 of a kind
    '2H 4D 3C AS 5S',  # straight (low ace)
    '3C 5C 4C 2C 6H',  # straight
    '6S 8S 7S 5H 9H',  # straight
    'JS QS 9H TS KH',  # straight
    'QC KH TS JS AH',  # straight (high ace)
    '8C 9C 5C 3C TC',  # flush
    '3S 8S 9S 5S KS',  # flush
    '4C 5C 9C 8C KC',  # flush
    'JH 8H AH KH QH',  # flush
    '3D 2H 3H 2C 2D',  # full house
    '2H 2C 3S 3H 3D',  # full house
    'KH KC 3S 3H 3D',  # full house
    'JC 6H JS JD JH',  # 4 of a kind
    'JC 7H JS JD JH',  # 4 of a kind
    'JC KH JS JD JH',  # 4 of a kind
    '2S AS 4S 5S 3S',  # straight flush (low ace)
    '2D 6D 3D 4D 5D',  # straight flush
    '5C 6C 3C 7C 4C',  # straight flush
    'JH 9H TH KH QH',  # straight flush
    'JH AH TH KH QH',  # royal flush (high ace straight flush)
)

__A		=(
    ('2H 3H 4H 5H 6H', 'KS AS TS QS JS', 'Loss'),
    ('2H 3H 4H 5H 6H', 'AS AD AC AH JD', 'Win'),
    ('AS AH 2H AD AC', 'JS JD JC JH 3D', 'Win'),
    ('2S AH 2H AS AC', 'JS JD JC JH AD', 'Loss'),
    ('2S AH 2H AS AC', '2H 3H 5H 6H 7H', 'Win'),
    ('AS 3S 4S 8S 2S', '2H 3H 5H 6H 7H', 'Win'),
    ('2H 3H 5H 6H 7H', '2S 3H 4H 5S 6C', 'Win'),
    ('2S 3H 4H 5S 6C', '3D 4C 5H 6H 2S', 'Tie'),
    ('2S 3H 4H 5S 6C', 'AH AC 5H 6H AS', 'Win'),
    ('2S 2H 4H 5S 4C', 'AH AC 5H 6H AS', 'Loss'),
    ('2S 2H 4H 5S 4C', 'AH AC 5H 6H 7S', 'Win'),
    ('6S AD 7H 4S AS', 'AH AC 5H 6H 7S', 'Loss'),
    ('2S AH 4H 5S KC', 'AH AC 5H 6H 7S', 'Loss'),
    ('2S 3H 6H 7S 9C', '7H 3C TH 6H 9S', 'Loss'),
    ('4S 5H 6H TS AC', '3S 5H 6H TS AC', 'Win'),
    ('2S AH 4H 5S 6C', 'AD 4C 5H 6H 2C', 'Tie'),
    ('AS AH 3H AD AC', 'AS AH 2H AD AC', 'Win'),
    ('AH AC 5H 5C QS', 'AH AC 5H 5C KS', 'Loss'),
    ('AH AC 5H 5C QS', 'KH KC 5H 5C QS', 'Win'),
    ('7C 7S KH 2H 7H', '3C 3S AH 2H 3H', 'Win'),
    ('3C 3S AH 2H 3H', '7C 7S KH 2H 7H', 'Loss'),
    ('6H 5H 4H 3H 2H', '5H 4H 3H 2H AH', 'Win'),
    ('5H 4H 3H 2H AH', '5H 4H 3H 2H AH', 'Tie'),
    ('5H 4H 3H 2H AH', '6H 5H 4H 3H 2H', 'Loss'),
    ('AH AD KS KC AC', 'AH KD KH AC KC', 'Win'),
    ('2H 4D 3C AS 5S', '2H 4D 3C 6S 5S', 'Loss'),
    ('2H 3S 3C 3H 2S', '3S 3C 2S 2H 2D', 'Win'),
    ('4D 6D 5D 2D JH', '3S 8S 3H TC KH', 'Loss'),
    ('4S 6C 8S 3S 7S', 'AD KS 2D 7D 7C', 'Loss'),
    ('6S 4C 7H 8C 3H', '5H JC AH 9D 9C', 'Loss'),
    ('9D 9H JH TC QH', '3C 2S JS 5C 7H', 'Win'),
    ('2H TC 8S AD 9S', '4H TS 7H 2C 5C', 'Win'),
    ('9D 3S 2C 7S 7C', 'JC TD 3C TC 9H', 'Loss'),
)

__A		=(
    ('2H 3H 4H 5H 6H', True),
    ('AS AH 2H AD AC', False),
    ('2H 3H 5H 6H 7H', True),
    ('KS AS TS QS JS', True),
    ('8H 9H QS JS TH', False),
    ('AS 3S 4S 8S 2S', True),
)

__A		=(
    ('2H 3H 4H 5H 6H', True),
    ('AS AH 2H AD AC', False),
    ('2H 3H 5H 6H 7H', False),
    ('KS AS TS QS JS', True),
    ('8H 9H QS JS TH', True),
)

__A		=(
    ('2H 4D 3C AS 5S', True, [5, 4, 3, 2, 14]),
    ('2H 5D 3C AS 5S', False, [14, 5, 5, 3, 2]),
    ('JH QD KC AS TS', False, [14, 13, 12, 11, 10]),
    ('9D 3S 2C 7S 7C', False, [9, 7, 7, 3, 2]),
)

__A		=(
    ('JH AH TH KH QH', 0),
    ('JH 9H TH KH QH', 0),
    ('JC KH JS JD JH', 7),
    ('KH KC 3S 3H 3D', 6),
    ('8C 9C 5C 3C TC', 0),
    ('JS QS 9H TS KH', 0),
    ('7C 7S KH 2H 7H', 3),
    ('3C KH 5D 5S KH', 2),
    ('QH 8H KD JH 8S', 1),
    ('2D 6D 9D TH 7D', 0),
)

__A		=(
    ('JH AH TH KH QH', 23),
    ('JH 9H TH KH QH', 22),
    ('JC KH JS JD JH', 21),
    ('KH KC 3S 3H 3D', 20),
    ('8C 9C 5C 3C TC', 19),
    ('JS QS 9H TS KH', 18),
    ('7C 7S KH 2H 7H', 17),
    ('3C KH 5D 5S KH', 16),
    ('QH 8H KD JH 8S', 15),
    ('2D 6D 9D TH 7D', 14),
)







def       _UpperCamelCase (						):
			UpperCAmelCase__					:   List[str]       				=       randrange(len(lowercase__   )   ), randrange(len(lowercase__   )   )
			UpperCAmelCase__					:   Tuple       				=       ["""Loss""", """Tie""", """Win"""][(play >= oppo) + (play > oppo)]
			UpperCAmelCase__					:   Tuple       				=       SORTED_HANDS[play], SORTED_HANDS[oppo]
			return hand, other, expected







def       _UpperCamelCase (						UpperCamelCase__ = 1_0_0   ):
			return (generate_random_hand() for _ in range(lowercase__   ))







@pytest.mark.parametrize("""hand, expected"""	, lowercase__   )
def       _UpperCamelCase (						UpperCamelCase__	, UpperCamelCase__   ):
			assert PokerHand(lowercase__   )._is_flush() == expected







@pytest.mark.parametrize("""hand, expected"""	, lowercase__   )
def       _UpperCamelCase (						UpperCamelCase__	, UpperCamelCase__   ):
			assert PokerHand(lowercase__   )._is_straight() == expected







@pytest.mark.parametrize("""hand, expected, card_values"""	, lowercase__   )
def       _UpperCamelCase (						UpperCamelCase__	, UpperCamelCase__	, UpperCamelCase__   ):
			UpperCAmelCase__					:   Optional[Any]       				=       PokerHand(lowercase__   )
			assert player._is_five_high_straight() == expected
			assert player._card_values == card_values







@pytest.mark.parametrize("""hand, expected"""	, lowercase__   )
def       _UpperCamelCase (						UpperCamelCase__	, UpperCamelCase__   ):
			assert PokerHand(lowercase__   )._is_same_kind() == expected







@pytest.mark.parametrize("""hand, expected"""	, lowercase__   )
def       _UpperCamelCase (						UpperCamelCase__	, UpperCamelCase__   ):
			assert PokerHand(lowercase__   )._hand_type == expected







@pytest.mark.parametrize("""hand, other, expected"""	, lowercase__   )
def       _UpperCamelCase (						UpperCamelCase__	, UpperCamelCase__	, UpperCamelCase__   ):
			assert PokerHand(lowercase__   ).compare_with(PokerHand(lowercase__   )   ) == expected







@pytest.mark.parametrize("""hand, other, expected"""	, generate_random_hands()   )
def       _UpperCamelCase (						UpperCamelCase__	, UpperCamelCase__	, UpperCamelCase__   ):
			assert PokerHand(lowercase__   ).compare_with(PokerHand(lowercase__   )   ) == expected







def       _UpperCamelCase (						):
			UpperCAmelCase__					:   List[Any]       				=       [PokerHand(lowercase__   ) for hand in SORTED_HANDS]
			UpperCAmelCase__					:   Optional[Any]       				=       poker_hands.copy()
			shuffle(lowercase__   )
			UpperCAmelCase__					:   Union[str, Any]       				=       chain(sorted(lowercase__   )   )
			for index, hand in enumerate(lowercase__   ):
						assert hand == poker_hands[index]







def       _UpperCamelCase (						):
			UpperCAmelCase__					:   Any       				=       [PokerHand("""2D AC 3H 4H 5S"""   ), PokerHand("""2S 3H 4H 5S 6C"""   )]
			pokerhands.sort(reverse=lowercase__   )
			assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C"







def       _UpperCamelCase (						):
			UpperCAmelCase__					:   Any       				=       PokerHand("""2C 4S AS 3D 5C"""   )
			UpperCAmelCase__					:   Union[str, Any]       				=       True
			UpperCAmelCase__					:   Tuple       				=       [5, 4, 3, 2, 1_4]
			for _ in range(1_0   ):
						assert pokerhand._is_five_high_straight() == expected
						assert pokerhand._card_values == expected_card_values







def       _UpperCamelCase (						):
			UpperCAmelCase__					:   List[Any]       				=       0
			UpperCAmelCase__					:   List[Any]       				=       os.path.abspath(os.path.dirname(lowercase__   )   )
			UpperCAmelCase__					:   int       				=       os.path.join(lowercase__	, """poker_hands.txt"""   )
			with open(lowercase__   ) as file_hand:
						for line in file_hand:
									UpperCAmelCase__					:   Any       				=       line[:1_4].strip()
									UpperCAmelCase__					:   Tuple       				=       line[1_5:].strip()
									UpperCAmelCase__					:   List[str]       				=       PokerHand(lowercase__   ), PokerHand(lowercase__   )
									UpperCAmelCase__					:   List[Any]       				=       player.compare_with(lowercase__   )
									if output == "Win":
												answer += 1
			assert answer == 3_7_6



'''simple docstring'''



import os
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import SPIECE_UNDERLINE, logging


__A		=logging.get_logger(__name__)

__A		={'vocab_file': 'spiece.model'}

__A		={
    'vocab_file': {
        'TsinghuaAI/CPM-Generate': 'https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model',
    }
}


class 						_snake_case   (       a__		):




					def __init__( self							,							_lowerCamelCase							,							_lowerCamelCase=False							,							_lowerCamelCase=True							,							_lowerCamelCase=False							,							_lowerCamelCase="<s>"							,							_lowerCamelCase="</s>"							,							_lowerCamelCase="<unk>"							,							_lowerCamelCase="<sep>"							,							_lowerCamelCase="<pad>"							,							_lowerCamelCase="<cls>"							,							_lowerCamelCase="<mask>"							,							_lowerCamelCase=["<eop>", "<eod>"]							,							_lowerCamelCase = None							,							**_lowerCamelCase							,							):
								UpperCAmelCase__					:   Dict       				=       AddedToken(_lowerCamelCase							,							lstrip=_lowerCamelCase							,							rstrip=_lowerCamelCase) if isinstance(_lowerCamelCase							,							_lowerCamelCase) else mask_token

								UpperCAmelCase__					:   Any       				=       {} if sp_model_kwargs is None else sp_model_kwargs

								super().__init__(
								    do_lower_case=_lowerCamelCase							,							remove_space=_lowerCamelCase							,							keep_accents=_lowerCamelCase							,							bos_token=_lowerCamelCase							,							eos_token=_lowerCamelCase							,							unk_token=_lowerCamelCase							,							sep_token=_lowerCamelCase							,							pad_token=_lowerCamelCase							,							cls_token=_lowerCamelCase							,							mask_token=_lowerCamelCase							,							additional_special_tokens=_lowerCamelCase							,							sp_model_kwargs=self.sp_model_kwargs							,							**_lowerCamelCase							,							)

								UpperCAmelCase__					:   List[str]       				=       3

								UpperCAmelCase__					:   str       				=       do_lower_case
								UpperCAmelCase__					:   Tuple       				=       remove_space
								UpperCAmelCase__					:   List[str]       				=       keep_accents
								UpperCAmelCase__					:   Any       				=       vocab_file

								UpperCAmelCase__					:   str       				=       spm.SentencePieceProcessor(**self.sp_model_kwargs)
								self.sp_model.Load(_lowerCamelCase)

								try:
											import jieba
								except ModuleNotFoundError as error:
											raise error.__class__(
											    """You need to install jieba to use CpmTokenizer or CpmTokenizerFast. """
											    """See https://pypi.org/project/jieba/ for installation.""")
								UpperCAmelCase__					:   Union[str, Any]       				=       jieba
								UpperCAmelCase__					:   int       				=       str.maketrans(""" \n"""							,							"""\u2582\u2583""")




					@property
					# Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size
					def 			snake_case__  ( self):
								return len(self.sp_model)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Optional[Any]       				=       {self.convert_ids_to_tokens(_lowerCamelCase): i for i in range(self.vocab_size)}
								vocab.update(self.added_tokens_encoder)
								return vocab




					def __getstate__( self):
								UpperCAmelCase__					:   List[str]       				=       self.__dict__.copy()
								UpperCAmelCase__					:   Optional[int]       				=       None
								return state




					def __setstate__( self							,							_lowerCamelCase):
								UpperCAmelCase__					:   Optional[Any]       				=       d

								# for backward compatibility
								if not hasattr(self							,							"""sp_model_kwargs"""):
											UpperCAmelCase__					:   Tuple       				=       {}

								UpperCAmelCase__					:   Union[str, Any]       				=       spm.SentencePieceProcessor(**self.sp_model_kwargs)
								self.sp_model.Load(self.vocab_file)




					def 			snake_case__  ( self							,							_lowerCamelCase):
								if self.remove_space:
											UpperCAmelCase__					:   Tuple       				=       """ """.join(inputs.strip().split())
								else:
											UpperCAmelCase__					:   str       				=       inputs
								UpperCAmelCase__					:   List[Any]       				=       outputs.replace("""``"""							,							"""\"""").replace("""''"""							,							"""\"""")

								if not self.keep_accents:
											UpperCAmelCase__					:   str       				=       unicodedata.normalize("""NFKD"""							,							_lowerCamelCase)
											UpperCAmelCase__					:   Any       				=       """""".join([c for c in outputs if not unicodedata.combining(_lowerCamelCase)])
								if self.do_lower_case:
											UpperCAmelCase__					:   Optional[int]       				=       outputs.lower()

								return outputs




					def 			snake_case__  ( self							,							_lowerCamelCase):
								UpperCAmelCase__					:   List[str]       				=       self.preprocess_text(_lowerCamelCase)
								UpperCAmelCase__					:   Dict       				=       self.sp_model.encode(_lowerCamelCase							,							out_type=_lowerCamelCase)
								UpperCAmelCase__					:   List[Any]       				=       []
								for piece in pieces:
											if len(_lowerCamelCase) > 1 and piece[-1] == str(""",""") and piece[-2].isdigit():
														UpperCAmelCase__					:   str       				=       self.sp_model.EncodeAsPieces(piece[:-1].replace(_lowerCamelCase							,							""""""))
														if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
																	if len(cur_pieces[0]) == 1:
																				UpperCAmelCase__					:   Optional[int]       				=       cur_pieces[1:]
																	else:
																				UpperCAmelCase__					:   Optional[int]       				=       cur_pieces[0][1:]
														cur_pieces.append(piece[-1])
														new_pieces.extend(_lowerCamelCase)
											else:
														new_pieces.append(_lowerCamelCase)

								return new_pieces




					def 			snake_case__  ( self							,							_lowerCamelCase):
								return self.sp_model.PieceToId(_lowerCamelCase)




					def 			snake_case__  ( self							,							_lowerCamelCase):
								return self.sp_model.IdToPiece(_lowerCamelCase)




					def 			snake_case__  ( self							,							_lowerCamelCase):
								UpperCAmelCase__					:   str       				=       """""".join(_lowerCamelCase).replace(_lowerCamelCase							,							""" """).strip()
								return out_string




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = None):
								UpperCAmelCase__					:   Optional[Any]       				=       [self.sep_token_id]
								UpperCAmelCase__					:   Optional[int]       				=       [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 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = None							,							_lowerCamelCase = False):

								if already_has_special_tokens:
											return super().get_special_tokens_mask(
											    token_ids_a=_lowerCamelCase							,							token_ids_a=_lowerCamelCase							,							already_has_special_tokens=_lowerCamelCase)

								if token_ids_a is not None:
											return ([0] * len(_lowerCamelCase)) + [1] + ([0] * len(_lowerCamelCase)) + [1, 1]
								return ([0] * len(_lowerCamelCase)) + [1, 1]




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = None):
								UpperCAmelCase__					:   int       				=       [self.sep_token_id]
								UpperCAmelCase__					:   Tuple       				=       [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 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = None):
								if not os.path.isdir(_lowerCamelCase):
											logger.error(f'''Vocabulary path ({save_directory}) should be a directory''')
											return
								UpperCAmelCase__					:   str       				=       os.path.join(
								    _lowerCamelCase							,							(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""])

								if os.path.abspath(self.vocab_file) != os.path.abspath(_lowerCamelCase) and os.path.isfile(self.vocab_file):
											copyfile(self.vocab_file							,							_lowerCamelCase)
								elif not os.path.isfile(self.vocab_file):
											with open(_lowerCamelCase							,							"""wb""") as fi:
														UpperCAmelCase__					:   str       				=       self.sp_model.serialized_model_proto()
														fi.write(_lowerCamelCase)

								return (out_vocab_file,)




					def 			snake_case__  ( self							,							*_lowerCamelCase							,							**_lowerCamelCase):
								UpperCAmelCase__					:   Union[str, Any]       				=       super()._decode(*_lowerCamelCase							,							**_lowerCamelCase)
								UpperCAmelCase__					:   Any       				=       text.replace(""" """							,							"""""").replace("""\u2582"""							,							""" """).replace("""\u2583"""							,							"""\n""")
								return text