FST  openfst-1.7.3
OpenFst Library
create.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Creates a finite-state archive from component FSTs.
5 
6 #ifndef FST_EXTENSIONS_FAR_CREATE_H_
7 #define FST_EXTENSIONS_FAR_CREATE_H_
8 
9 #include <libgen.h>
10 #include <sstream>
11 #include <string>
12 #include <vector>
13 
14 #include <fst/extensions/far/far.h>
15 
16 namespace fst {
17 
18 template <class Arc>
19 void FarCreate(const std::vector<std::string> &in_fnames,
20  const std::string &out_fname, const int32 generate_keys,
21  const FarType &far_type, const std::string &key_prefix,
22  const std::string &key_suffix) {
23  std::unique_ptr<FarWriter<Arc>> far_writer(
24  FarWriter<Arc>::Create(out_fname, far_type));
25  if (!far_writer) return;
26  for (size_t i = 0; i < in_fnames.size(); ++i) {
27  std::unique_ptr<Fst<Arc>> ifst(Fst<Arc>::Read(in_fnames[i]));
28  if (!ifst) return;
29  std::string key;
30  if (generate_keys > 0) {
31  std::ostringstream keybuf;
32  keybuf.width(generate_keys);
33  keybuf.fill('0');
34  keybuf << i + 1;
35  key = keybuf.str();
36  } else {
37  auto *filename = new char[in_fnames[i].size() + 1];
38  strcpy(filename, in_fnames[i].c_str());
39  key = basename(filename);
40  delete[] filename;
41  }
42  far_writer->Add(key_prefix + key + key_suffix, *ifst);
43  }
44 }
45 
46 } // namespace fst
47 
48 #endif // FST_EXTENSIONS_FAR_CREATE_H_
FarType
Definition: far.h:71
void FarCreate(const std::vector< std::string > &in_fnames, const std::string &out_fname, const int32 generate_keys, const FarType &far_type, const std::string &key_prefix, const std::string &key_suffix)
Definition: create.h:19
int32_t int32
Definition: types.h:26