|
1 | 1 | import logging |
2 | 2 | import math |
3 | 3 | import os |
| 4 | +import shutil |
4 | 5 | import subprocess |
5 | 6 | import sys |
6 | 7 | import tempfile |
@@ -55,36 +56,28 @@ def get_language_model(kaldi_seq, proto_langdir='PROTO_LANGDIR'): |
55 | 56 | `kaldi_seq` is a list of words within kaldi's vocabulary. |
56 | 57 | """ |
57 | 58 |
|
58 | | - # Create a language model directory |
59 | | - lang_model_dir = tempfile.mkdtemp() |
60 | | - logging.info('saving language model to %s', lang_model_dir) |
61 | | - |
62 | | - # Symlink in necessary files from the prototype directory |
63 | | - for dirpath, dirnames, filenames in os.walk(proto_langdir, followlinks=True): |
64 | | - for dirname in dirnames: |
65 | | - relpath = os.path.relpath(os.path.join(dirpath, dirname), proto_langdir) |
66 | | - os.makedirs(os.path.join(lang_model_dir, relpath)) |
67 | | - for filename in filenames: |
68 | | - abspath = os.path.abspath(os.path.join(dirpath, filename)) |
69 | | - relpath = os.path.relpath(os.path.join(dirpath, filename), proto_langdir) |
70 | | - dstpath = os.path.join(lang_model_dir, relpath) |
71 | | - os.symlink(abspath, dstpath) |
72 | | - |
73 | 59 | # Generate a textual FST |
74 | 60 | txt_fst = make_bigram_lm_fst(kaldi_seq) |
75 | | - txt_fst_file = os.path.join(lang_model_dir, 'G.txt') |
76 | | - open(txt_fst_file, 'w').write(txt_fst) |
| 61 | + txt_fst_file = tempfile.NamedTemporaryFile(delete=False) |
| 62 | + txt_fst_file.write(txt_fst) |
| 63 | + txt_fst_file.close() |
77 | 64 |
|
78 | | - words_file = os.path.join(proto_langdir, "graphdir/words.txt") |
79 | | - subprocess.check_output([MKGRAPH_PATH, |
80 | | - os.path.join(lang_model_dir, 'langdir'), |
81 | | - os.path.join(lang_model_dir, 'modeldir'), |
82 | | - txt_fst_file, |
83 | | - words_file, |
84 | | - os.path.join(lang_model_dir, 'graphdir', 'HCLG.fst')]) |
85 | | - |
86 | | - # Return the language model directory |
87 | | - return lang_model_dir |
| 65 | + out_dir = tempfile.mkdtemp() |
| 66 | + |
| 67 | + try: |
| 68 | + subprocess.check_output([MKGRAPH_PATH, |
| 69 | + os.path.join(proto_langdir, 'langdir'), |
| 70 | + os.path.join(proto_langdir, 'modeldir'), |
| 71 | + txt_fst_file.name, |
| 72 | + os.path.join(proto_langdir, "graphdir/words.txt"), |
| 73 | + os.path.join(out_dir, 'HCLG.fst')]) |
| 74 | + except Exception, e: |
| 75 | + shutil.rmtree(out_dir) |
| 76 | + raise e |
| 77 | + finally: |
| 78 | + os.unlink(txt_fst_file.name) |
| 79 | + |
| 80 | + return out_dir |
88 | 81 |
|
89 | 82 | if __name__=='__main__': |
90 | 83 | import sys |
|
0 commit comments