forked from gooofy/kaldi-adapt-lm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
adapt.py
77 lines (60 loc) · 2.26 KB
/
adapt.py
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
72
73
74
75
76
77
#
# Copyright 2016, 2017, 2018 Guenter Bartsch, 2021 Florian Quirin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# adapt an existing kaldi model to a new language model
#
import os
import sys
import logging
from optparse import OptionParser
from kaldi_adapt_lm import kaldi_adapt_lm
DEFAULT_KALDI_ROOT = '/opt/kaldi'
DEFAULT_WORK_DIR = 'work'
# commandline parsing
parser = OptionParser("usage: %prog [options] src_model_dir lm.arpa dst_model_name")
parser.add_option ("-f", "--force", action="store_true", dest="force",
help="overwrite work dir if it exists")
parser.add_option ("-k", "--kaldi-root", dest="kaldi_root", type = "str", default=DEFAULT_KALDI_ROOT,
help="kaldi root dir (default: %s)" % DEFAULT_KALDI_ROOT)
parser.add_option ("-v", "--verbose", action="store_true", dest="verbose",
help="enable verbose logging")
parser.add_option ("-w", "--work-dir", dest="work_dir", type = "str", default=DEFAULT_WORK_DIR,
help="work dir (default: %s)" % DEFAULT_WORK_DIR)
(options, args) = parser.parse_args()
if options.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
if len(args) != 3:
parser.print_usage()
sys.exit(1)
src_model_dir = args[0]
lm_fn = args[1]
dst_model_name = args[2]
work_dir = options.work_dir
if os.path.exists(work_dir):
if options.force:
# cleanup leftovers from previous runs
cmd = 'rm -rf %s' % work_dir
logging.info(cmd)
os.system(cmd)
else:
logging.error("work dir %s already exists." % work_dir)
sys.exit(1)
kaldi_root = options.kaldi_root
# main
kaldi_adapt_lm (kaldi_root, src_model_dir, lm_fn, work_dir, dst_model_name)
logging.info ("All done.")