1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#!/usr/bin/env python
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
import StringIO
from operator import truth
try:
from Bio import trie
except ImportError:
import os
from Bio import MissingExternalDependencyError
if os.name=="java":
message = "Not available on Jython, Bio.trie requires compiled C code."
else:
message = "Could not import Bio.trie, check C code was compiled."
raise MissingExternalDependencyError(message)
from Bio import triefind
trieobj = trie.trie()
trieobj["hello"] = 5
trieobj["he"] = 7
trieobj["hej"] = 9
trieobj["foo"] = "bar"
trieobj["wor"] = "ld"
print triefind.match("hello world!", trieobj) # "hello"
k = triefind.match_all("hello world!", trieobj)
k.sort()
print k # ["he", "hello"]
k = triefind.find("hello world!", trieobj)
k.sort()
print k # [("he", 0, 2), ("hello", 0, 5), ("wor", 6, 9)]
k = triefind.find_words("hello world!", trieobj)
k.sort()
print k # [("hello", 0, 5)]
trieobj["world"] = "full"
k = triefind.find("hello world!", trieobj)
k.sort()
print k # [("he", 0, 2), ("hello", 0, 5), ("wor", 6, 9), ("world", 6, 11)]
k = triefind.find_words("hello world!", trieobj)
k.sort()
print k # [("hello", 0, 5), ("world", 6, 11)]
|