




import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 							__UpperCamelCase			( __UpperCAmelCase     ,					unittest.TestCase  ):





    '''simple docstring'''



    __a							: Union[str, Any]										=CLIPTokenizer
    __a							: Dict										=CLIPTokenizerFast
    __a							: Optional[int]										=True
    __a							: int										={}
    __a							: Any										=False



    def       __snake_case		( self						):
           super().setUp()

           # fmt: off
           lowerCAmelCase   =		['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
           # fmt: on
           lowerCAmelCase   =		dict(zip(UpperCAmelCase_       , range(len(UpperCAmelCase_						)						)						)						)
           lowerCAmelCase   =		['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''']
           lowerCAmelCase   =		{'''unk_token''': '''<unk>'''}

           lowerCAmelCase   =		os.path.join(self.tmpdirname       , VOCAB_FILES_NAMES['''vocab_file''']						)
           lowerCAmelCase   =		os.path.join(self.tmpdirname       , VOCAB_FILES_NAMES['''merges_file''']						)
           with open(self.vocab_file       , '''w'''       , encoding='''utf-8'''						) as fp:
                  fp.write(json.dumps(UpperCAmelCase_						) + '''\n'''						)
           with open(self.merges_file       , '''w'''       , encoding='''utf-8'''						) as fp:
                  fp.write('''\n'''.join(UpperCAmelCase_						)						)



    def       __snake_case		( self       , **UpperCAmelCase_						):
           kwargs.update(self.special_tokens_map						)
           return CLIPTokenizer.from_pretrained(self.tmpdirname       , **UpperCAmelCase_						)



    def       __snake_case		( self       , **UpperCAmelCase_						):
           kwargs.update(self.special_tokens_map						)
           return CLIPTokenizerFast.from_pretrained(self.tmpdirname       , **UpperCAmelCase_						)



    def       __snake_case		( self       , UpperCAmelCase_						):
           lowerCAmelCase   =		'''lower newer'''
           lowerCAmelCase   =		'''lower newer'''
           return input_text, output_text



    def       __snake_case		( self						):
           lowerCAmelCase   =		CLIPTokenizer(self.vocab_file       , self.merges_file       , **self.special_tokens_map						)
           lowerCAmelCase   =		'''lower newer'''
           lowerCAmelCase   =		['''lo''', '''w''', '''er</w>''', '''n''', '''e''', '''w''', '''er</w>''']
           lowerCAmelCase   =		tokenizer.tokenize(UpperCAmelCase_						)
           self.assertListEqual(UpperCAmelCase_       , UpperCAmelCase_						)

           lowerCAmelCase   =		tokens + [tokenizer.unk_token]
           lowerCAmelCase   =		[10, 2, 16, 9, 3, 2, 16, 20]
           self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_						)       , UpperCAmelCase_						)



    @require_ftfy
    def       __snake_case		( self						):
           for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                  with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""						):
                         lowerCAmelCase   =		self.tokenizer_class.from_pretrained(UpperCAmelCase_       , **UpperCAmelCase_						)
                         lowerCAmelCase   =		self.rust_tokenizer_class.from_pretrained(UpperCAmelCase_       , **UpperCAmelCase_						)

                         lowerCAmelCase   =		'''A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'''
                         lowerCAmelCase   =		tokenizer_s.tokenize(UpperCAmelCase_						)
                         lowerCAmelCase   =		tokenizer_r.tokenize(UpperCAmelCase_						)

                         self.assertListEqual(UpperCAmelCase_       , UpperCAmelCase_						)

                         # Test that the tokenization is identical on an example containing a character (Latin Small Letter A
                         # with Tilde) encoded in 2 different ways
                         lowerCAmelCase   =		'''xa\u0303y''' + ''' ''' + '''x\xe3y'''
                         lowerCAmelCase   =		tokenizer_s.tokenize(UpperCAmelCase_						)
                         lowerCAmelCase   =		tokenizer_r.tokenize(UpperCAmelCase_						)

                         self.assertListEqual(UpperCAmelCase_       , UpperCAmelCase_						)

                         # Test that the tokenization is identical on unicode of space type
                         lowerCAmelCase   =		[
                             '''\u0009''',  # (horizontal tab, '\t')
                             '''\u000B''',  # (vertical tab)
                             '''\u000C''',  # (form feed)
                             '''\u0020''',  # (space, ' ')
                             '''\u200E''',  # (left-to-right mark):w
                             '''\u200F''',  # (right-to-left mark)
                         ]
                         for unicode_seq in spaces_unicodes:
                                lowerCAmelCase   =		tokenizer_s.tokenize(UpperCAmelCase_						)
                                lowerCAmelCase   =		tokenizer_r.tokenize(UpperCAmelCase_						)

                                self.assertListEqual(UpperCAmelCase_       , UpperCAmelCase_						)

                         # Test that the tokenization is identical on unicode of line break type
                         lowerCAmelCase   =		[
                             '''\u000A''',  # (line feed, '\n')
                             '''\r\n''',  # (carriage return and line feed, '\r\n')
                             '''\u000D''',  # (carriage return, '\r')
                             '''\r''',  # (carriage return, '\r')
                             '''\u000D''',  # (carriage return, '\r')
                             '''\u2028''',  # (line separator)
                             '''\u2029''',  # (paragraph separator)
                             # "\u0085", # (next line)
                         ]

                         # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
                         # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
                         # space (and thus into an empty list).

                         for unicode_seq in line_break_unicodes:
                                lowerCAmelCase   =		tokenizer_s.tokenize(UpperCAmelCase_						)
                                lowerCAmelCase   =		tokenizer_r.tokenize(UpperCAmelCase_						)

                                self.assertListEqual(UpperCAmelCase_       , UpperCAmelCase_						)



    def       __snake_case		( self						):
           # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
           for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                  with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""						):
                         lowerCAmelCase   =		'''hello'''  # `hello` is a token in the vocabulary of `pretrained_name`
                         lowerCAmelCase   =		F"""{text_of_1_token} {text_of_1_token}"""

                         lowerCAmelCase   =		self.rust_tokenizer_class.from_pretrained(
                             UpperCAmelCase_       , use_fast=UpperCAmelCase_       , )
                         lowerCAmelCase   =		tokenizer_r(UpperCAmelCase_       , return_offsets_mapping=UpperCAmelCase_       , add_special_tokens=UpperCAmelCase_						)
                         self.assertEqual(encoding.offset_mapping[0]       , (0, len(UpperCAmelCase_						))						)
                         self.assertEqual(
                             encoding.offset_mapping[1]       , (len(UpperCAmelCase_						) + 1, len(UpperCAmelCase_						) + 1 + len(UpperCAmelCase_						))       , )

                         lowerCAmelCase   =		F""" {text}"""

                         lowerCAmelCase   =		self.rust_tokenizer_class.from_pretrained(
                             UpperCAmelCase_       , use_fast=UpperCAmelCase_       , )
                         lowerCAmelCase   =		tokenizer_r(UpperCAmelCase_       , return_offsets_mapping=UpperCAmelCase_       , add_special_tokens=UpperCAmelCase_						)
                         self.assertEqual(encoding.offset_mapping[0]       , (1, 1 + len(UpperCAmelCase_						))						)
                         self.assertEqual(
                             encoding.offset_mapping[1]       , (1 + len(UpperCAmelCase_						) + 1, 1 + len(UpperCAmelCase_						) + 1 + len(UpperCAmelCase_						))       , )



    def       __snake_case		( self						):
           # Test related to the breaking change introduced in transformers v4.17.0
           # We need to check that an error in raised when the user try to load a previous version of the tokenizer.
           with self.assertRaises(UpperCAmelCase_						) as context:
                  self.rust_tokenizer_class.from_pretrained('''robot-test/old-clip-tokenizer'''						)

           self.assertTrue(
               context.exception.args[0].startswith(
                   '''The `backend_tokenizer` provided does not match the expected format.'''						)						)



    @require_ftfy
    def       __snake_case		( self						):
           super().test_tokenization_python_rust_equals()






    def       __snake_case		( self						):
           # CLIP always lower cases letters
           pass









from collections.abc import Sequence



def       UpperCAmelCase       (     _snake_case						, _snake_case = False							):
							if not arr:
														return 0

							lowerCAmelCase   =		0 if allow_empty_subarrays else float('''-inf'''							)
							lowerCAmelCase   =		0.0
							for num in arr:
														lowerCAmelCase   =		max(0 if allow_empty_subarrays else num						, curr_sum + num							)
														lowerCAmelCase   =		max(_snake_case						, _snake_case							)

							return max_sum


if __name__ == "__main__":
		from doctest import testmod

		testmod()

		UpperCAmelCase_						=[-2, 1, -3, 4, -1, 2, 1, -5, 4]
		print(F'''{max_subarray_sum(nums) = }''')



