


import unittest

from cer import CER


_snake_case   = CER()




class    A_   ( unittest.TestCase     ):







 """simple docstring"""





 def   _UpperCamelCase    (			self					: Any				)    ->			str:
      UpperCAmelCase						:				List[Any]		  =			["White House"]
      UpperCAmelCase						:				Optional[int]		  =			["white house"]
      # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
      UpperCAmelCase						:				Tuple		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.1_8_1_8_1_8_1_8_1_8				) < 1E-6				)
 def   _UpperCamelCase    (			self					: str				)    ->			List[Any]:
      UpperCAmelCase						:				Union[str, Any]		  =			["were wolf"]
      UpperCAmelCase						:				List[str]		  =			["werewolf"]
      # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
      UpperCAmelCase						:				Tuple		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.1_1_1_1_1_1_1				) < 1E-6				)

      UpperCAmelCase						:				List[Any]		  =			["werewolf"]
      UpperCAmelCase						:				List[Any]		  =			["weae     wolf"]
      # S = 1, D = 1, I = 0, N = 8, CER = 0.25
      UpperCAmelCase						:				int		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.2_5				) < 1E-6				)

      # consecutive whitespaces case 1
      UpperCAmelCase						:				Union[str, Any]		  =			["were wolf"]
      UpperCAmelCase						:				List[str]		  =			["were               wolf"]
      # S = 0, D = 0, I = 0, N = 9, CER = 0
      UpperCAmelCase						:				Optional[Any]		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.0				) < 1E-6				)

      # consecutive whitespaces case 2
      UpperCAmelCase						:				str		  =			["were   wolf"]
      UpperCAmelCase						:				Any		  =			["were               wolf"]
      # S = 0, D = 0, I = 0, N = 9, CER = 0
      UpperCAmelCase						:				Optional[int]		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.0				) < 1E-6				)
 def   _UpperCamelCase    (			self					: int				)    ->			List[str]:
      UpperCAmelCase						:				Optional[int]		  =			["werewolf"]
      UpperCAmelCase						:				int		  =			["weaewolf"]
      # S = 1, D = 0, I = 0, N = 8, CER = 0.125
      UpperCAmelCase						:				int		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.1_2_5				) < 1E-6				)
 def   _UpperCamelCase    (			self					: Dict				)    ->			Optional[int]:
      UpperCAmelCase						:				List[str]		  =			["werewolf"]
      UpperCAmelCase						:				List[Any]		  =			["wereawolf"]
      # S = 0, D = 1, I = 0, N = 8, CER = 0.125
      UpperCAmelCase						:				List[str]		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.1_2_5				) < 1E-6				)
 def   _UpperCamelCase    (			self					: Any				)    ->			int:
      UpperCAmelCase						:				Optional[int]		  =			["werewolf"]
      UpperCAmelCase						:				Optional[int]		  =			["wereolf"]
      # S = 0, D = 0, I = 1, N = 8, CER = 0.125
      UpperCAmelCase						:				int		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.1_2_5				) < 1E-6				)
 def   _UpperCamelCase    (			self					: str				)    ->			Optional[Any]:
      UpperCAmelCase						:				Tuple		  =			["werewolf"]
      UpperCAmelCase						:				str		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertEqual(_a       ,    0.0				)
 def   _UpperCamelCase    (			self					: Tuple				)    ->			Optional[Any]:
      UpperCAmelCase						:				str		  =			["werewolf", "I am your father"]
      UpperCAmelCase						:				List[str]		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertEqual(_a       ,    0.0				)

      UpperCAmelCase						:				Optional[int]		  =			["werewolf", "I am your father", "doge"]
      UpperCAmelCase						:				Union[str, Any]		  =			["werxwolf", "I       am your father", "doge"]
      # S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
      UpperCAmelCase						:				List[str]		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.0_3_5_7_1_4_2_8				) < 1E-6				)
 def   _UpperCamelCase    (			self					: Optional[Any]				)    ->			int:
      UpperCAmelCase						:				int		  =			["My hovercraft", "is full of eels"]
      UpperCAmelCase						:				Optional[int]		  =			["My hovercraft is full", " of eels"]
      # S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
      # whitespace at the front of " of eels" will be strip during preporcessing
      # so need to insert 2 whitespaces
      UpperCAmelCase						:				Optional[Any]		  =			cer.compute(predictions=_a       ,    references=_a       ,    concatenate_texts=_a				)
      self.assertTrue(abs(char_error_rate - 0.0_7_1_4_2_8				) < 1E-6				)
 def   _UpperCamelCase    (			self					: List[str]				)    ->			List[Any]:
      UpperCAmelCase						:				Dict		  =			["我能吞下玻璃而不伤身体"]
      UpperCAmelCase						:				List[Any]		  =			[" 能吞虾玻璃而 不霜身体啦"]
      # S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
      UpperCAmelCase						:				Union[str, Any]		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5				) < 1E-6				)

      UpperCAmelCase						:				List[str]		  =			["我能吞下玻璃", "而不伤身体"]
      UpperCAmelCase						:				str		  =			["我    能 吞 下 玻 璃", "而不伤身体"]
      # S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
      UpperCAmelCase						:				Optional[int]		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5_4_5				) < 1E-6				)

      UpperCAmelCase						:				Optional[Any]		  =			["我能吞下玻璃而不伤身体"]
      UpperCAmelCase						:				str		  =			cer.compute(predictions=_a       ,    references=_a				)
      self.assertFalse(_a       ,    0.0				)

 def   _UpperCamelCase    (			self					: Union[str, Any]				)    ->			Optional[Any]:
      UpperCAmelCase						:				Tuple		  =			[""]
      UpperCAmelCase						:				Tuple		  =			["Hypothesis"]
      with self.assertRaises(_a				):
           cer.compute(predictions=_a       ,    references=_a				)


if __name__ == "__main__":
      unittest.main()







from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

_snake_case   = TypeVar("T")
_snake_case   = TypeVar("U")




class    A_   ( Generic[T, U]     ):







 """simple docstring"""





 def __init__(			self					: List[Any]       ,    _a					: T | None       ,    _a					: U | None				)    ->			Dict:
      UpperCAmelCase						:				str		  =			key
      UpperCAmelCase						:				Dict		  =			val
      UpperCAmelCase						:				int		  =			0
      UpperCAmelCase						:				DoubleLinkedListNode[T, U] | None		  =			None
      UpperCAmelCase						:				DoubleLinkedListNode[T, U] | None		  =			None

 def __repr__(			self					: List[str]				)    ->			str:
      return "Node: key: {}, val: {}, freq: {}, has next: {}, has prev: {}".format(
          self.key       ,    self.val       ,    self.freq       ,    self.next is not None       ,    self.prev is not None				)




class    A_   ( Generic[T, U]     ):







 """simple docstring"""





 def __init__(			self					: Any				)    ->			None:
      UpperCAmelCase						:				DoubleLinkedListNode[T, U]		  =			DoubleLinkedListNode(_a       ,    _a				)
      UpperCAmelCase						:				DoubleLinkedListNode[T, U]		  =			DoubleLinkedListNode(_a       ,    _a				)
      UpperCAmelCase  , UpperCAmelCase						:				Optional[int]		  =			self.rear, self.head
 def __repr__(			self					: int				)    ->			str:
      UpperCAmelCase						:				Union[str, Any]		  =			["DoubleLinkedList"]
      UpperCAmelCase						:				int		  =			self.head
      while node.next is not None:
           rep.append(str(_a				)				)
           UpperCAmelCase						:				Union[str, Any]		  =			node.next
      rep.append(str(self.rear				)				)
      return ",\n    ".join(_a				)
 def   _UpperCamelCase    (			self					: List[Any]       ,    _a					: DoubleLinkedListNode[T, U]				)    ->			None:
      UpperCAmelCase						:				str		  =			self.rear.prev

      # All nodes other than self.head are guaranteed to have non-None previous
      assert previous is not None

      UpperCAmelCase						:				Any		  =			node
      UpperCAmelCase						:				Optional[int]		  =			previous
      UpperCAmelCase						:				Optional[int]		  =			node
      UpperCAmelCase						:				Union[str, Any]		  =			self.rear
      node.freq += 1
      self._position_node(_a				)
 def   _UpperCamelCase    (			self					: Any       ,    _a					: DoubleLinkedListNode[T, U]				)    ->			None:

      while node.prev is not None and node.prev.freq > node.freq:
           # swap node with previous node
           UpperCAmelCase						:				List[str]		  =			node.prev

           UpperCAmelCase						:				Any		  =			previous_node.prev
           UpperCAmelCase						:				int		  =			node.prev
           UpperCAmelCase						:				Tuple		  =			previous_node
           UpperCAmelCase						:				Optional[int]		  =			node

 def   _UpperCamelCase    (			self					: str       ,    _a					: DoubleLinkedListNode[T, U]				)    ->			DoubleLinkedListNode[T, U] | None:

      if node.prev is None or node.next is None:
           return None

      UpperCAmelCase						:				Dict		  =			node.next
      UpperCAmelCase						:				Optional[Any]		  =			node.prev
      UpperCAmelCase						:				List[Any]		  =			None
      UpperCAmelCase						:				int		  =			None
      return node




class    A_   ( Generic[T, U]     ):







 """simple docstring"""





 _UpperCamelCase				: dict[Callable[[T], U], LFUCache[T, U]]      =					{}
 def __init__(			self					: List[Any]       ,    _a					: int				)    ->			int:
      UpperCAmelCase						:				DoubleLinkedList[T, U]		  =			DoubleLinkedList()
      UpperCAmelCase						:				Any		  =			capacity
      UpperCAmelCase						:				Union[str, Any]		  =			0
      UpperCAmelCase						:				Any		  =			0
      UpperCAmelCase						:				Dict		  =			0
      UpperCAmelCase						:				dict[T, DoubleLinkedListNode[T, U]]		  =			{}
 def __repr__(			self					: Union[str, Any]				)    ->			str:
      return (
          f'CacheInfo(hits={self.hits}, misses={self.miss}, '
          f'capacity={self.capacity}, current_size={self.num_keys})'
      )
 def __contains__(			self					: Optional[int]       ,    _a					: T				)    ->			bool:
      return key in self.cache
 def   _UpperCamelCase    (			self					: Optional[int]       ,    _a					: T				)    ->			U | None:

      if key in self.cache:
           self.hits += 1
           UpperCAmelCase						:				DoubleLinkedListNode[T, U]		  =			self.cache[key]
           UpperCAmelCase						:				Optional[int]		  =			self.list.remove(self.cache[key]				)
           assert node == value_node

           # node is guaranteed not None because it is in self.cache
           assert node is not None
           self.list.add(_a				)
           return node.val
      self.miss += 1
      return None
 def   _UpperCamelCase    (			self					: str       ,    _a					: T       ,    _a					: U				)    ->			None:

      if key not in self.cache:
           if self.num_keys >= self.capacity:
                # delete first node when over capacity
                UpperCAmelCase						:				Any		  =			self.list.head.next

                # guaranteed to have a non-None first node when num_keys > 0
                # explain to type checker via assertions
                assert first_node is not None
                assert first_node.key is not None
                assert self.list.remove(_a				) is not None
                # first_node guaranteed to be in list

                del self.cache[first_node.key]
                self.num_keys -= 1
           UpperCAmelCase						:				Optional[Any]		  =			DoubleLinkedListNode(_a       ,    _a				)
           self.list.add(self.cache[key]				)
           self.num_keys += 1

      else:
           UpperCAmelCase						:				Any		  =			self.list.remove(self.cache[key]				)
           assert node is not None  # node guaranteed to be in list
           UpperCAmelCase						:				str		  =			value
           self.list.add(_a				)

 @classmethod
 def   _UpperCamelCase    (			cls					: type[LFUCache[T, U]]       ,    _a					: int = 1_2_8				)    ->			Callable[[Callable[[T], U]], Callable[..., U]]:

      def cache_decorator_inner(_a					: Callable[[T], U]				) -> Callable[..., U]:
           def cache_decorator_wrapper(*_a					: T				) -> U:
                if func not in cls.decorator_function_to_instance_map:
                     UpperCAmelCase						:				List[Any]		  =			LFUCache(_a				)

                UpperCAmelCase						:				List[Any]		  =			cls.decorator_function_to_instance_map[func].get(args[0]				)
                if result is None:
                     UpperCAmelCase						:				List[Any]		  =			func(*_a				)
                     cls.decorator_function_to_instance_map[func].put(args[0]       ,    _a				)
                return result

           def cache_info() -> LFUCache[T, U]:
                return cls.decorator_function_to_instance_map[func]

           setattr(_a       ,    "cache_info"       ,    _a				)  # noqa: B010

           return cache_decorator_wrapper

      return cache_decorator_inner


if __name__ == "__main__":
      import doctest

      doctest.testmod()



