【发布时间】:2014-04-25 04:47:16
【问题描述】:
我的应用程序的一部分同时使用了trie 到chunk 字词。例如,["Summer", "in", "Los", "Angeles"] 变为 ["Summer", "in", "Los Angeles"]。
现在,这个 trie 在应用程序启动时从 a large database 填充,以 SQL 形式存储在本地。这需要很长时间,大约15s。我想减少应用程序的启动时间,所以我研究了序列化 Trie。不幸的是,pickling 太慢了 - 比从数据库中加载所有内容都慢。
有没有更快的方法来序列化我的 trie?
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
有什么方法可以改变它,使它更可序列化?
【问题讨论】:
-
我曾经做过这样的事情来节省内存 (stackoverflow.com/questions/2574357/…),但是使用像 mongoDB 这样的优化数据库和像 Lucene 这样的索引 API,我会避免构建一个新的结构来索引和检索东西。跨度>
-
+1 for MongoDB,我实际上正在考虑放弃关系数据库。
标签: python serialization nlp pickle trie