o
    Git$                     @   s   d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 ed	Z
ed
dG dd dee
 ZdS )    )	dataclass)field)Dict)Generic)List)Optional)TypeVar)UnionSymbolF)reprc                   @   s  e Zd ZU dZeedZeee	f e
d< 	 eedZee	ef e
d< 	 dZee
d< 	 dZe	e
d< 	 d	d
 Zededd fddZededd fddZdefddZdefddZd0de	dee defddZdeee	f dee	ef fddZd1dd Zd2d!d"Zd#eee	f dee	ef fd$d%Zd#eee	f defd&d'Zdefd(d)Zdd defd*d+Ze de!e fd,d-Z"e de!e	 fd.d/Z#dS )3SymbolTablea:  SymbolTable that maps symbol IDs, found on the FSA arcs to
    actual objects. These objects can be arbitrary Python objects
    that can serve as keys in a dictionary (i.e. they need to be
    hashable and immutable).

    The SymbolTable can only be read to/written from disk if the
    symbols are strings.
    )default_factory_id2sym_sym2id   _next_available_id<eps>epsc                 C   s   | j  D ]\}}| j| |ksJ |dksJ q| j D ]\}}|dks(J | j | |ks1J qd| j vrD| j| j d< d| j| j< n| j d | jksNJ | j| j dksXJ t| j d | _d S )Nr   r   )r   itemsr   r   maxr   )selfidxsym r   C/home/ubuntu/.local/lib/python3.10/site-packages/k2/symbol_table.py__post_init__9   s   
zSymbolTable.__post_init__sreturnc                 C   s   t  }t  }| dD ]E}| }t|dkrqt|dks'J dt| |d t|d }}||vs=J d| ||vsHJ d| |||< |||< q|dd}t|||d	S )
a  Build a symbol table from a string.

        The string consists of lines. Every line has two fields separated
        by space(s), tab(s) or both. The first field is the symbol and the
        second the integer id of the symbol.

        Args:
          s:
            The input string with the format described above.
        Returns:
          An instance of :class:`SymbolTable`.
        
r      z$Expect a line with 2 fields. Given: r   zDuplicated symbol zDuplicated id r   r   r   r   )dictsplitlenintgetr   )r   id2symsym2idlinefieldsr   r   r   r   r   r   from_strK   s    
zSymbolTable.from_strfilenamec                 C   sD   t | ddd}t|  W  d   S 1 sw   Y  dS )a  Build a symbol table from file.

        Every line in the symbol table file has two fields separated by
        space(s), tab(s) or both. The following is an example file:

        .. code-block::

            <eps> 0
            a 1
            b 2
            c 3

        Args:
          filename:
            Name of the symbol table file. Its format is documented above.

        Returns:
          An instance of :class:`SymbolTable`.

        rzutf-8)encodingN)openr   r*   readstrip)r+   fr   r   r   	from_filel   s   $zSymbolTable.from_filec                 C   s4   d}t | j D ]\}}|| d| d7 }q	|S )z
        Returns:
          Return a string representation of this object. You can pass
          it to the method ``from_str`` to recreate an identical object.
          r   )sortedr   r   )r   r   r   symbolr   r   r   to_str   s   zSymbolTable.to_strc                 C   sV   t |d}t| j D ]\}}t|||d qW d   dS 1 s$w   Y  dS )a  Serialize the SymbolTable to a file.

        Every line in the symbol table file has two fields separated by
        space(s), tab(s) or both. The following is an example file:

        .. code-block::

            <eps> 0
            a 1
            b 2
            c 3

        Args:
          filename:
            Name of the symbol table file. Its format is documented above.
        w)fileN)r.   r5   r   r   print)r   r+   r1   r   r6   r   r   r   to_file   s
   "zSymbolTable.to_fileNr6   indexc                 C   sx   || j v r
| j | S |du r| j}|| jv r&td| d| d| j|  || j |< || j|< | j|kr:|d | _|S )ah  Add a new symbol to the SymbolTable.

        Args:
            symbol:
                The symbol to be added.
            index:
                Optional int id to which the symbol should be assigned.
                If it is not available, a ValueError will be raised.

        Returns:
            The int id to which the symbol has been assigned.
        NzCannot assign id 'z' to 'z' - already occupied by r   )r   r   r   
ValueError)r   r6   r<   r   r   r   add   s   






zSymbolTable.addkc                 C   s   t |tr
| j| S | j| S )aP  Get a symbol for an id or get an id for a symbol

        Args:
          k:
            If it is an id, it tries to find the symbol corresponding
            to the id; if it is a symbol, it tries to find the id
            corresponding to the symbol.

        Returns:
          An id or a symbol depending on the given `k`.
        
isinstancer$   r   r   )r   r?   r   r   r   r%      s   


zSymbolTable.getotherc                 C   s:   |  | i | j|j}i | j|j}t||| jdS )a  Create a union of two SymbolTables.
        Raises an AssertionError if the same IDs are occupied by
        different symbols.

        Args:
            other:
                A symbol table to merge with ``self``.

        Returns:
            A new symbol table.
        r    )_check_compatibler   r   r   r   )r   rB   r&   r'   r   r   r   merge   s   
zSymbolTable.mergec              	   C   s   | j |j ksJ d| j  d|j  t| j|j}|D ]}| | || ks9J d| d| |  d||  dqt| j|j}|D ]}| | || ksaJ d| d| |  d||  dqEd S )	NzMismatched epsilon symbol: z != zID conflict for id: z, self[idx] = "z", other[idx] = ""z, self[sym] = "z", other[sym] = ")r   setr   intersectionr   )r   rB   
common_idsr   common_symbolsr   r   r   r   rC      s(   zSymbolTable._check_compatibleitemc                 C   s
   |  |S N)r%   r   rJ   r   r   r   __getitem__      
zSymbolTable.__getitem__c                 C   s   t |tr
|| jv S || jv S rK   r@   rL   r   r   r   __contains__   s   


zSymbolTable.__contains__c                 C   s
   t | jS rK   )r#   r   )r   r   r   r   __len__  rN   zSymbolTable.__len__c                 C   s:   t | t |kr
dS | jD ]}| | || kr dS qdS )NFT)r#   symbols)r   rB   r   r   r   r   __eq__  s   
zSymbolTable.__eq__c                 C      t | j }|  |S )zDReturns a list of integer IDs corresponding to the symbols.
        )listr   keyssortr   ansr   r   r   ids  s   zSymbolTable.idsc                 C   rS   )z\Returns a list of symbols (e.g., strings) corresponding to
        the integer IDs.
        )rT   r   rU   rV   rW   r   r   r   rQ     s   zSymbolTable.symbolsrK   )rB   r   r   r   )rB   r   r   N)$__name__
__module____qualname____doc__r   r!   r   r   r$   r
   __annotations__r   r   r   r   staticmethodstrr*   r2   r7   r;   r   r>   r	   r%   rD   rC   rM   boolrO   rP   rR   propertyr   rY   rQ   r   r   r   r   r      s:   
  " 

"
r   N)dataclassesr   r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   <module>   s   