





import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


lowerCAmelCase_        =			(30, 487)
lowerCAmelCase_        =			(36, 1024)
lowerCAmelCase_        =			(100, 100)
lowerCAmelCase_        =			100

lowerCAmelCase_        =			datasets.Features(
    {"""text""": ArrayaD(SHAPE_TEST_1, dtype="""float32"""), """image""": ArrayaD(SHAPE_TEST_2, dtype="""float32""")}
)

lowerCAmelCase_      ,							lowerCAmelCase_        =			os.path.split(__file__)
lowerCAmelCase_        =			os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json"""))


@get_duration
def 			lowerCamelCase_		(					lowerCAmelCase:		Optional[Any]			,							lowerCAmelCase:		Dict			,							lowerCAmelCase:		Optional[Any] )->						Optional[Any]:
  with ArrowWriter(features=lowerCAmelCase			,							path=os.path.join(lowerCAmelCase			,							'beta.arrow' ) ) as writer:
    for key, record in dummy_data:
      _snake_case      : Optional[int]														=		my_features.encode_example(lowerCAmelCase )
      writer.write(lowerCAmelCase )
    _snake_case							,    _snake_case      : Any														=		writer.finalize()


@get_duration
def 			lowerCamelCase_		(					lowerCAmelCase:		Dict			,							lowerCAmelCase:		Optional[Any] )->						Optional[int]:
  _snake_case      : Tuple														=		datasets.Dataset.from_file(
      filename=os.path.join(lowerCAmelCase			,							'beta.arrow' )			,							info=datasets.DatasetInfo(features=lowerCAmelCase ) )
  for _ in dataset:
    pass


@get_duration
def 			lowerCamelCase_		(					lowerCAmelCase:		Optional[int]			,							lowerCAmelCase:		int )->						Tuple:
  _snake_case      : str														=		datasets.Dataset.from_file(
      filename=os.path.join(lowerCAmelCase			,							'beta.arrow' )			,							info=datasets.DatasetInfo(features=lowerCAmelCase ) )
  dataset.set_format('numpy' )
  for _ in dataset:
    pass


@get_duration
def 			lowerCamelCase_		(					lowerCAmelCase:		Any			,							lowerCAmelCase:		List[str] )->						int:
  _snake_case      : Union[str, Any]														=		10
  _snake_case      : Optional[int]														=		datasets.Dataset.from_file(
      filename=os.path.join(lowerCAmelCase			,							'beta.arrow' )			,							info=datasets.DatasetInfo(features=lowerCAmelCase ) )
  for i in range(0			,							len(lowerCAmelCase )			,							lowerCAmelCase ):
    _snake_case      : str														=		dataset[i : i + batch_size]


@get_duration
def 			lowerCamelCase_		(					lowerCAmelCase:		Dict			,							lowerCAmelCase:		Tuple )->						int:
  _snake_case      : Tuple														=		10
  _snake_case      : Union[str, Any]														=		datasets.Dataset.from_file(
      filename=os.path.join(lowerCAmelCase			,							'beta.arrow' )			,							info=datasets.DatasetInfo(features=lowerCAmelCase ) )
  dataset.set_format('numpy' )
  for i in range(0			,							len(lowerCAmelCase )			,							lowerCAmelCase ):
    _snake_case      : Any														=		dataset[i : i + batch_size]


@get_duration
def 			lowerCamelCase_		(					lowerCAmelCase:		Dict			,							lowerCAmelCase:		str )->						Optional[int]:
  _snake_case      : Tuple														=		datasets.Dataset.from_file(
      filename=os.path.join(lowerCAmelCase			,							'beta.arrow' )			,							info=datasets.DatasetInfo(features=lowerCAmelCase ) )
  for col in feats:
    _snake_case      : str														=		dataset[col]


@get_duration
def 			lowerCamelCase_		(					lowerCAmelCase:		Tuple			,							lowerCAmelCase:		List[str] )->						str:
  _snake_case      : int														=		datasets.Dataset.from_file(
      filename=os.path.join(lowerCAmelCase			,							'beta.arrow' )			,							info=datasets.DatasetInfo(features=lowerCAmelCase ) )
  dataset.set_format('numpy' )
  for col in feats:
    _snake_case      : Any														=		dataset[col]


def 			lowerCamelCase_		(					)->						Union[str, Any]:
  _snake_case      : List[str]														=		{}
  _snake_case      : Union[str, Any]														=		(
      read_unformated,
      read_formatted_as_numpy,
      read_batch_unformated,
      read_batch_formatted_as_numpy,
      read_col_unformated,
      read_col_formatted_as_numpy,
  )
  with tempfile.TemporaryDirectory() as tmp_dir:
    _snake_case      : Union[str, Any]														=		datasets.Features({'image': ArrayaD(lowerCAmelCase			,							dtype='float32' )} )
    _snake_case      : Dict														=		generate_examples(features=lowerCAmelCase			,							num_examples=lowerCAmelCase )
    _snake_case      : List[str]														=		write(lowerCAmelCase			,							lowerCAmelCase			,							lowerCAmelCase )
    for read_func in read_functions:
      _snake_case      : int														=		read_func(lowerCAmelCase			,							lowerCAmelCase )

  with tempfile.TemporaryDirectory() as tmp_dir:
    # don't use fixed length for fair comparison
    # feats = datasets.Features(
    #     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
    # )
    _snake_case      : List[Any]														=		datasets.Features({'image': datasets.Sequence(datasets.Sequence(datasets.Value('float32' ) ) )} )
    _snake_case      : Any														=		generate_examples(
        features=lowerCAmelCase			,							num_examples=lowerCAmelCase			,							seq_shapes={'image': SPEED_TEST_SHAPE} )
    _snake_case      : Dict														=		write(lowerCAmelCase			,							lowerCAmelCase			,							lowerCAmelCase )
    for read_func in read_functions:
      _snake_case      : Dict														=		read_func(lowerCAmelCase			,							lowerCAmelCase )

  with tempfile.TemporaryDirectory() as tmp_dir:
    # don't use fixed length for fair comparison
    # feats = datasets.Features(
    #     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
    # )
    _snake_case      : str														=		datasets.Features({'image': datasets.Sequence(datasets.Value('float32' ) )} )
    _snake_case      : Dict														=		generate_examples(
        features=lowerCAmelCase			,							num_examples=lowerCAmelCase			,							seq_shapes={'image': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]}			,							)
    _snake_case      : Optional[int]														=		write(lowerCAmelCase			,							lowerCAmelCase			,							lowerCAmelCase )
    for read_func in read_functions:
      _snake_case      : Union[str, Any]														=		read_func(lowerCAmelCase			,							lowerCAmelCase )

  with open(lowerCAmelCase			,							'wb' ) as f:
    f.write(json.dumps(lowerCAmelCase ).encode('utf-8' ) )


if __name__ == "__main__":  # useful to run the profiler
      benchmark_array_xd()











def 			lowerCamelCase_		(					lowerCAmelCase:		str			,							lowerCAmelCase:		str )->						int:
  # The longer word should come first
  if len(lowerCAmelCase ) < len(lowerCAmelCase ):
    return levenshtein_distance(lowerCAmelCase			,							lowerCAmelCase )

  if len(lowerCAmelCase ) == 0:
    return len(lowerCAmelCase )

  _snake_case      : Dict														=		list(range(len(lowerCAmelCase ) + 1 ) )

  for i, ca in enumerate(lowerCAmelCase ):
    _snake_case      : Optional[int]														=		[i + 1]

    for j, ca in enumerate(lowerCAmelCase ):
      # Calculate insertions, deletions and substitutions
      _snake_case      : Optional[Any]														=		previous_row[j + 1] + 1
      _snake_case      : Optional[int]														=		current_row[j] + 1
      _snake_case      : Union[str, Any]														=		previous_row[j] + (ca != ca)

      # Get the minimum to append to the current row
      current_row.append(min(lowerCAmelCase			,							lowerCAmelCase			,							lowerCAmelCase ) )

    # Store the previous row
    _snake_case      : str														=		current_row

  # Returns the last element (distance)
  return previous_row[-1]


if __name__ == "__main__":
      lowerCAmelCase_        =			input("""Enter the first word:\n""").strip()
      lowerCAmelCase_        =			input("""Enter the second word:\n""").strip()

      lowerCAmelCase_        =			levenshtein_distance(first_word, second_word)
      print(F"""Levenshtein distance between {first_word} and {second_word} is {result}""")




