commit 7857286c54883210b0ce5ce3c319ef7ac85fc16b
parent c8eef9d4def727451f40f4f4e6c67e3b094fb97a
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Sun, 12 Jun 2011 19:29:03 -0400
add old file
Diffstat:
1 file changed, 32 insertions(+), 0 deletions(-)
diff --git a/rendertrie.py b/rendertrie.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+"""Read json trie in stdin, trim unneeded branches and output json dump
+to stdout"""
+
+import json
+import sys
+
+trie = json.load(sys.stdin)
+
+def trie2list(trie, mul, prefix):
+ v, c = trie
+ if c == {}:
+ return [mul, prefix]
+ else:
+ l = []
+ for child in c.keys():
+ l += trie2list(c[child], float(c[child][0]) / v, child+prefix)
+ return l
+
+def render(trie):
+ """Render the trie"""
+ if linear(trie[0]):
+ # no need for children, there is no more doubt
+ trie[1] = {}
+ for child in trie[1].values():
+ compress(child)
+
+compress(trie)
+
+print(json.dumps(trie))
+