
"""simple docstring"""




from __future__ import annotations

from collections import deque







class   _lowercase						:


     '''simple docstring'''





     def __init__(      self ,			__UpperCamelCase  )->					str:
          UpperCAmelCase__							: list[dict] 							=						[]
          self.adlist.append(
              {"value": "", "next_states": [], "fail_state": 0, "output": []}  )

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



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase  )->					int | None:
          for state in self.adlist[current_state]["next_states"]:
               if char == self.adlist[state]["value"]:
                    return state
          return None



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					None:
          UpperCAmelCase__							: Optional[Any] 							=						0
          for character in keyword:
               UpperCAmelCase__							: Tuple 							=						self.find_next_state(__UpperCamelCase ,			__UpperCamelCase  )
               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__							: List[Any] 							=						len(self.adlist  ) - 1
               else:
                    UpperCAmelCase__							: List[Any] 							=						next_state
          self.adlist[current_state]["output"].append(__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					None:
          UpperCAmelCase__							: deque 							=						deque()
          for node in self.adlist[0]["next_states"]:
               q.append(__UpperCamelCase  )
               UpperCAmelCase__							: Dict 							=						0
          while q:
               UpperCAmelCase__							: Union[str, Any] 							=						q.popleft()
               for child in self.adlist[r]["next_states"]:
                    q.append(__UpperCamelCase  )
                    UpperCAmelCase__							: List[Any] 							=						self.adlist[r]["fail_state"]
                    while (
                        self.find_next_state(__UpperCamelCase ,			self.adlist[child]["value"]  ) is None
                        and state != 0
                    ):
                         UpperCAmelCase__							: Tuple 							=						self.adlist[state]["fail_state"]
                    UpperCAmelCase__							: Optional[Any] 							=						self.find_next_state(
                        __UpperCamelCase ,			self.adlist[child]["value"]  )
                    if self.adlist[child]["fail_state"] is None:
                         UpperCAmelCase__							: Any 							=						0
                    UpperCAmelCase__							: Any 							=						(
                        self.adlist[child]["output"]
                        + self.adlist[self.adlist[child]["fail_state"]]["output"]
                    )



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					dict[str, list[int]]:
          UpperCAmelCase__							: dict 							=						{}  # returns a dict with keywords and list of its occurrences
          UpperCAmelCase__							: int 							=						0
          for i in range(len(__UpperCamelCase  )  ):
               while (
                   self.find_next_state(__UpperCamelCase ,			string[i]  ) is None
                   and current_state != 0
               ):
                    UpperCAmelCase__							: Dict 							=						self.adlist[current_state]["fail_state"]
               UpperCAmelCase__							: Optional[Any] 							=						self.find_next_state(__UpperCamelCase ,			string[i]  )
               if next_state is None:
                    UpperCAmelCase__							: List[Any] 							=						0
               else:
                    UpperCAmelCase__							: Optional[Any] 							=						next_state
                    for key in self.adlist[current_state]["output"]:
                         if key not in result:
                              UpperCAmelCase__							: Any 							=						[]
                         result[key].append(i - len(__UpperCamelCase  ) + 1  )
          return result


if __name__ == "__main__":
    import doctest

    doctest.testmod()





"""simple docstring"""




import timeit

import numpy as np

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features.features import _ArrayXD



def 							a__ (				lowerCAmelCase	:       List[str]		):






     '''simple docstring'''
     def wrapper(*lowerCAmelCase	:       Any       ,    **lowerCAmelCase	:       Tuple		):
          UpperCAmelCase__							: Optional[int] 							=						timeit.default_timer()
          UpperCAmelCase__							: int 							=						func(*lowerCAmelCase       ,    **lowerCAmelCase		)
          UpperCAmelCase__							: List[Any] 							=						timeit.default_timer() - starttime
          return delta

     UpperCAmelCase__							: int 							=						func.__name__

     return wrapper



def 							a__ (				lowerCAmelCase	:       dict       ,    lowerCAmelCase	:       Optional[int]=100       ,    lowerCAmelCase	:       List[str]=None		):






     '''simple docstring'''
     UpperCAmelCase__							: str 							=						[]
     UpperCAmelCase__							: Optional[Any] 							=						seq_shapes or {}
     for i in range(lowerCAmelCase		):
          UpperCAmelCase__							: int 							=						{}
          for col_id, (k, v) in enumerate(features.items()		):
               if isinstance(lowerCAmelCase       ,    _ArrayXD		):
                    UpperCAmelCase__							: List[str] 							=						np.random.rand(*v.shape		).astype(v.dtype		)
               elif isinstance(lowerCAmelCase       ,    datasets.Value		):
                    if v.dtype == "string":
                         UpperCAmelCase__							: Dict 							=						"The small grey turtle was surprisingly fast when challenged."
                    else:
                         UpperCAmelCase__							: str 							=						np.random.randint(10       ,    size=1		).astype(v.dtype		).item()
               elif isinstance(lowerCAmelCase       ,    datasets.Sequence		):
                    while isinstance(lowerCAmelCase       ,    datasets.Sequence		):
                         UpperCAmelCase__							: List[str] 							=						v.feature
                    UpperCAmelCase__							: Optional[int] 							=						seq_shapes[k]
                    UpperCAmelCase__							: Optional[int] 							=						np.random.rand(*lowerCAmelCase		).astype(v.dtype		)
               UpperCAmelCase__							: Union[str, Any] 							=						data

          dummy_data.append((i, example)		)

     return dummy_data



def 							a__ (				lowerCAmelCase	:       List[str]       ,    lowerCAmelCase	:       Tuple       ,    lowerCAmelCase	:       List[str]=100       ,    lowerCAmelCase	:       Optional[int]=None		):






     '''simple docstring'''
     UpperCAmelCase__							: int 							=						generate_examples(lowerCAmelCase       ,    num_examples=lowerCAmelCase       ,    seq_shapes=lowerCAmelCase		)

     with ArrowWriter(features=lowerCAmelCase       ,    path=lowerCAmelCase		) as writer:
          for key, record in dummy_data:
               UpperCAmelCase__							: List[Any] 							=						features.encode_example(lowerCAmelCase		)
               writer.write(lowerCAmelCase		)

          UpperCAmelCase__							,	UpperCAmelCase__							: Optional[Any] 							=						writer.finalize()

     if not num_final_examples == num_examples:
          raise ValueError(
              F"Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}."		)

     UpperCAmelCase__							: Optional[int] 							=						datasets.Dataset.from_file(filename=lowerCAmelCase       ,    info=datasets.DatasetInfo(features=lowerCAmelCase		)		)

     return dataset



