a
     g                     @   sR   d Z ddlmZmZmZmZ G dd dee Zeeee ef edddZdS )	zf
Trie data structure.

Designed to be used for tokenizer's dictionary, but can be for other purposes.
    )IterableIteratorListUnionc                   @   s   e Zd ZG dd dZee dddZedddd	Zeddd
dZee	e dddZ
eedddZee dddZedddZdS )Triec                   @   s   e Zd ZdZdd ZdS )z	Trie.Nodeendchildrenc                 C   s   d| _ i | _d S )NFr   self r   =/usr/local/lib/python3.9/dist-packages/pythainlp/util/trie.py__init__   s    zTrie.Node.__init__N)__name__
__module____qualname__	__slots__r   r   r   r   r   Node   s   r   wordsc                 C   s,   t || _t | _|D ]}| | qd S N)setr   r   r   rootadd)r   r   wordr   r   r   r      s    

zTrie.__init__N)r   returnc                 C   sT   |  }| j| | j}|D ]*}|j|}|sDt }||j|< |}qd|_dS )z
        Add a word to the trie.
        Spaces in front of and following the word will be removed.

        :param str text: a word
        TN)	stripr   r   r   r	   getr   r   r   )r   r   curchchildr   r   r   r      s    
zTrie.addc                 C   s   || j vrdS | j | | j}g }|D ]"}|j| }||||f |}q(d|_t|D ]"\}}}|jsp|jrt q~|j|= qZdS )z}
        Remove a word from the trie.
        If the word is not found, do nothing.

        :param str text: a word
        NF)r   remover   r	   appendr   reversed)r   r   parentdatar   r    r   r   r   r!   -   s    

zTrie.remove)textr   c                 C   sT   g }| j }t|D ]<\}}|j|}|s. qP|jrJ||d|d   |}q|S )z
        List all possible words from first sequence of characters in a word.

        :param str text: a word
        :return: a list of possible words
        :rtype: List[str]
        N   )r   	enumerater	   r   r   r"   )r   r&   resr   ir   noder   r   r   prefixesG   s    zTrie.prefixes)keyr   c                 C   s
   || j v S r   r   )r   r-   r   r   r   __contains__Z   s    zTrie.__contains__)r   c                 c   s   | j E d H  d S r   r   r
   r   r   r   __iter__]   s    zTrie.__iter__c                 C   s
   t | jS r   )lenr   r
   r   r   r   __len__`   s    zTrie.__len__)r   r   r   r   r   strr   r   r!   r   r,   boolr.   r   r/   intr1   r   r   r   r   r      s   r   )dict_sourcer   c                 C   s   t g }t| trbt| dkrbt| ddd$}|  }t |}W d   q1 sV0    Y  n&t| trt| tst | }ntd|S )a  
    Create a dictionary trie from a file or an iterable.

    :param str|Iterable[str]|pythainlp.util.Trie dict_source: a path to
        dictionary file or a list of words or a pythainlp.util.Trie object
    :return: a trie object
    :rtype: pythainlp.util.Trie
    r   rutf8)encodingNziType of dict_source must be pythainlp.util.Trie, or Iterable[str], or non-empty str (path to source file))	r   
isinstancer2   r0   openread
splitlinesr   	TypeError)r5   ZtriefZ_vocabsr   r   r   	dict_tried   s    	(
r?   N)	__doc__typingr   r   r   r   r2   r   r?   r   r   r   r   <module>   s   X