FST  openfst-1.8.2
OpenFst Library
create.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Creates a finite-state archive from component FSTs.
19 
20 #ifndef FST_EXTENSIONS_FAR_CREATE_H_
21 #define FST_EXTENSIONS_FAR_CREATE_H_
22 
23 #include <libgen.h>
24 
25 #include <cstdint>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 
30 #include <fst/extensions/far/far.h>
31 
32 namespace fst {
33 
34 template <class Arc>
35 void Create(const std::vector<std::string> &sources, FarWriter<Arc> &writer,
36  int32_t generate_keys, const std::string &key_prefix,
37  const std::string &key_suffix) {
38  for (size_t i = 0; i < sources.size(); ++i) {
39  std::unique_ptr<Fst<Arc>> ifst(Fst<Arc>::Read(sources[i]));
40  if (!ifst) return;
41  std::string key;
42  if (generate_keys > 0) {
43  std::ostringstream keybuf;
44  keybuf.width(generate_keys);
45  keybuf.fill('0');
46  keybuf << i + 1;
47  key = keybuf.str();
48  } else {
49  auto source =
50  fst::make_unique_for_overwrite<char[]>(sources[i].size() + 1);
51  strcpy(source.get(), sources[i].c_str()); // NOLINT(runtime/printf)
52  key = basename(source.get());
53  }
54  writer.Add(key_prefix + key + key_suffix, *ifst);
55  }
56 }
57 
58 } // namespace fst
59 
60 #endif // FST_EXTENSIONS_FAR_CREATE_H_
void Create(const std::vector< std::string > &sources, FarWriter< Arc > &writer, int32_t generate_keys, const std::string &key_prefix, const std::string &key_suffix)
Definition: create.h:35
virtual void Add(std::string_view key, const Fst< Arc > &fst)=0