
    h%                    x    % S r SSKJr  SSKJr  SSKrSSKJr  SSK	J
r
  \" S5      rS\S	'    " S
 S\
\   5      rg)z:Implementation of a space that represents textual strings.    )annotations)AnyN)NDArray)Space>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789frozenset[str]alphanumericc                     ^  \ rS rSrSrS\SS.       SU 4S jjjr  S     SS jjr        SS jrSS	 jr	SS
 jr
SS jr\SS j5       r\SS j5       rSS jr\SS j5       r\SS j5       rSrU =r$ )Text   a  A space representing a string comprised of characters from a given charset.

Example:
    >>> from gymnasium.spaces import Text
    >>> # {"", "B5", "hello", ...}
    >>> Text(5)
    Text(1, 5, charset=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)
    >>> # {"0", "42", "0123456789", ...}
    >>> import string
    >>> Text(min_length = 1,
    ...      max_length = 10,
    ...      charset = string.digits)
    Text(1, 10, charset=0123456789)
   N)
min_lengthcharsetseedc                 > [         R                  " [        U5      [         R                  5      (       d   S[        U5       35       e[         R                  " [        U5      [         R                  5      (       d   S[        U5       35       eSU::  d
   SU 35       eX!::  d   SU SU 35       e[	        U5      U l        [	        U5      U l        [        U5      U l        [        U5      U l
        [        [        U5      5       VVs0 s H  u  pVU[         R                  " U5      _M     snnU l        SR                  [        [        U5      5      5      U l        ["        TU ]I  [&        US9  g	s  snnf )
a  Constructor of :class:`Text` space.

Both bounds for text length are inclusive.

Args:
    min_length (int): Minimum text length (in characters). Defaults to 1 to prevent empty strings.
    max_length (int): Maximum text length (in characters).
    charset (Union[set], str): Character set, defaults to the lower and upper english alphabet plus latin digits.
    seed: The seed for sampling from the space.
z6Expects the min_length to be an integer, actual type: z6Expects the max_length to be an integer, actual type: r   z8Minimum text length must be non-negative, actual value: zIThe min_length must be less than or equal to the max_length, min_length: z, max_length:  )dtyper   N)np
issubdtypetypeintegerintr   
max_length	frozenset	_char_settuple
_char_list	enumerateint32_char_indexjoinsorted	_char_strsuper__init__str)selfr   r   r   r   ival	__class__s          O/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/spaces/text.pyr%   Text.__init__"   sm   $ }}bjj
 
 	WCDDTCUV	W 
 }}bjj
 
 	WCDDTCUV	W 
 O	SEj\R	S $	~VWaVbbpq{p|}	~$  #:":)27);+0>+4U7^+D1
+DC!+D1
 !ggfU7^&<= 	s.1
s   #E.c                   Ub  Ub  [        SU SU 35      eUb  U R                  U[        R                  S5      u  p4Ubs  [        R                  " [        R
                  " US:H  US:H  5      5      (       d
   SU 35       e[        R                  " U5      S:  a  U[        R                  " U5      -  nOUb  U R                  U[        R                  S5      u  p4Ub  [        R                  " [        R                  " US:  US:*  5      5      (       d
   S	U 35       e[        R                  " [        R                  " U5      S5      (       d   S
[        R                  " U5       35       eOS=p4Uc3  U R                  R                  U R                  U R                  S-   5      nUc?  [        R                  " [        U R                   5      5      [        U R                   5      -  n[        R                  " US:H  5      (       a*  U R                  S:X  a  g[        SU R                   S35      eU R                  R#                  U R$                  X4S9nSR'                  U5      $ )a^  Generates a single random sample from this space with by default a random length between ``min_length`` and ``max_length`` and sampled from the ``charset``.

Args:
    mask: An optional tuples of length and mask for the text.
        The length is expected to be between the ``min_length`` and ``max_length``.
        Otherwise, a random integer between ``min_length`` and ``max_length`` is selected.
        For the mask, we expect a numpy array of length of the charset passed with ``dtype == np.int8``.
        If the charlist mask is all zero then an empty string is returned no matter the ``min_length``
    probability: An optional tuples of length and probability mask for the text.
        The length is expected to be between the ``min_length`` and ``max_length``.
        Otherwise, a random integer between ``min_length`` and ``max_length`` is selected.
        For the probability mask, we expect a numpy array of length of the charset passed with ``dtype == np.float64``.
        The sum of the probability mask should be 1, otherwise an exception is raised.

Returns:
    A sampled string from the space
NzIOnly one of `mask` or `probability` can be provided, actual values: mask=z, probability=maskr   r   z2Expects all mask values to 0 or 1, actual values: probabilityzIExpects all probability mask values to be within 0 and 1, actual values: z=Expects the sum of the probability mask to be 1, actual sum: r   zBTrying to sample with a minimum length > 0 (actual minimum length=zP) but the character mask is all zero meaning that no character could be sampled.)sizep)
ValueError_validate_maskr   int8all
logical_orsumfloat64logical_andisclose	np_randomintegersr   r   oneslencharacter_setchoicecharacter_listr!   )r'   r.   r/   lengthcharlist_maskstrings         r+   sampleText.sampleN   sh   ,  7[\`[aaop{o|}  $($7$7bggv$N!F(vvMM-1"4mq6HI  XGWX 
 66-(1,$1BFF=4I$IM$$($7$7RZZ%!F (vvNN=A#5}7IJ  o^_l^mno  zzFF=)1  kRSUSYSYZgShRijk  &*)F>^^,,T__dooPQ>QRF GGC(:(:$;<s4CUCU?VVM66-1$%%!# !XY]YhYhXi  jz  {  &&f ' 
 wwv    c                8   [        U[        5      (       d   SU S[        U5       35       e[        U5      S:X  d   SU S[        U5       35       eUu  pEUb  [        R
                  " [        U5      [        R                  5      (       d   S[        U5       35       eU R                  Us=::  a  U R                  ::  d&  O   SU R                   SU R                   SU 35       eUb  [        U[        R                  5      (       d   S	U S
[        U5       35       eUR                  U:X  d   S	U SU SUR                   35       eUR                  [        U R                  5      4:X  d.   SU S[        U R                  5      4 SUR                   35       eXE4$ )NzExpects the `z#` type to be a tuple, actual type:    z#` length to be two, actual length: z>Expects the Text sample length to be an integer, actual type: z*Expects the Text sample length be between z and z, actual length: zExpects the Text sample `z$` to be an np.ndarray, actual type: z` to be type z, actual dtype: zexpects the Text sample `z` to be z, actual shape: )
isinstancer   r   r>   r   r   r   r   r   ndarrayr   shaper?   )r'   r.   expected_dtype	mask_typerB   rC   s         r+   r3   Text._validate_mask   s    %
 
 	V9+%HdU	V 
 IN	U9+%HTT	U $==Vbjj  _OPTU[P\~^_  6<T__<};DOO;LERVRaRaQbbstzs{|}<$rzz  p*9+5YZ^_lZmYnop  ##~5y*9+]>BRRbcpcvcvbwxy5 &&D&&'+  @*9+Xs4CUCU?V>X=YYijwj}j}i~@  $$rG   c                   ^  [        U[        5      (       aB  T R                  [        U5      s=::  a  T R                  ::  a  O  g[        U 4S jU 5       5      $ g)z?Return boolean specifying if x is a valid member of this space.c              3  @   >#    U  H  oTR                   ;   v   M     g 7f)N)r?   ).0cr'   s     r+   	<genexpr> Text.contains.<locals>.<genexpr>   s     >Aq 2 22As   F)rJ   r&   r   r>   r   r5   )r'   xs   ` r+   containsText.contains   sB    a#a&;DOO; >A>>>rG   c                V    SU R                    SU R                   SU R                   S3$ )z,Gives a string representation of this space.zText(z, z
, charset=))r   r   
charactersr'   s    r+   __repr__Text.__repr__   s-    t'r$//):*T__DUUVWWrG   c                    [        U[        5      =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ )z7Check whether ``other`` is equivalent to this instance.)rJ   r   r   r   r?   )r'   others     r+   __eq__Text.__eq__   s\     ud# :5#3#33:5#3#33: ""e&9&99		
rG   c                    U R                   $ )z(Returns the character set for the space.)r   r\   s    r+   r?   Text.character_set        ~~rG   c                    U R                   $ )z+Returns a tuple of characters in the space.)r   r\   s    r+   rA   Text.character_list   s     rG   c                     U R                   U   $ )zGReturns a unique index for each character in the space's character set.)r    )r'   chars     r+   character_indexText.character_index   s    %%rG   c                    U R                   $ )z*Returns a string with all Text characters.)r#   r\   s    r+   r[   Text.characters   re   rG   c                    g)zaThe flattened version is an integer array for each character, padded to the max character length.T r\   s    r+   is_np_flattenableText.is_np_flattenable   s     rG   )r    r   r   r#   r   r   )r   r   r   r   r   zfrozenset[str] | strr   z int | np.random.Generator | None)NN)r.   z1None | tuple[int | None, NDArray[np.int8] | None]r/   z4None | tuple[int | None, NDArray[np.float64] | None]returnr&   )r.   @tuple[int | None, NDArray[np.int8] | NDArray[np.float64] | None]rM   znp.dtyperN   r&   rr   rs   )rV   r   rr   bool)rr   r&   )r`   r   rr   rt   )rr   r   )rr   ztuple[str, ...])ri   r&   rr   znp.int32)rr   rt   )__name__
__module____qualname____firstlineno____doc__r	   r%   rE   r3   rW   r]   ra   propertyr?   rA   rj   r[   rp   __static_attributes____classcell__)r*   s   @r+   r   r      s   & (415*/*/ 	*/
 &*/ /*/ */\ EINREAE LE 
	EN %N % ! % 	 %
 
J %DX
    &    rG   r   )ry   
__future__r   typingr   numpyr   numpy.typingr   gymnasium.spaces.spacer   r   r	   __annotations__r&   r   ro   rG   r+   <module>r      s?    @ "     (  )D n 
O5: OrG   