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  """ 
  7  Given a trie, find all occurrences of a word in the trie in a string. 
  8   
  9  Like searching a string for a substring, except that the substring is 
 10  any word in a trie. 
 11   
 12  Functions: 
 13  match         Find longest key in a trie matching the beginning of the string. 
 14  match_all     Find all keys in a trie matching the beginning of the string. 
 15  find          Find keys in a trie matching anywhere in a string. 
 16  find_words    Find keys in a trie matching whole words in a string. 
 17   
 18  """ 
 19  import string 
 20  import re 
 21   
 22   
23 -def match(string, trie):
24 """match(string, trie) -> longest key or None 25 26 Find the longest key in the trie that matches the beginning of the 27 string. 28 29 """ 30 longest = None 31 for i in range(len(string)): 32 substr = string[:i+1] 33 if not trie.has_prefix(substr): 34 break 35 if substr in trie: 36 longest = substr 37 return longest
38 39
40 -def match_all(string, trie):
41 """match_all(string, trie) -> list of keys 42 43 Find all the keys in the trie that matches the beginning of the 44 string. 45 46 """ 47 matches = [] 48 for i in range(len(string)): 49 substr = string[:i+1] 50 if not trie.has_prefix(substr): 51 break 52 if substr in trie: 53 matches.append(substr) 54 return matches
55 56
57 -def find(string, trie):
58 """find(string, trie) -> list of tuples (key, start, end) 59 60 Find all the keys in the trie that match anywhere in the string. 61 62 """ 63 results = [] 64 start = 0 # index to start the search 65 while start < len(string): 66 # Look for a match. 67 keys = match_all(string[start:], trie) 68 for key in keys: 69 results.append((key, start, start+len(key))) 70 start += 1 71 return results
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