commit 33ec5eafbcdbdecc0cfa833a2695fb51b96d50ee
parent 5d7d1b541867c51d93ae53f2257b6b7570574ff2
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Sun, 24 Feb 2013 19:00:03 +0100
+leavestrie.py
Diffstat:
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/README b/README
@@ -111,3 +111,5 @@ of the trie ("trie2dot.py h 0 1"). The result of such a drawing is given
as haspirater.pdf (before majoritytrie.py: contains frequency info, but
more nodes) and haspirater_majority.pdf (no frequency, less nodes).
+You can use leavestrie.py to get the leaves of a trie.
+
diff --git a/leavestrie.py b/leavestrie.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+"""Read json trie in stdin, produce leaves and values
+argv[1] is 1 or -1 to reverse the label sequence or not"""
+
+import json
+import sys
+
+trie = json.load(sys.stdin)
+
+def leaves(trie, prefix=""):
+ """Keep only the most probable values at each node"""
+ if len(trie[1].keys()) == 0:
+ assert(len(trie[0].keys()) == 1)
+ k, v = trie[0].popitem()
+ print("%s\t%s" % (k, prefix[::int(sys.argv[1])]))
+ for child in trie[1].keys():
+ leaves(trie[1][child], prefix + child)
+
+leaves(trie)
+