summaryrefslogtreecommitdiff
path: root/util/cdec2json.py
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-12 14:39:37 +0200
committerPatrick Simianer <p@simianer.de>2014-06-12 14:39:37 +0200
commite834fd628f61aca04f98691eb56e9808c33c6787 (patch)
treea22c8f49fae4aff05e896b2b2fb5313d531cc629 /util/cdec2json.py
parent7d267062cd109ac27cff8a818ff368b949d7f6a0 (diff)
cleanup
Diffstat (limited to 'util/cdec2json.py')
-rwxr-xr-xutil/cdec2json.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/util/cdec2json.py b/util/cdec2json.py
new file mode 100755
index 0000000..76e2cd4
--- /dev/null
+++ b/util/cdec2json.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python2
+
+import cdec
+import sys, argparse
+
+
+def hg2json(hg, weights):
+ """
+ output a JSON representation of a cdec hypegraph
+ """
+ res = ''
+ res += "{\n"
+ res += '"weights":{'+"\n"
+ a = []
+ for i in weights:
+ if i[1] != 0:
+ a.append( '"%s":%s'%(i[0], i[1]) )
+ res += ", ".join(a)+"\n"
+ res += "},\n"
+ res += '"nodes":'+"\n"
+ res += "[\n"
+ a = []
+ a.append( '{ "id":-1, "cat":"root", "span":[-1,-1] }' )
+ for i in hg.nodes:
+ a.append('{ "id":%d, "cat":"%s", "span":[%d,%d] }'%(i.id, i.cat, i.span[0], i.span[1]))
+ res += ",\n".join(a)+"\n"
+ res += "],\n"
+ res += '"edges":'+"\n"
+ res += "[\n"
+ a = []
+ for i in hg.edges:
+ s = "{"
+ s += '"head":%d'%(i.head_node.id)
+ s += ', "rule":"%s"'%(i.trule)
+ # f
+ xs = ' "f":{'
+ b = []
+ for j in i.feature_values:
+ b.append( '"%s":%s'%(j[0], j[1]) )
+ xs += ", ".join(b)
+ xs += "},"
+ # tails
+ if len(list(i.tail_nodes)) > 0:
+ s += ', "tails":[ %s ],'%(",".join([str(n.id) for n in i.tail_nodes]))
+ else:
+ s += ', "tails":[ -1 ],'
+ s += xs
+ s += ' "weight":%s }'%(i.prob)
+ a.append(s)
+ res += ",\n".join(a)+"\n"
+ res += "]\n"
+ res += "}\n"
+ return res
+
+def main():
+ parser = argparse.ArgumentParser(description='get a proper json representation of cdec hypergraphs')
+ parser.add_argument('-c', '--config', required=True, help='decoder configuration')
+ parser.add_argument('-w', '--weights', required=True, help='feature weights')
+ args = parser.parse_args()
+ with open(args.config) as config:
+ config = config.read()
+ decoder = cdec.Decoder(config)
+ decoder.read_weights(args.weights)
+ ins = sys.stdin.readline().strip()
+ hg = decoder.translate(ins)
+
+ sys.stderr.write( "input:\n '%s'\n"%(ins) )
+ sys.stderr.write( "viterbi translation:\n '%s'\n"%(hg.viterbi()) )
+ num_nodes = 0
+ for i in hg.nodes: num_nodes+=1
+ sys.stderr.write( "# nodes = %s\n"%(num_nodes) )
+ num_edges = 0
+ for i in hg.edges: num_edges+=1
+ sys.stderr.write( "# edges = %s\n"%(num_edges) )
+ sys.stderr.write( "viterbi score = %s\n"%(round(hg.viterbi_features().dot(decoder.weights), 2)) )
+
+ print hg2json(hg, decoder.weights)
+
+
+if __name__=="__main__":
+ main()
+