Package Bio :: Module triefind
[hide private]
[frames] | no frames]

Source Code for Module Bio.triefind

  1  # This code is part of the Biopython distribution and governed by its 
  2  # license.  Please see the LICENSE file that should have been included 
  3  # as part of this package. 
  4  # 
  5   
  6  """Given a trie, find all occurrences of a word in the trie in a string. 
  7   
  8  Like searching a string for a substring, except that the substring is 
  9  any word in a trie. 
 10   
 11  Functions: 
 12  match         Find longest key in a trie matching the beginning of the string. 
 13  match_all     Find all keys in a trie matching the beginning of the string. 
 14  find          Find keys in a trie matching anywhere in a string. 
 15  find_words    Find keys in a trie matching whole words in a string. 
 16  """ 
 17   
 18  import string 
 19  import re 
 20   
 21   
22 -def match(string, trie):
23 """match(string, trie) -> longest key or None 24 25 Find the longest key in the trie that matches the beginning of the 26 string. 27 28 """ 29 longest = None 30 for i in range(len(string)): 31 substr = string[:i + 1] 32 if not trie.has_prefix(substr): 33 break 34 if substr in trie: 35 longest = substr 36 return longest
37 38
39 -def match_all(string, trie):
40 """match_all(string, trie) -> list of keys 41 42 Find all the keys in the trie that matches the beginning of the 43 string. 44 45 """ 46 matches = [] 47 for i in range(len(string)): 48 substr = string[:i + 1] 49 if not trie.has_prefix(substr): 50 break 51 if substr in trie: 52 matches.append(substr) 53 return matches
54 55
56 -def find(string, trie):
57 """find(string, trie) -> list of tuples (key, start, end) 58 59 Find all the keys in the trie that match anywhere in the string. 60 61 """ 62 results = [] 63 start = 0 # index to start the search 64 while start < len(string): 65 # Look for a match. 66 keys = match_all(string[start:], trie) 67 for key in keys: 68 results.append((key, start, start + len(key))) 69 start += 1 70 return results
71 72 73 DEFAULT_BOUNDARY_CHARS = string.punctuation + string.whitespace 74 75
76 -def find_words(string, trie):
77 """find_words(string, trie) -> list of tuples (key, start, end) 78 79 Find all the keys in the trie that match full words in the string. 80 Word boundaries are defined as any punctuation or whitespace. 81 82 """ 83 _boundary_re = re.compile(r"[%s]+" % re.escape(DEFAULT_BOUNDARY_CHARS)) 84 85 results = [] 86 start = 0 # index of word boundary 87 while start < len(string): 88 # Look for a match. 89 keys = match_all(string[start:], trie) 90 for key in keys: 91 l = len(key) 92 # Make sure it ends at a boundary. 93 if start + l == len(string) or \ 94 _boundary_re.match(string[start + l]): 95 results.append((key, start, start + l)) 96 # Move forward to the next boundary. 97 m = _boundary_re.search(string, start) 98 if m is None: 99 break 100 start = m.end() 101 return results
102