




from __future__ import annotations

from collections import deque




class       _A				:






		def __init__(    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			[]
								self.adlist.append(
								    {"""value""": """""", """next_states""": [], """fail_state""": 0, """output""": []}			)

								for keyword in keywords:
														self.add_keyword(_SCREAMING_SNAKE_CASE			)
								self.set_fail_transitions()






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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			0
								for character in keyword:
														_UpperCAmelCase      =			self.find_next_state(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)
														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(_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			deque()
								for node in self.adlist[0]["next_states"]:
														q.append(_SCREAMING_SNAKE_CASE			)
														_UpperCAmelCase      =			0
								while q:
														_UpperCAmelCase      =			q.popleft()
														for child in self.adlist[r]["next_states"]:
																				q.append(_SCREAMING_SNAKE_CASE			)
																				_UpperCAmelCase      =			self.adlist[r]["""fail_state"""]
																				while (
																				    self.find_next_state(_SCREAMING_SNAKE_CASE						,		self.adlist[child]["""value"""]			) is None
																				    and state != 0
																				):
																										_UpperCAmelCase      =			self.adlist[state]["""fail_state"""]
																				_UpperCAmelCase      =			self.find_next_state(
																				    _SCREAMING_SNAKE_CASE						,		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 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			{}  # returns a dict with keywords and list of its occurrences
								_UpperCAmelCase      =			0
								for i in range(len(_SCREAMING_SNAKE_CASE			)			):
														while (
														    self.find_next_state(_SCREAMING_SNAKE_CASE						,		string[i]			) is None
														    and current_state != 0
														):
																				_UpperCAmelCase      =			self.adlist[current_state]["""fail_state"""]
														_UpperCAmelCase      =			self.find_next_state(_SCREAMING_SNAKE_CASE						,		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(_SCREAMING_SNAKE_CASE			) + 1			)
								return result


if __name__ == "__main__":
				import doctest

				doctest.testmod()





from typing import Dict

from .base import GenericTensor, Pipeline




class       _A				(		__lowercase     ):






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE=None						,		_SCREAMING_SNAKE_CASE=None						,		_SCREAMING_SNAKE_CASE=None						,		**_SCREAMING_SNAKE_CASE			):
								if tokenize_kwargs is None:
														_UpperCAmelCase      =			{}

								if truncation is not None:
														if "truncation" in tokenize_kwargs:
																				raise ValueError(
																				    """truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)"""			)
														_UpperCAmelCase      =			truncation

								_UpperCAmelCase      =			tokenize_kwargs

								_UpperCAmelCase      =			{}
								if return_tensors is not None:
														_UpperCAmelCase      =			return_tensors

								return preprocess_params, {}, postprocess_params






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.framework
								_UpperCAmelCase      =			self.tokenizer(_SCREAMING_SNAKE_CASE						,		return_tensors=_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			)
								return model_inputs






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.model(**_SCREAMING_SNAKE_CASE			)
								return model_outputs






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE=False			):
								# [0] is the first available tensor, logits or last_hidden_state.
								if return_tensors:
														return model_outputs[0]
								if self.framework == "pt":
														return model_outputs[0].tolist()
								elif self.framework == "tf":
														return model_outputs[0].numpy().tolist()






		def __call__(    self						,		*_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			):
								return super().__call__(*_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			)