"""simple docstring"""


import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
    from transformers import SpeechaTextFeatureExtractor

__UpperCamelCase									=			random.Random()







def 					UpperCAmelCase    (		UpperCAmelCase						,					UpperCAmelCase=1.0						,					UpperCAmelCase=None						,					UpperCAmelCase=None					)				->      str:
     if rng is None:
          snake_case_					       =					global_rng

     snake_case_					       =					[]
     for batch_idx in range(shape[0]					):
          values.append([]					)
          for _ in range(shape[1]					):
               values[-1].append(rng.random() * scale					)

     return values




@require_torch
@require_torchaudio
class  UpperCamelCase					(				unittest.TestCase   ):







       def __init__( self,							lowerCAmelCase__,							lowerCAmelCase__=7,							lowerCAmelCase__=400,							lowerCAmelCase__=2000,							lowerCAmelCase__=24,							lowerCAmelCase__=24,							lowerCAmelCase__=0.0,							lowerCAmelCase__=1_6000,							lowerCAmelCase__=True,							lowerCAmelCase__=True,							)      ->      Optional[Any]:
            snake_case_					       =					parent
            snake_case_					       =					batch_size
            snake_case_					       =					min_seq_length
            snake_case_					       =					max_seq_length
            snake_case_					       =					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
            snake_case_					       =					feature_size
            snake_case_					       =					num_mel_bins
            snake_case_					       =					padding_value
            snake_case_					       =					sampling_rate
            snake_case_					       =					return_attention_mask
            snake_case_					       =					do_normalize







       def 		a_						( self)      ->      Optional[Any]:
            return {
                "feature_size": self.feature_size,
                "num_mel_bins": self.num_mel_bins,
                "padding_value": self.padding_value,
                "sampling_rate": self.sampling_rate,
                "return_attention_mask": self.return_attention_mask,
                "do_normalize": self.do_normalize,
            }







       def 		a_						( self,							lowerCAmelCase__=False,							lowerCAmelCase__=False)      ->      int:
            def _flatten(lowerCAmelCase__):
                 return list(itertools.chain(*lowerCAmelCase__))

            if equal_length:
                 snake_case_					       =					[floats_list((self.max_seq_length, self.feature_size)) for _ in range(self.batch_size)]
            else:
                 # make sure that inputs increase in size
                 snake_case_					       =					[
                     floats_list((x, self.feature_size))
                     for x in range(self.min_seq_length,							self.max_seq_length,							self.seq_length_diff)
                 ]
            if numpify:
                 snake_case_					       =					[np.asarray(lowerCAmelCase__) for x in speech_inputs]
            return speech_inputs




@require_torch
@require_torchaudio
class  UpperCamelCase					(				lowerCAmelCase__    ,		unittest.TestCase   ):
       SCREAMING_SNAKE_CASE_			=      SpeechaTextFeatureExtractor if is_speech_available() else None







       def 		a_						( self)      ->      int:
            snake_case_					       =					SpeechaTextFeatureExtractionTester(self)







       def 		a_						( self,							lowerCAmelCase__)      ->      Tuple:
            self.assertTrue(np.all(np.mean(lowerCAmelCase__,							axis=0) < 1e-3))
            self.assertTrue(np.all(np.abs(np.var(lowerCAmelCase__,							axis=0) - 1) < 1e-3))







       def 		a_						( self)      ->      int:
            # Tests that all call wrap to encode_plus and batch_encode_plus
            snake_case_					       =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            # create three inputs of length 800, 1000, and 1200
            snake_case_					       =					[floats_list((1, x))[0] for x in range(800,							1400,							200)]
            snake_case_					       =					[np.asarray(lowerCAmelCase__) for speech_input in speech_inputs]

            # Test feature size
            snake_case_					       =					feature_extractor(lowerCAmelCase__,							padding=lowerCAmelCase__,							return_tensors='np').input_features
            self.assertTrue(input_features.ndim == 3)
            self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size)

            # Test not batched input
            snake_case_					       =					feature_extractor(speech_inputs[0],							return_tensors='np').input_features
            snake_case_					       =					feature_extractor(np_speech_inputs[0],							return_tensors='np').input_features
            self.assertTrue(np.allclose(lowerCAmelCase__,							lowerCAmelCase__,							atol=1e-3))

            # Test batched
            snake_case_					       =					feature_extractor(lowerCAmelCase__,							return_tensors='np').input_features
            snake_case_					       =					feature_extractor(lowerCAmelCase__,							return_tensors='np').input_features
            for enc_seq_a, enc_seq_a in zip(lowerCAmelCase__,							lowerCAmelCase__):
                 self.assertTrue(np.allclose(lowerCAmelCase__,							lowerCAmelCase__,							atol=1e-3))

            # Test 2-D numpy arrays are batched.
            snake_case_					       =					[floats_list((1, x))[0] for x in (800, 800, 800)]
            snake_case_					       =					np.asarray(lowerCAmelCase__)
            snake_case_					       =					feature_extractor(lowerCAmelCase__,							return_tensors='np').input_features
            snake_case_					       =					feature_extractor(lowerCAmelCase__,							return_tensors='np').input_features
            for enc_seq_a, enc_seq_a in zip(lowerCAmelCase__,							lowerCAmelCase__):
                 self.assertTrue(np.allclose(lowerCAmelCase__,							lowerCAmelCase__,							atol=1e-3))







       def 		a_						( self)      ->      Optional[Any]:
            snake_case_					       =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            snake_case_					       =					[floats_list((1, x))[0] for x in range(800,							1400,							200)]

            snake_case_					       =					['longest', 'max_length', 'do_not_pad']
            snake_case_					       =					[None, 16, None]
            for max_length, padding in zip(lowerCAmelCase__,							lowerCAmelCase__):
                 snake_case_					       =					feature_extractor(
                     lowerCAmelCase__,							padding=lowerCAmelCase__,							max_length=lowerCAmelCase__,							return_attention_mask=lowerCAmelCase__)
                 snake_case_					       =					inputs.input_features
                 snake_case_					       =					inputs.attention_mask
                 snake_case_					       =					[np.sum(lowerCAmelCase__) for x in attention_mask]

                 self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]])
                 self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]])
                 self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]])







       def 		a_						( self)      ->      Dict:
            snake_case_					       =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            snake_case_					       =					[floats_list((1, x))[0] for x in range(800,							1400,							200)]

            snake_case_					       =					['longest', 'max_length', 'do_not_pad']
            snake_case_					       =					[None, 16, None]
            for max_length, padding in zip(lowerCAmelCase__,							lowerCAmelCase__):
                 snake_case_					       =					feature_extractor(
                     lowerCAmelCase__,							max_length=lowerCAmelCase__,							padding=lowerCAmelCase__,							return_tensors='np',							return_attention_mask=lowerCAmelCase__)
                 snake_case_					       =					inputs.input_features
                 snake_case_					       =					inputs.attention_mask
                 snake_case_					       =					[np.sum(lowerCAmelCase__) for x in attention_mask]

                 self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]])
                 self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6)
                 self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]])
                 self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6)
                 self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]])







       def 		a_						( self)      ->      str:
            snake_case_					       =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            snake_case_					       =					[floats_list((1, x))[0] for x in range(800,							1400,							200)]
            snake_case_					       =					feature_extractor(
                lowerCAmelCase__,							padding='max_length',							max_length=4,							truncation=lowerCAmelCase__,							return_tensors='np',							return_attention_mask=lowerCAmelCase__,							)
            snake_case_					       =					inputs.input_features
            snake_case_					       =					inputs.attention_mask
            snake_case_					       =					np.sum(attention_mask == 1,							axis=1)

            self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]])
            self._check_zero_mean_unit_variance(input_features[1])
            self._check_zero_mean_unit_variance(input_features[2])







       def 		a_						( self)      ->      Optional[Any]:
            snake_case_					       =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            snake_case_					       =					[floats_list((1, x))[0] for x in range(800,							1400,							200)]
            snake_case_					       =					feature_extractor(
                lowerCAmelCase__,							padding='longest',							max_length=4,							truncation=lowerCAmelCase__,							return_tensors='np',							return_attention_mask=lowerCAmelCase__,							)
            snake_case_					       =					inputs.input_features
            snake_case_					       =					inputs.attention_mask
            snake_case_					       =					np.sum(attention_mask == 1,							axis=1)

            self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]])
            self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]])
            self._check_zero_mean_unit_variance(input_features[2])

            # make sure that if max_length < longest -> then pad to max_length
            self.assertEqual(input_features.shape,							(3, 4, 24))

            snake_case_					       =					[floats_list((1, x))[0] for x in range(800,							1400,							200)]
            snake_case_					       =					feature_extractor(
                lowerCAmelCase__,							padding='longest',							max_length=16,							truncation=lowerCAmelCase__,							return_tensors='np',							return_attention_mask=lowerCAmelCase__,							)
            snake_case_					       =					inputs.input_features
            snake_case_					       =					inputs.attention_mask
            snake_case_					       =					np.sum(attention_mask == 1,							axis=1)

            self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]])
            self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]])
            self._check_zero_mean_unit_variance(input_features[2])

            # make sure that if max_length < longest -> then pad to max_length
            self.assertEqual(input_features.shape,							(3, 6, 24))







       def 		a_						( self)      ->      Union[str, Any]:
            import torch

            snake_case_					       =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            snake_case_					       =					np.random.rand(100,							32).astype(np.floataa)
            snake_case_					       =					np_speech_inputs.tolist()

            for inputs in [py_speech_inputs, np_speech_inputs]:
                 snake_case_					       =					feature_extractor.pad([{'input_features': inputs}],							return_tensors='np')
                 self.assertTrue(np_processed.input_features.dtype == np.floataa)
                 snake_case_					       =					feature_extractor.pad([{'input_features': inputs}],							return_tensors='pt')
                 self.assertTrue(pt_processed.input_features.dtype == torch.floataa)







       def 		a_						( self,							lowerCAmelCase__)      ->      Union[str, Any]:
            from datasets import load_dataset

            snake_case_					       =					load_dataset('hf-internal-testing/librispeech_asr_dummy',							'clean',							split='validation')
            # automatic decoding with librispeech
            snake_case_					       =					ds.sort('id').select(range(lowerCAmelCase__))[:num_samples]['audio']

            return [x["array"] for x in speech_samples]







       def 		a_						( self)      ->      Optional[Any]:
            # fmt: off
            snake_case_					       =					np.array([
                -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
                -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
                -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
            ])
            # fmt: on

            snake_case_					       =					self._load_datasamples(1)
            snake_case_					       =					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            snake_case_					       =					feature_extractor(lowerCAmelCase__,							return_tensors='pt').input_features
            self.assertEquals(input_features.shape,							(1, 584, 24))
            self.assertTrue(np.allclose(input_features[0, 0, :30],							lowerCAmelCase__,							atol=1e-4))


from __future__ import annotations

from collections import deque





class      A							:


   def __init__(self     :						Dict       ,  __UpperCAmelCase     :						list[str]		)						->				Optional[int]:
         """simple docstring"""



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

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


   def 			lowercase_      (self     :						Tuple       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						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 			lowercase_      (self     :						Dict       ,  __UpperCAmelCase     :						str		)						->				None:
         """simple docstring"""



         UpperCAmelCase__								=  0
         for character in keyword:
               UpperCAmelCase__								=  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__								=  len(self.adlist		) - 1
               else:
                     UpperCAmelCase__								=  next_state
         self.adlist[current_state]["output"].append(__UpperCAmelCase		)


   def 			lowercase_      (self     :						Optional[int]		)						->				None:
         """simple docstring"""



         UpperCAmelCase__								=  deque()
         for node in self.adlist[0]["next_states"]:
               q.append(__UpperCAmelCase		)
               UpperCAmelCase__								=  0
         while q:
               UpperCAmelCase__								=  q.popleft()
               for child in self.adlist[r]["next_states"]:
                     q.append(__UpperCAmelCase		)
                     UpperCAmelCase__								=  self.adlist[r]["fail_state"]
                     while (
                         self.find_next_state(__UpperCAmelCase       ,  self.adlist[child]["value"]		) is None
                         and state != 0
                     ):
                           UpperCAmelCase__								=  self.adlist[state]["fail_state"]
                     UpperCAmelCase__								=  self.find_next_state(
                         __UpperCAmelCase       ,  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     :						Union[str, Any]       ,  __UpperCAmelCase     :						str		)						->				dict[str, list[int]]:
         """simple docstring"""



         UpperCAmelCase__								=  {}  # returns a dict with keywords and list of its occurrences
         UpperCAmelCase__								=  0
         for i in range(len(__UpperCAmelCase		)		):
               while (
                   self.find_next_state(__UpperCAmelCase       ,  string[i]		) is None
                   and current_state != 0
               ):
                     UpperCAmelCase__								=  self.adlist[current_state]["fail_state"]
               UpperCAmelCase__								=  self.find_next_state(__UpperCAmelCase       ,  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(__UpperCAmelCase		) + 1		)
         return result


if __name__ == "__main__":
      import doctest

      doctest.testmod()



