





from __future__ import annotations

from collections import deque

class       A_			:
	def __init__(       self				,   _A						):






				'''simple docstring'''







				UpperCAmelCase  						=							[]
				self.adlist.append(
				    {'''value''': '''''', '''next_states''': [], '''fail_state''': 0, '''output''': []}						)

				for keyword in keywords:
							self.add_keyword(lowerCamelCase__						)
				self.set_fail_transitions()
	def 	_lowercase							(       self				,   _A				,   _A						):






				'''simple docstring'''







				for state in self.adlist[current_state]["next_states"]:
							if char == self.adlist[state]["value"]:
										return state
				return None
	def 	_lowercase							(       self				,   _A						):






				'''simple docstring'''







				UpperCAmelCase  						=							0
				for character in keyword:
							UpperCAmelCase  						=							self.find_next_state(lowerCamelCase__				,   lowerCamelCase__						)
							if next_state is None:
										self.adlist.append(
										    {
										        '''value''': character,
										        '''next_states''': [],
										        '''fail_state''': 0,
										        '''output''': [],
										    }						)
										self.adlist[current_state]["next_states"].append(len(self.adlist						) - 1						)
										UpperCAmelCase  						=							len(self.adlist						) - 1
							else:
										UpperCAmelCase  						=							next_state
				self.adlist[current_state]["output"].append(lowerCamelCase__						)
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							deque()
				for node in self.adlist[0]["next_states"]:
							q.append(lowerCamelCase__						)
							UpperCAmelCase  						=							0
				while q:
							UpperCAmelCase  						=							q.popleft()
							for child in self.adlist[r]["next_states"]:
										q.append(lowerCamelCase__						)
										UpperCAmelCase  						=							self.adlist[r]['''fail_state''']
										while (
										    self.find_next_state(lowerCamelCase__				,   self.adlist[child]['''value''']						) is None
										    and state != 0
										):
													UpperCAmelCase  						=							self.adlist[state]['''fail_state''']
										UpperCAmelCase  						=							self.find_next_state(
										    lowerCamelCase__				,   self.adlist[child]['''value''']						)
										if self.adlist[child]["fail_state"] is None:
													UpperCAmelCase  						=							0
										UpperCAmelCase  						=							(
										    self.adlist[child]['''output''']
										    + self.adlist[self.adlist[child]['''fail_state''']]['''output''']
										)







	def 	_lowercase							(       self				,   _A						):






				'''simple docstring'''







				UpperCAmelCase  						=							{}  # returns a dict with keywords and list of its occurrences
				UpperCAmelCase  						=							0
				for i in range(len(lowerCamelCase__						)						):
							while (
							    self.find_next_state(lowerCamelCase__				,   string[i]						) is None
							    and current_state != 0
							):
										UpperCAmelCase  						=							self.adlist[current_state]['''fail_state''']
							UpperCAmelCase  						=							self.find_next_state(lowerCamelCase__				,   string[i]						)
							if next_state is None:
										UpperCAmelCase  						=							0
							else:
										UpperCAmelCase  						=							next_state
										for key in self.adlist[current_state]["output"]:
													if key not in result:
																UpperCAmelCase  						=							[]
													result[key].append(i - len(lowerCamelCase__						) + 1						)
				return result


if __name__ == "__main__":
				import doctest

				doctest.testmod()









import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch

					from transformers.activations import gelu_new, gelu_python, get_activation







@require_torch
class        __lowerCAmelCase					(			unittest.TestCase					):

			"""simple docstring"""







			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertTrue(torch.allclose(gelu_python(lowerCamelCase__				)     ,		torch_builtin(lowerCamelCase__				)				)				)
							self.assertFalse(torch.allclose(gelu_python(lowerCamelCase__				)     ,		gelu_new(lowerCamelCase__				)				)				)


			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       get_activation('gelu_10'				)

							__lowerCamelCase									=       torch_builtin(lowerCamelCase__				)
							__lowerCamelCase									=       geluaa(lowerCamelCase__				)

							__lowerCamelCase									=       torch.where(y_gelu_aa < 10.0     ,		1     ,		0				)

							self.assertTrue(torch.max(lowerCamelCase__				).item() == 10.0				)
							self.assertTrue(torch.allclose(y_gelu * clipped_mask     ,		y_gelu_aa * clipped_mask				)				)


			def  lowercase_       ( self				)       -> Any:



							'''simple docstring'''
							get_activation('gelu'				)
							get_activation('gelu_10'				)
							get_activation('gelu_fast'				)
							get_activation('gelu_new'				)
							get_activation('gelu_python'				)
							get_activation('gelu_pytorch_tanh'				)
							get_activation('linear'				)
							get_activation('mish'				)
							get_activation('quick_gelu'				)
							get_activation('relu'				)
							get_activation('sigmoid'				)
							get_activation('silu'				)
							get_activation('swish'				)
							get_activation('tanh'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation('bogus'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation(lowerCamelCase__				)






			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       1
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertEqual(acta.a     ,		1				)
							with self.assertRaises(lowerCamelCase__				):
											__lowerCamelCase									=       acta.a


