






'''simple docstring'''







import json
import os
import shutil
import tempfile
from unittest import TestCase

from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow
from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available


if is_torch_available() and is_datasets_available() and is_faiss_available():
		from transformers.models.rag.configuration_rag import RagConfig
		from transformers.models.rag.tokenization_rag import RagTokenizer





@require_faiss
@require_torch
class 						SCREAMING_SNAKE_CASE       (       SCREAMING_SNAKE_CASE_							):






			def       SCREAMING_SNAKE_CASE   (			self   :      Union[str, Any]       )      ->      Any:
							a_		:	Union[str, Any]									=   tempfile.mkdtemp()
							a_		:	List[Any]									=   8

							# DPR tok
							a_		:	str									=   [
							    '''[UNK]''',
							    '''[CLS]''',
							    '''[SEP]''',
							    '''[PAD]''',
							    '''[MASK]''',
							    '''want''',
							    '''##want''',
							    '''##ed''',
							    '''wa''',
							    '''un''',
							    '''runn''',
							    '''##ing''',
							    ''',''',
							    '''low''',
							    '''lowest''',
							]
							a_		:	Optional[Any]									=   os.path.join(self.tmpdirname	,    '''dpr_tokenizer'''       )
							os.makedirs(__SCREAMING_SNAKE_CASE	,    exist_ok=__SCREAMING_SNAKE_CASE       )
							a_		:	Union[str, Any]									=   os.path.join(__SCREAMING_SNAKE_CASE	,    DPR_VOCAB_FILES_NAMES['''vocab_file''']       )
							with open(self.vocab_file	,    '''w'''	,    encoding='''utf-8'''       ) as vocab_writer:
											vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]       )       )

							# BART tok
							a_		:	int									=   [
							    '''l''',
							    '''o''',
							    '''w''',
							    '''e''',
							    '''r''',
							    '''s''',
							    '''t''',
							    '''i''',
							    '''d''',
							    '''n''',
							    '''\u0120''',
							    '''\u0120l''',
							    '''\u0120n''',
							    '''\u0120lo''',
							    '''\u0120low''',
							    '''er''',
							    '''\u0120lowest''',
							    '''\u0120newer''',
							    '''\u0120wider''',
							    '''<unk>''',
							]
							a_		:	str									=   dict(zip(__SCREAMING_SNAKE_CASE	,    range(len(__SCREAMING_SNAKE_CASE       )       )       )       )
							a_		:	Union[str, Any]									=   ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
							a_		:	Optional[int]									=   {'''unk_token''': '''<unk>'''}

							a_		:	Dict									=   os.path.join(self.tmpdirname	,    '''bart_tokenizer'''       )
							os.makedirs(__SCREAMING_SNAKE_CASE	,    exist_ok=__SCREAMING_SNAKE_CASE       )
							a_		:	List[Any]									=   os.path.join(__SCREAMING_SNAKE_CASE	,    BART_VOCAB_FILES_NAMES['''vocab_file''']       )
							a_		:	str									=   os.path.join(__SCREAMING_SNAKE_CASE	,    BART_VOCAB_FILES_NAMES['''merges_file''']       )
							with open(self.vocab_file	,    '''w'''	,    encoding='''utf-8'''       ) as fp:
											fp.write(json.dumps(__SCREAMING_SNAKE_CASE       ) + '''\n'''       )
							with open(self.merges_file	,    '''w'''	,    encoding='''utf-8'''       ) as fp:
											fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE       )       )






			def       SCREAMING_SNAKE_CASE   (			self   :      Union[str, Any]       )      ->      DPRQuestionEncoderTokenizer:
							return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname	,    '''dpr_tokenizer'''       )       )






			def       SCREAMING_SNAKE_CASE   (			self   :      List[Any]       )      ->      BartTokenizer:
							return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname	,    '''bart_tokenizer'''       )       )






			def       SCREAMING_SNAKE_CASE   (			self   :      Optional[Any]       )      ->      Tuple:
							shutil.rmtree(self.tmpdirname       )






			@require_tokenizers
			def       SCREAMING_SNAKE_CASE   (			self   :      List[str]       )      ->      List[Any]:
							a_		:	int									=   os.path.join(self.tmpdirname	,    '''rag_tokenizer'''       )
							a_		:	str									=   RagConfig(question_encoder=DPRConfig().to_dict()	,    generator=BartConfig().to_dict()       )
							a_		:	int									=   RagTokenizer(question_encoder=self.get_dpr_tokenizer()	,    generator=self.get_bart_tokenizer()       )
							rag_config.save_pretrained(__SCREAMING_SNAKE_CASE       )
							rag_tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE       )
							a_		:	Optional[int]									=   RagTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE	,    config=__SCREAMING_SNAKE_CASE       )
							self.assertIsInstance(new_rag_tokenizer.question_encoder	,    __SCREAMING_SNAKE_CASE       )
							self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab()	,    rag_tokenizer.question_encoder.get_vocab()       )
							self.assertIsInstance(new_rag_tokenizer.generator	,    __SCREAMING_SNAKE_CASE       )
							self.assertEqual(new_rag_tokenizer.generator.get_vocab()	,    rag_tokenizer.generator.get_vocab()       )






			@slow
			def       SCREAMING_SNAKE_CASE   (			self   :      Optional[int]       )      ->      Optional[int]:
							a_		:	List[Any]									=   RagTokenizer.from_pretrained('''facebook/rag-token-nq'''       )
							a_		:	List[Any]									=   [
							    '''who got the first nobel prize in physics''',
							    '''when is the next deadpool movie being released''',
							    '''which mode is used for short wave broadcast service''',
							    '''who is the owner of reading football club''',
							    '''when is the next scandal episode coming out''',
							    '''when is the last time the philadelphia won the superbowl''',
							    '''what is the most current adobe flash player version''',
							    '''how many episodes are there in dragon ball z''',
							    '''what is the first step in the evolution of the eye''',
							    '''where is gall bladder situated in human body''',
							    '''what is the main mineral in lithium batteries''',
							    '''who is the president of usa right now''',
							    '''where do the greasers live in the outsiders''',
							    '''panda is a national animal of which country''',
							    '''what is the name of manchester united stadium''',
							]
							a_		:	Dict									=   tokenizer(__SCREAMING_SNAKE_CASE       )
							self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )







			@slow
			def       SCREAMING_SNAKE_CASE   (			self   :      List[Any]       )      ->      List[Any]:
							a_		:	str									=   RagTokenizer.from_pretrained('''facebook/rag-sequence-nq'''       )
							a_		:	List[Any]									=   [
							    '''who got the first nobel prize in physics''',
							    '''when is the next deadpool movie being released''',
							    '''which mode is used for short wave broadcast service''',
							    '''who is the owner of reading football club''',
							    '''when is the next scandal episode coming out''',
							    '''when is the last time the philadelphia won the superbowl''',
							    '''what is the most current adobe flash player version''',
							    '''how many episodes are there in dragon ball z''',
							    '''what is the first step in the evolution of the eye''',
							    '''where is gall bladder situated in human body''',
							    '''what is the main mineral in lithium batteries''',
							    '''who is the president of usa right now''',
							    '''where do the greasers live in the outsiders''',
							    '''panda is a national animal of which country''',
							    '''what is the name of manchester united stadium''',
							]
							a_		:	Union[str, Any]									=   tokenizer(__SCREAMING_SNAKE_CASE       )
							self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )












'''simple docstring'''







from __future__ import annotations





def 		_UpperCAmelCase (				__A				: str    ,		__A				: str   ):
				a_		:	int									=   get_failure_array(__A   )

				# 2) Step through text searching for pattern
				a_					,      a_		:	Any									=   0, 0  # index into text, pattern
				while i < len(__A   ):
								if pattern[j] == text[i]:
												if j == (len(__A   ) - 1):
																return True
												j += 1

								# if this is a prefix in our pattern
								# just go back far enough to continue
								elif j > 0:
												a_		:	Any									=   failure[j - 1]
												continue
								i += 1
				return False





def 		_UpperCAmelCase (				__A				: str   ):
				a_		:	Optional[Any]									=   [0]
				a_		:	Any									=   0
				a_		:	int									=   1
				while j < len(__A   ):
								if pattern[i] == pattern[j]:
												i += 1
								elif i > 0:
												a_		:	List[Any]									=   failure[i - 1]
												continue
								j += 1
								failure.append(__A   )
				return failure


if __name__ == "__main__":
		# Test 1)
		__lowerCAmelCase									=   'abc1abc12'
		__lowerCAmelCase									=   'alskfjaldsabc1abc1abc12k23adsfabcabc'
		__lowerCAmelCase									=   'alskfjaldsk23adsfabcabc'
		assert kmp(pattern, texta) and not kmp(pattern, texta)

		# Test 2)
		__lowerCAmelCase									=   'ABABX'
		__lowerCAmelCase									=   'ABABZABABYABABX'
		assert kmp(pattern, text)

		# Test 3)
		__lowerCAmelCase									=   'AAAB'
		__lowerCAmelCase									=   'ABAAAAAB'
		assert kmp(pattern, text)

		# Test 4)
		__lowerCAmelCase									=   'abcdabcy'
		__lowerCAmelCase									=   'abcxabcdabxabcdabcdabcy'
		assert kmp(pattern, text)

		# Test 5)
		__lowerCAmelCase									=   'aabaabaaa'
		assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]




