~ubuntu-branches/ubuntu/saucy/sphinxtrain/saucy

« back to all changes in this revision

Viewing changes to python/cmusphinx/lat2dot.py

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import sys
 
4
 
 
5
def lattice_s3(latfile):
 
6
    mode="empty"
 
7
    for line in latfile:
 
8
 
 
9
        if line.startswith("Nodes"):
 
10
            mode = "node"
 
11
            continue
 
12
        if line.startswith("Edges"):
 
13
            mode = "edge"
 
14
            continue
 
15
        if line.startswith("#") or line.startswith("End"):
 
16
            mode = "none"
 
17
            continue
 
18
        
 
19
        items = line.strip().split(" ")
 
20
        if mode == "node":
 
21
            if items[1] != "":
 
22
                print items[0] + " [label = \"" + items[1] + " " + items[2] + " " + items[3] + " " + items[4] + "\"];"
 
23
            else:
 
24
                print "node " + items[0] + ";"
 
25
        if mode == "edge":
 
26
            print items[0] + " -> " + items[1] + " [label = \"" + items[2] + "\"];"
 
27
    print "}"
 
28
 
 
29
def create_map(items):
 
30
    dct = {}
 
31
    for i in items:
 
32
        dct[i[0]] = i[2:]
 
33
    return dct
 
34
 
 
35
def lattice_htk_wordnode(latfile):
 
36
    mode="empty"
 
37
    for line in latfile:
 
38
        items = line.strip().split()
 
39
        if items[0].startswith("I="):
 
40
            dct = create_map(items)
 
41
            if dct.has_key("W"):
 
42
                print dct["J"] + " [label = \"" + dct[W] + "\"];"
 
43
        if items[0].startswith("J="):
 
44
            dct = create_map(items)
 
45
            if dct.has_key("W"):
 
46
                print dct["S"] + " -> " + dct["E"] + " [label = \"" + dct["W"] + "," + dct["a"] + "," + dct["l"] + "\"];"
 
47
            else:
 
48
                print dct["S"] + " -> " + dct["E"] + " [label = \"" + dct["a"] + "," + dct["l"] + "\"];"
 
49
    print "}"
 
50
 
 
51
if __name__ == '__main__':
 
52
    latfilename = sys.argv[1]
 
53
    latfile = open(latfilename, "r")
 
54
 
 
55
    print """
 
56
    digraph lattice {
 
57
        rankdir=LR;
 
58
    """
 
59
 
 
60
    if latfilename.endswith("slf"):
 
61
        lattice_htk_wordnode(latfile)
 
62
    else:
 
63
        lattice_s3(latfile)