
:>"^n                 @   s   d  Z  d d l Z d d l Z d d l m Z d d l Z e j d e  d d   Z d d   Z d	 d
   Z	 e j
 e j Z d d   Z d S)ad  Given a trie, find all occurrences of a word in the trie in a string.

Like searching a string for a substring, except that the substring is
any word in a trie.

Functions:
 - match         Find longest key in a trie matching the beginning of the string.
 - match_all     Find all keys in a trie matching the beginning of the string.
 - find          Find keys in a trie matching anywhere in a string.
 - find_words    Find keys in a trie matching whole words in a string.

This module is DEPRECATED. We encourage users to switch to alternative libraries
implementing a trie data structure, for example pygtrie.
    N)BiopythonDeprecationWarningzThis module has been deprecated. We encourage users to switch to alternative libraries implementing a trie data structure, for example pygtrie.c             C   s]   d } xP t  t |    D]< } |  d | d  } | j |  sC P| | k r | } q W| S)zzFind longest key, or return None.

    Find the longest key in the trie that matches the beginning of the
    string.
    N   )rangelen
has_prefix)stringtrieZlongestisubstr r   1/tmp/pip-build-ww9dw3qa/biopython/Bio/triefind.pymatch%   s    
r   c             C   sd   g  } xW t  t |    D]C } |  d | d  } | j |  sC P| | k r | j |  q W| S)zuFind and return a list of keys.

    Find all the keys in the trie that matches the beginning of the
    string.
    Nr   )r   r   r   append)r   r   matchesr	   r
   r   r   r   	match_all5   s    r   c             C   s}   g  } d } xj | t  |   k  rx t |  | d  |  } x. | D]& } | j | | | t  |  f  qA W| d 7} q W| S)zvFind all the keys in the trie that match anywhere in the string.

    Returns a list of tuples (key, start, end).
    r   Nr   )r   r   r   )r   r   resultsstartkeyskeyr   r   r   findE   s    $r   c       	      C   s   t  j d t  j t   } g  } d } x | t |   k  r t |  | d  |  } xa | D]Y } t |  } | | t |   k s | j |  | |  r] | j | | | | f  q] W| j |  |  } | d k r P| j	   } q+ W| S)a  Find all the keys in the trie that match full words in the string.

    Find all the keys in the trie that match full words in the string.
    Word boundaries are defined as any punctuation or whitespace.

    Returns a list of tuples (key, start, end).
    z[%s]+r   N)
recompileescapeDEFAULT_BOUNDARY_CHARSr   r   r   r   searchend)	r   r   Z_boundary_rer   r   r   r   lengthmr   r   r   
find_wordsX   s    r   )__doc__r   r   ZBior   warningswarnr   r   r   punctuation
whitespacer   r   r   r   r   r   <module>   s   	