一个c实现的后缀树包
csuffixtree的Python项目详细描述
简介
这是一个后缀树数据结构LIB,用Python包在C++中实现。
除了构造树之外,这个库还提供了构造查询树以加快查询字符串的速度。
安装并构建
从pypi安装
运行pip install csuffixtree
建造
您可以在Linux或Windows上构建它。
安装python包
- 在windows中构建suffixtreepybinding解决方案
- 在Linux上,您应该转到
linux
目录,然后运行make python
- 在suffixtreepy目录下。运行
python setup.py install
如何使用
你可以用下面的方法构造一个后缀树。
from suffixtree import *
tree = SuffixTree(True,["abc","123","321"])
tree.addStrings(["xyz","abcd"])
print(tree.findString("1"))
print(tree.findString("a"))
如果您完成了向后缀树插入字符串,并且希望查询速度非常快 您可以这样做:
# convert tree to query tree, this release a part of memory
qtree = tree.createQueryTree()
qtree.cacheNodes() # take some time to cache intermediate nodes
qtree.findString("a")
为了简单起见,您还可以编写
# do not preserve string
qtree = SuffixQueryTree(False,["abc","abcd","123","321"])
idx = qtree.findStringIdx("abc") # you cannot use findString now
print(idx)
序列化
suffixquerytree可以序列化并反序列化到文件a bytes对象。 例如:
# do not preserve string
qtree = SuffixQueryTree(False,["abc","abcd","123","321"])
idx = qtree.findStringIdx("abc") # you cannot use findString now
print(idx)