我的應用程序的一部分使用trie到chunk單詞在一起。例如,["Summer", "in", "Los", "Angeles"]
變爲["Summer", "in", "Los Angeles"]
。一個trie的快速序列化
現在,這個特里從a large database填充,在本地存儲爲SQL,在應用程序啓動。這需要很長時間,大約15s。我想減少應用程序的啓動時間,所以我已經考慮過序列化Trie。不幸的是,pickling太慢 - 比從數據庫加載所有內容慢。
有沒有更快的方法來序列化我的trie?
這裏的特里類的樣子:
class Trie:
def __init__(self):
self.values = set()
self.children = dict()
def insert(self, key, value):
"""Insert a (key,value) pair into the trie.
The key should be a list of strings.
The value can be of arbitrary type."""
current_node = self
for key_part in key:
if key_part not in current_node.children:
current_node.children[key_part] = Trie()
current_node = current_node.children[key_part]
current_node.values.add(value)
def retrieve(self, key):
"""Returns either the value stored at the key, or raises KeyError."""
current_node = self
for key_part in key:
current_node = current_node.children[key_part]
return current_node.values
有沒有改變它的任何方式,將使其更序列化?
我曾經這樣做,以節省內存(http://stackoverflow.com/questions/2574357/how-to-transform-phrases-and-words-into-md5-hash),但與優化數據庫,如mongoDB和索引API像Lucene,我會避免建立一個新的結構索引和檢索的東西。 – alvas
MongoDB的+1,我實際上正在考慮離開關係數據庫。 – misha