From 3a701f28dca7fb43965e1dac0ddc8287c7ae816a Mon Sep 17 00:00:00 2001 From: David Huggins-Daines Date: Mon, 9 Nov 2020 22:11:55 -0500 Subject: [PATCH] reformat and fix an error --- python/cmusphinx/lat2dot.py | 76 ++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/python/cmusphinx/lat2dot.py b/python/cmusphinx/lat2dot.py index e793b6bb..10f92c51 100755 --- a/python/cmusphinx/lat2dot.py +++ b/python/cmusphinx/lat2dot.py @@ -2,62 +2,70 @@ import sys + def lattice_s3(latfile): - mode="empty" + mode = "empty" for line in latfile: - if line.startswith("Nodes"): - mode = "node" - continue - if line.startswith("Edges"): - mode = "edge" - continue - if line.startswith("#") or line.startswith("End"): - mode = "none" - continue - + if line.startswith("Nodes"): + mode = "node" + continue + if line.startswith("Edges"): + mode = "edge" + continue + if line.startswith("#") or line.startswith("End"): + mode = "none" + continue + items = line.strip().split(" ") - if mode == "node": - if items[1] != "": - print items[0] + " [label = \"" + items[1] + " " + items[2] + " " + items[3] + " " + items[4] + "\"];" - else: - print "node " + items[0] + ";" + if mode == "node": + if items[1] != "": + print(items[0] + " [label = \"" + items[1] + " " + items[2] + + " " + items[3] + " " + items[4] + "\"];") + else: + print("node " + items[0] + ";") if mode == "edge": - print items[0] + " -> " + items[1] + " [label = \"" + items[2] + "\"];" - print "}" + print(items[0] + " -> " + items[1] + " [label = \"" + items[2] + + "\"];") + print("}") + def create_map(items): dct = {} for i in items: - dct[i[0]] = i[2:] + dct[i[0]] = i[2:] return dct + def lattice_htk_wordnode(latfile): - mode="empty" + mode = "empty" for line in latfile: items = line.strip().split() - if items[0].startswith("I="): - dct = create_map(items) - if dct.has_key("W"): - print dct["J"] + " [label = \"" + dct[W] + "\"];" + if items[0].startswith("I="): + dct = create_map(items) + if "W" in dct: + print(dct["J"] + " [label = \"" + dct["W"] + "\"];") if items[0].startswith("J="): - dct = create_map(items) - if dct.has_key("W"): - print dct["S"] + " -> " + dct["E"] + " [label = \"" + dct["W"] + "," + dct["a"] + "," + dct["l"] + "\"];" - else: - print dct["S"] + " -> " + dct["E"] + " [label = \"" + dct["a"] + "," + dct["l"] + "\"];" - print "}" + dct = create_map(items) + if "W" in dct: + print(dct["S"] + " -> " + dct["E"] + " [label = \"" + + dct["W"] + "," + dct["a"] + "," + dct["l"] + "\"];") + else: + print(dct["S"] + " -> " + dct["E"] + " [label = \"" + + dct["a"] + "," + dct["l"] + "\"];") + print("}") + if __name__ == '__main__': latfilename = sys.argv[1] latfile = open(latfilename, "r") - print """ + print(""" digraph lattice { rankdir=LR; - """ + """) if latfilename.endswith("slf"): - lattice_htk_wordnode(latfile) + lattice_htk_wordnode(latfile) else: - lattice_s3(latfile) + lattice_s3(latfile)