speech_recognition
fstcompiler.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 # Python 2/3 compatibility
4 from __future__ import (absolute_import, division,
5  print_function, unicode_literals)
6 from builtins import *
7 
8 import os
9 import sys
10 
12  """A class to generate fst binaries from raw text files or strings"""
13  def __init__(self, fpath, outpath):
14  if not os.path.exists(fpath):
15  self.error("File path '{}' does not exist".format(fpath))
16 
17  self.fpath = fpath
18 
19  if not os.path.isdir(outpath):
20  self.warning("'{}' does not exist".format(outpath))
21  os.makedirs(outpath)
22 
23  self.outpath = outpath
24 
25  fname_split = os.path.basename(fpath).split('.')
26  try:
27  fname = fname_split[0]
28  fstype = fname_split[1]
29  fext = fname_split[2]
30 
31  if not fext == ".txt":
32  self.error("Incorrect extension '{}' of '{}'. Need '.txt' file".format(fext, fpath))
33 
34  if not (fstype == "fsa" or fstype == "fst"):
35  self.error("Unknown fst type '{}' in '{}'. Should either be 'fst' or 'fsa'".format(fstype, fpath))
36 
37  except:
38  self.error("Error! Incorrect input file '{}'. Check usage to understand the correct file input".format(fpath))
39 
40  self.fname = fname
41  self.fstype = fstype
42 
43  self.fsFile = os.path.join(self.outpath, self.fname + "." + self.fstype)
44  self.isymsFile = os.path.join(self.outpath, self.fname + ".isyms")
45  self.osymsFile = os.path.join(self.outpath, self.fname + ".osyms")
46 
47  def generateSymsFiles(self):
48  """Method to generate the symbols files isyms and osyms"""
49 
50  self.isymsFileHandle = open(self.isymsFile, 'w')
51  self.isymsFileHandle.write("- 0")
52 
53  # FSAs have only one field, hence 2 by default is added
54  self.fieldFileDict = {2: self.isymsFileHandle}
55 
56  # FSTs have two fields
57  if self.fstype == "fst":
58  self.osymsFileHandle = open(self.osymsFile, 'w')
59  self.osymsFileHandle.write("- 0")
60  self.fieldFileDict[3] = self.osymsFileHandle
61 
62  # Read the raw text file
63  with open(self.fpath, 'r') as fsfiletxt:
64  lines = fsfiletxt.readlines()
65  lines = [line.strip().split(' ') for line in lines]
66 
67  for index in self.fieldFileDict:
68  fh = self.fieldFileDict[index]
69  field_count = 1
70  for line in lines:
71  try:
72  field = line[index]
73  except:
74  pass
75  else:
76  fh.write("{} {}".format(field_count + 1, field))
77  fh.close()
78 
79  def compile(self):
80  """Method to compile FSA/FST after generating symbols files using
81  generateSymsFiles"""
82 
83 
84  def error(self, *args, **kwargs):
85  """Error function of FstCompiler class"""
86  print("[Kaldi-Grammar_Parser]", *args, file=sys.stderr, **kwargs)
87  sys.exit(1)
88 
89  def warning(self, *args, **kwargs):
90  """Warning method of FstCompiler class"""
91  print("[Kaldi-Grammar_Parser]", *args, file=sys.stdout, **kwargs)
92 
93 
94 
95 if __name__ == "__main__":
96 
97  import sys
98  if len(sys.argv) < 3 or "-h" in sys.argv or "--help" in sys.argv:
99  print("""
100  Usage: python makeSymbols file fieldNumber
101 
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
106 
107  The Symbols Table is output to standard out, and can be piped into a file
108  """)
109  sys.exit(1)
110  else:
111  fname = sys.argv[1]
112  fieldNumber = int(sys.argv[2])
113 
114  sym_gen = FsSymbolGenerator(fname, fieldNumber)
115  sym_gen.generate()
116 
fstcompiler.FstCompiler.fname
fname
Definition: fstcompiler.py:40
fstcompiler.FstCompiler.osymsFileHandle
osymsFileHandle
Definition: fstcompiler.py:58
fstcompiler.FstCompiler.warning
def warning(self, *args, **kwargs)
Definition: fstcompiler.py:89
fstcompiler.FstCompiler.fpath
fpath
Definition: fstcompiler.py:17
fstcompiler.FstCompiler.outpath
outpath
Definition: fstcompiler.py:23
fstcompiler.FstCompiler
Definition: fstcompiler.py:11
fstcompiler.FstCompiler.isymsFileHandle
isymsFileHandle
Definition: fstcompiler.py:50
fstcompiler.FstCompiler.isymsFile
isymsFile
Definition: fstcompiler.py:44
fstcompiler.FstCompiler.__init__
def __init__(self, fpath, outpath)
Definition: fstcompiler.py:13
fstcompiler.FstCompiler.osymsFile
osymsFile
Definition: fstcompiler.py:45
fstcompiler.FstCompiler.fsFile
fsFile
Definition: fstcompiler.py:43
fstcompiler.FstCompiler.generateSymsFiles
def generateSymsFiles(self)
Definition: fstcompiler.py:47
fstcompiler.FstCompiler.error
def error(self, *args, **kwargs)
Definition: fstcompiler.py:84
fstcompiler.FstCompiler.fstype
fstype
Definition: fstcompiler.py:41
fstcompiler.FstCompiler.fieldFileDict
fieldFileDict
Definition: fstcompiler.py:54
fstcompiler.FstCompiler.compile
def compile(self)
Definition: fstcompiler.py:79