ó
F»]@c           @   sV   d  Z  d d l Z d d l Z d „  Z d „  Z d „  Z e j e j Z d „  Z	 d S(   sÏ  
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.

iÿÿÿÿNc         C   s`   d } xS t t |  ƒ ƒ D]? } |  | d  } | j | ƒ s@ Pn  | j | ƒ r | } q q W| S(   s„   match(string, trie) -> longest key or None

    Find the longest key in the trie that matches the beginning of the
    string.

    i   N(   t   Nonet   ranget   lent
   has_prefixt   has_key(   t   stringt   triet   longestt   it   substr(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/triefind.pyt   match   s    c         C   sg   g  } xZ t  t |  ƒ ƒ D]F } |  | d  } | j | ƒ s@ Pn  | j | ƒ r | j | ƒ q q W| S(   s~   match_all(string, trie) -> list of keys

    Find all the keys in the trie that matches the beginning of the
    string.

    i   (   R   R   R   R   t   append(   R   R   t   matchesR   R	   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/triefind.pyt	   match_all!   s    c         C   sw   g  } d } xd | t  |  ƒ k  rr t |  | | ƒ } x. | D]& } | j | | | t  | ƒ f ƒ q; W| d 7} q W| S(   s‚   find(string, trie) -> list of tuples (key, start, end)

    Find all the keys in the trie that match anywhere in the string.

    i    i   (   R   R   R   (   R   R   t   resultst   startt   keyst   key(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/triefind.pyt   find1   s    $c   	      C   sí   t  j d t  j t ƒ ƒ } g  } d } x¾ | t |  ƒ k  rè t |  | | ƒ } xd | D]\ } t | ƒ } | | t |  ƒ k s– | j |  | | ƒ rW | j | | | | f ƒ qW qW W| j |  | ƒ } | d k rÙ Pn  | j
 ƒ  } q+ W| S(   sÌ   find_words(string, trie) -> list of tuples (key, start, end)

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

    s   [%s]+i    N(   t   ret   compilet   escapet   DEFAULT_BOUNDARY_CHARSR   R   R
   R   t   searchR    t   end(	   R   R   t   _boundary_reR   R   R   R   t   lt   m(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/triefind.pyt
   find_wordsC   s    !(
   t   __doc__R   R   R
   R   R   t   punctuationt
   whitespaceR   R   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/triefind.pyt   <module>   s   			