File: lat2dot.py

package info (click to toggle)
sphinxtrain 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,572 kB
  • sloc: ansic: 94,052; perl: 8,939; python: 6,702; cpp: 2,044; makefile: 6
file content (71 lines) | stat: -rwxr-xr-x 1,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3

import sys


def lattice_s3(latfile):
    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

        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 == "edge":
            print(items[0] + " -> " + items[1] + " [label = \"" + items[2] +
                  "\"];")
    print("}")


def create_map(items):
    dct = {}
    for i in items:
        dct[i[0]] = i[2:]
    return dct


def lattice_htk_wordnode(latfile):
    mode = "empty"
    for line in latfile:
        items = line.strip().split()
        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 "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("""
    digraph lattice {
	rankdir=LR;
    """)

    if latfilename.endswith("slf"):
        lattice_htk_wordnode(latfile)
    else:
        lattice_s3(latfile)