4 from __future__
import (absolute_import, division,
5 print_function, unicode_literals)
12 """A class to generate fst binaries from raw text files or strings"""
14 if not os.path.exists(fpath):
15 self.
error(
"File path '{}' does not exist".format(fpath))
19 if not os.path.isdir(outpath):
20 self.
warning(
"'{}' does not exist".format(outpath))
25 fname_split = os.path.basename(fpath).split(
'.')
27 fname = fname_split[0]
28 fstype = fname_split[1]
31 if not fext ==
".txt":
32 self.
error(
"Incorrect extension '{}' of '{}'. Need '.txt' file".format(fext, fpath))
34 if not (fstype ==
"fsa" or fstype ==
"fst"):
35 self.
error(
"Unknown fst type '{}' in '{}'. Should either be 'fst' or 'fsa'".format(fstype, fpath))
38 self.
error(
"Error! Incorrect input file '{}'. Check usage to understand the correct file input".format(fpath))
48 """Method to generate the symbols files isyms and osyms"""
63 with open(self.
fpath,
'r')
as fsfiletxt:
64 lines = fsfiletxt.readlines()
65 lines = [line.strip().split(
' ')
for line
in lines]
76 fh.write(
"{} {}".format(field_count + 1, field))
80 """Method to compile FSA/FST after generating symbols files using
84 def error(self, *args, **kwargs):
85 """Error function of FstCompiler class"""
86 print(
"[Kaldi-Grammar_Parser]", *args, file=sys.stderr, **kwargs)
90 """Warning method of FstCompiler class"""
91 print(
"[Kaldi-Grammar_Parser]", *args, file=sys.stdout, **kwargs)
95 if __name__ ==
"__main__":
98 if len(sys.argv) < 3
or "-h" in sys.argv
or "--help" in sys.argv:
100 Usage: python makeSymbols file fieldNumber
102 file: the textual FST/FSA file (.fst.txt or .fsa.txt usually), to extract the symbols from
103 fieldNumber: which column of the file to take symbols from
104 input symbols use fieldNumber of 2
105 output symbols use fieldNumber of 3
107 The Symbols Table is output to standard out, and can be piped into a file
112 fieldNumber = int(sys.argv[2])
114 sym_gen = FsSymbolGenerator(fname, fieldNumber)