FST  openfst-1.7.2
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<string> &in_fnames, const string &out_fname,
20  const int32 generate_keys, const FarType &far_type,
21  const string &key_prefix, const string &key_suffix) {
22  std::unique_ptr<FarWriter<Arc>> far_writer(
23  FarWriter<Arc>::Create(out_fname, far_type));
24  if (!far_writer) return;
25  for (size_t i = 0; i < in_fnames.size(); ++i) {
26  std::unique_ptr<Fst<Arc>> ifst(Fst<Arc>::Read(in_fnames[i]));
27  if (!ifst) return;
28  string key;
29  if (generate_keys > 0) {
30  std::ostringstream keybuf;
31  keybuf.width(generate_keys);
32  keybuf.fill('0');
33  keybuf << i + 1;
34  key = keybuf.str();
35  } else {
36  auto *filename = new char[in_fnames[i].size() + 1];
37  strcpy(filename, in_fnames[i].c_str());
38  key = basename(filename);
39  delete[] filename;
40  }
41  far_writer->Add(key_prefix + key + key_suffix, *ifst);
42  }
43 }
44 
45 } // namespace fst
46 
47 #endif // FST_EXTENSIONS_FAR_CREATE_H_
void FarCreate(const std::vector< string > &in_fnames, const string &out_fname, const int32 generate_keys, const FarType &far_type, const string &key_prefix, const string &key_suffix)
Definition: create.h:19
FarType
Definition: far.h:71
int32_t int32
Definition: types.h:26