



'''simple docstring'''





from __future__ import annotations

from collections import deque



class  snake_case__						:
 def __init__(   self						:							Optional[int]      ,     __a						:							list[str]  )      ->   Any:







       '''simple docstring'''
       __snake_case :   list[dict]													=    []
       self.adlist.append(
           {'value': '', 'next_states': [], 'fail_state': 0, 'output': []}  )

       for keyword in keywords:
             self.add_keyword(__a  )
       self.set_fail_transitions()
 def A_   (   self						:							List[Any]      ,     __a						:							int      ,     __a						:							str  )      ->   int | None:







       '''simple docstring'''
       for state in self.adlist[current_state]["next_states"]:
             if char == self.adlist[state]["value"]:
                   return state
       return None
 def A_   (   self						:							Dict      ,     __a						:							str  )      ->   None:







       '''simple docstring'''
       __snake_case :   List[str]													=    0
       for character in keyword:
             __snake_case :   Tuple													=    self.find_next_state(__a      ,     __a  )
             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  )
                   __snake_case :   Union[str, Any]													=    len(self.adlist  ) - 1
             else:
                   __snake_case :   Union[str, Any]													=    next_state
       self.adlist[current_state]["output"].append(__a  )
 def A_   (   self						:							int  )      ->   None:







       '''simple docstring'''
       __snake_case :   deque													=    deque()
       for node in self.adlist[0]["next_states"]:
             q.append(__a  )
             __snake_case :   Union[str, Any]													=    0
       while q:
             __snake_case :   Any													=    q.popleft()
             for child in self.adlist[r]["next_states"]:
                   q.append(__a  )
                   __snake_case :   str													=    self.adlist[r]['fail_state']
                   while (
                       self.find_next_state(__a      ,     self.adlist[child]['value']  ) is None
                       and state != 0
                   ):
                         __snake_case :   Tuple													=    self.adlist[state]['fail_state']
                   __snake_case :   Dict													=    self.find_next_state(
                       __a      ,     self.adlist[child]['value']  )
                   if self.adlist[child]["fail_state"] is None:
                         __snake_case :   Optional[Any]													=    0
                   __snake_case :   Tuple													=    (
                       self.adlist[child]['output']
                       + self.adlist[self.adlist[child]['fail_state']]['output']
                   )





 def A_   (   self						:							str      ,     __a						:							str  )      ->   dict[str, list[int]]:







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


if __name__ == "__main__":
    import doctest

    doctest.testmod()








'''simple docstring'''





import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

A__    :   List[str]           =					logging.getLogger()





def   a_   (		_UpperCAmelCase    :      int				)    ->  Optional[int]:
      __snake_case :   List[str]													=    {}
      __snake_case :   List[str]													=    os.path.join(_UpperCAmelCase ,'all_results.json'				)
      if os.path.exists(_UpperCAmelCase				):
            with open(_UpperCAmelCase ,'r'				) as f:
                  __snake_case :   Dict													=    json.load(_UpperCAmelCase				)
      else:
            raise ValueError(f'''can\'t find {path}'''				)
      return results


A__    :   Dict           =					logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





@require_torch_tpu
class  snake_case__						(    SCREAMING_SNAKE_CASE_				):
 def A_   (   self						:							Optional[Any]  )      ->   List[str]:







       '''simple docstring'''
       import xla_spawn

       __snake_case :   int													=    self.get_auto_remove_tmp_dir()
       __snake_case :   Tuple													=    f'''
            ./examples/pytorch/text-classification/run_glue.py
            --num_cores=8
            ./examples/pytorch/text-classification/run_glue.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --do_train
            --do_eval
            --debug tpu_metrics_debug
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
            --max_seq_length=128
            '''.split()

       with patch.object(__a      ,     'argv'      ,     __a  ):
             __snake_case :   Tuple													=    time()
             xla_spawn.main()
             __snake_case :   Optional[int]													=    time()

             __snake_case :   List[Any]													=    get_results(__a  )
             self.assertGreaterEqual(result['eval_accuracy']      ,     0.7_5  )

             # Assert that the script takes less than 500 seconds to make sure it doesn't hang.
             self.assertLess(end - start      ,     500  )





 def A_   (   self						:							List[str]  )      ->   Optional[int]:







       '''simple docstring'''
       import xla_spawn

       __snake_case :   Optional[Any]													=    '\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            '.split()
       with patch.object(__a      ,     'argv'      ,     __a  ):
             xla_spawn.main()



