






'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import numpy
			import tensorflow as tf

			from transformers import (
			    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    BertConfig,
			    DPRConfig,
			    TFDPRContextEncoder,
			    TFDPRQuestionEncoder,
			    TFDPRReader,
			)

class _lowercase	:




							def __init__(							self:				int			,							UpperCamelCase__:				Union[str, Any]			,							UpperCamelCase__:				Optional[Any]=13			,							UpperCamelCase__:				Dict=7			,							UpperCamelCase__:				Tuple=True			,							UpperCamelCase__:				Any=True			,							UpperCamelCase__:				List[str]=True			,							UpperCamelCase__:				Optional[Any]=True			,							UpperCamelCase__:				List[str]=99			,							UpperCamelCase__:				Dict=32			,							UpperCamelCase__:				int=2			,							UpperCamelCase__:				Any=4			,							UpperCamelCase__:				Optional[int]=37			,							UpperCamelCase__:				List[Any]="gelu"			,							UpperCamelCase__:				int=0.1			,							UpperCamelCase__:				List[str]=0.1			,							UpperCamelCase__:				Dict=512			,							UpperCamelCase__:				Union[str, Any]=16			,							UpperCamelCase__:				Optional[int]=2			,							UpperCamelCase__:				int=0.02			,							UpperCamelCase__:				Tuple=3			,							UpperCamelCase__:				Optional[Any]=4			,							UpperCamelCase__:				Optional[int]=None			,							UpperCamelCase__:				List[Any]=0			,							):
														lowerCamelCase__					:	str         =       parent
														lowerCamelCase__					:	Tuple         =       batch_size
														lowerCamelCase__					:	Optional[int]         =       seq_length
														lowerCamelCase__					:	Dict         =       is_training
														lowerCamelCase__					:	Optional[int]         =       use_input_mask
														lowerCamelCase__					:	Optional[Any]         =       use_token_type_ids
														lowerCamelCase__					:	Optional[Any]         =       use_labels
														lowerCamelCase__					:	Tuple         =       vocab_size
														lowerCamelCase__					:	List[str]         =       hidden_size
														lowerCamelCase__					:	List[Any]         =       num_hidden_layers
														lowerCamelCase__					:	Any         =       num_attention_heads
														lowerCamelCase__					:	Optional[int]         =       intermediate_size
														lowerCamelCase__					:	List[str]         =       hidden_act
														lowerCamelCase__					:	Dict         =       hidden_dropout_prob
														lowerCamelCase__					:	Dict         =       attention_probs_dropout_prob
														lowerCamelCase__					:	str         =       max_position_embeddings
														lowerCamelCase__					:	List[str]         =       type_vocab_size
														lowerCamelCase__					:	Optional[Any]         =       type_sequence_label_size
														lowerCamelCase__					:	Optional[Any]         =       initializer_range
														lowerCamelCase__					:	int         =       num_labels
														lowerCamelCase__					:	List[Any]         =       num_choices
														lowerCamelCase__					:	int         =       scope
														lowerCamelCase__					:	Tuple         =       projection_dim




							def 		lowerCamelCase_				(							self:				List[str]     ):
														lowerCamelCase__					:	Union[str, Any]         =       ids_tensor([self.batch_size, self.seq_length]			,							self.vocab_size     )

														lowerCamelCase__					:	Dict         =       None
														if self.use_input_mask:
																					# follow test_modeling_tf_ctrl.py
																					lowerCamelCase__					:	Optional[Any]         =       random_attention_mask([self.batch_size, self.seq_length]     )

														lowerCamelCase__					:	Tuple         =       None
														if self.use_token_type_ids:
																					lowerCamelCase__					:	List[Any]         =       ids_tensor([self.batch_size, self.seq_length]			,							self.type_vocab_size     )

														lowerCamelCase__					:	Optional[int]         =       None
														lowerCamelCase__					:	int         =       None
														lowerCamelCase__					:	Tuple         =       None
														if self.use_labels:
																					lowerCamelCase__					:	Optional[int]         =       ids_tensor([self.batch_size]			,							self.type_sequence_label_size     )
																					lowerCamelCase__					:	str         =       ids_tensor([self.batch_size, self.seq_length]			,							self.num_labels     )
																					lowerCamelCase__					:	Optional[Any]         =       ids_tensor([self.batch_size]			,							self.num_choices     )

														lowerCamelCase__					:	Union[str, Any]         =       BertConfig(
														    vocab_size=self.vocab_size			,							hidden_size=self.hidden_size			,							num_hidden_layers=self.num_hidden_layers			,							num_attention_heads=self.num_attention_heads			,							intermediate_size=self.intermediate_size			,							hidden_act=self.hidden_act			,							hidden_dropout_prob=self.hidden_dropout_prob			,							attention_probs_dropout_prob=self.attention_probs_dropout_prob			,							max_position_embeddings=self.max_position_embeddings			,							type_vocab_size=self.type_vocab_size			,							is_decoder=UpperCamelCase__			,							initializer_range=self.initializer_range			,							)
														lowerCamelCase__					:	Tuple         =       DPRConfig(projection_dim=self.projection_dim			,							**config.to_dict()     )

														return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




							def 		lowerCamelCase_				(							self:				int			,							UpperCamelCase__:				Union[str, Any]			,							UpperCamelCase__:				str			,							UpperCamelCase__:				List[Any]			,							UpperCamelCase__:				List[Any]			,							UpperCamelCase__:				str			,							UpperCamelCase__:				int			,							UpperCamelCase__:				Any     ):
														lowerCamelCase__					:	Optional[Any]         =       TFDPRContextEncoder(config=UpperCamelCase__     )
														lowerCamelCase__					:	List[Any]         =       model(UpperCamelCase__			,							attention_mask=UpperCamelCase__			,							token_type_ids=UpperCamelCase__     )
														lowerCamelCase__					:	int         =       model(UpperCamelCase__			,							token_type_ids=UpperCamelCase__     )
														lowerCamelCase__					:	str         =       model(UpperCamelCase__     )
														self.parent.assertEqual(result.pooler_output.shape			,							(self.batch_size, self.projection_dim or self.hidden_size)     )




							def 		lowerCamelCase_				(							self:				int			,							UpperCamelCase__:				Union[str, Any]			,							UpperCamelCase__:				List[str]			,							UpperCamelCase__:				List[str]			,							UpperCamelCase__:				List[Any]			,							UpperCamelCase__:				str			,							UpperCamelCase__:				str			,							UpperCamelCase__:				Any     ):
														lowerCamelCase__					:	int         =       TFDPRQuestionEncoder(config=UpperCamelCase__     )
														lowerCamelCase__					:	Any         =       model(UpperCamelCase__			,							attention_mask=UpperCamelCase__			,							token_type_ids=UpperCamelCase__     )
														lowerCamelCase__					:	Optional[int]         =       model(UpperCamelCase__			,							token_type_ids=UpperCamelCase__     )
														lowerCamelCase__					:	Tuple         =       model(UpperCamelCase__     )
														self.parent.assertEqual(result.pooler_output.shape			,							(self.batch_size, self.projection_dim or self.hidden_size)     )




							def 		lowerCamelCase_				(							self:				List[Any]			,							UpperCamelCase__:				Optional[Any]			,							UpperCamelCase__:				str			,							UpperCamelCase__:				List[str]			,							UpperCamelCase__:				Any			,							UpperCamelCase__:				int			,							UpperCamelCase__:				Any			,							UpperCamelCase__:				Tuple     ):
														lowerCamelCase__					:	str         =       TFDPRReader(config=UpperCamelCase__     )
														lowerCamelCase__					:	Optional[Any]         =       model(UpperCamelCase__			,							attention_mask=UpperCamelCase__     )

														self.parent.assertEqual(result.start_logits.shape			,							(self.batch_size, self.seq_length)     )
														self.parent.assertEqual(result.end_logits.shape			,							(self.batch_size, self.seq_length)     )
														self.parent.assertEqual(result.relevance_logits.shape			,							(self.batch_size,)     )




							def 		lowerCamelCase_				(							self:				List[Any]     ):
														lowerCamelCase__					:	Optional[Any]         =       self.prepare_config_and_inputs()
														(
														    (
														    lowerCamelCase__
														)  ,		(
														    lowerCamelCase__
														)  ,		(
														    lowerCamelCase__
														)  ,		(
														    lowerCamelCase__
														)  ,		(
														    lowerCamelCase__
														)  ,		(
														    lowerCamelCase__
														)  ,		(
														    lowerCamelCase__
														)  ,		
														)					:	str         =       config_and_inputs
														lowerCamelCase__					:	Optional[int]         =       {"""input_ids""": input_ids}
														return config, inputs_dict



@require_tf
class _lowercase	(				_lowercase				,      _lowercase				,      unittest.TestCase		):
							a				      =       (
							    (
							        TFDPRContextEncoder,
							        TFDPRQuestionEncoder,
							        TFDPRReader,
							    )
							    if is_tf_available()
							    else ()
							)
							a				      =       {"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

							a				      =       False
							a				      =       False
							a				      =       False
							a				      =       False
							a				      =       False




							def 		lowerCamelCase_				(							self:				Any     ):
														lowerCamelCase__					:	str         =       TFDPRModelTester(self     )
														lowerCamelCase__					:	int         =       ConfigTester(self			,							config_class=UpperCamelCase__			,							hidden_size=37     )




							def 		lowerCamelCase_				(							self:				Dict     ):
														self.config_tester.run_common_tests()




							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														lowerCamelCase__					:	Optional[Any]         =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_dpr_context_encoder(*UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				str     ):
														lowerCamelCase__					:	int         =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_dpr_question_encoder(*UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				Any     ):
														lowerCamelCase__					:	Tuple         =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_dpr_reader(*UpperCamelCase__     )




							@slow
							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	str         =       TFDPRContextEncoder.from_pretrained(UpperCamelCase__     )
																					self.assertIsNotNone(UpperCamelCase__     )

														for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	Optional[int]         =       TFDPRContextEncoder.from_pretrained(UpperCamelCase__     )
																					self.assertIsNotNone(UpperCamelCase__     )

														for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	Tuple         =       TFDPRQuestionEncoder.from_pretrained(UpperCamelCase__     )
																					self.assertIsNotNone(UpperCamelCase__     )

														for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	int         =       TFDPRReader.from_pretrained(UpperCamelCase__     )
																					self.assertIsNotNone(UpperCamelCase__     )



@require_tf
class _lowercase	(				unittest.TestCase		):




							@slow
							def 		lowerCamelCase_				(							self:				Union[str, Any]     ):
														lowerCamelCase__					:	List[str]         =       TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""     )

														lowerCamelCase__					:	Optional[Any]         =       tf.constant(
														    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]     )  # [CLS] hello, is my dog cute? [SEP]
														lowerCamelCase__					:	Optional[Any]         =       model(UpperCamelCase__     )[0]  # embedding shape = (1, 768)
														# compare the actual values for a slice.
														lowerCamelCase__					:	Union[str, Any]         =       tf.constant(
														    [
														        [
														            0.03_236_253,
														            0.12_753_335,
														            0.16_818_509,
														            0.00_279_786,
														            0.3_896_933,
														            0.24_264_945,
														            0.2_178_971,
														            -0.02_335_227,
														            -0.08_481_959,
														            -0.14_324_117,
														        ]
														    ]     )
														self.assertTrue(numpy.allclose(output[:, :10].numpy()			,							expected_slice.numpy()			,							atol=1e-4     )     )





from __future__ import annotations


class        _a							:

							def __init__(							self:	Union[str, Any]    ,						UpperCamelCase_:	str    ,						UpperCamelCase_:	str							)			->   List[Any]:





									"""simple docstring"""







									lowercase__     ,		lowercase__            =      text, pattern
									lowercase__     ,		lowercase__            =      len(UpperCamelCase_							), len(UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	Optional[int]    ,						UpperCamelCase_:	str							)			->   int:





									"""simple docstring"""







									for i in range(self.patLen - 1    ,						-1    ,						-1							):
											if char == self.pattern[i]:
													return i
									return -1

							def       lowerCamelCase_ (							self:	Optional[int]    ,						UpperCamelCase_:	int							)			->   int:





									"""simple docstring"""







									for i in range(self.patLen - 1    ,						-1    ,						-1							):
											if self.pattern[i] != self.text[current_pos + i]:
													return current_pos + i
									return -1


							def       lowerCamelCase_ (							self:	List[Any]							)			->   list[int]:





									"""simple docstring"""







									lowercase__            =      []
									for i in range(self.textLen - self.patLen + 1							):
											lowercase__            =      self.mismatch_in_text(UpperCamelCase_							)
											if mismatch_index == -1:
													positions.append(UpperCamelCase_							)
											else:
													lowercase__            =      self.match_in_pattern(self.text[mismatch_index]							)
													lowercase__            =      (
													    mismatch_index - match_index
													)  # shifting index lgtm [py/multiple-definition]
									return positions
lowerCAmelCase       =							'ABAABA'
lowerCAmelCase       =							'AB'
lowerCAmelCase       =							BoyerMooreSearch(text, pattern)
lowerCAmelCase       =							bms.bad_character_heuristic()

if len(positions) == 0:
			print('No match found')
else:
			print('Pattern found in following positions: ')
			print(positions)


