FST  openfst-1.8.3
OpenFst Library
fstrandgen-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 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 // Generates random paths through an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
26 #include <fst/randgen.h>
27 #include <fst/script/fst-class.h>
28 #include <fst/script/getters.h>
29 #include <fst/script/randgen.h>
30 #include <fst/script/script-impl.h>
31 
32 DECLARE_int32(max_length);
33 DECLARE_int32(npath);
34 DECLARE_uint64(seed);
35 DECLARE_string(select);
36 DECLARE_bool(weighted);
37 DECLARE_bool(remove_total_weight);
38 
39 int fstrandgen_main(int argc, char **argv) {
40  namespace s = fst::script;
41  using fst::RandGenOptions;
44 
45  std::string usage = "Generates random paths through an FST.\n\n Usage: ";
46  usage += argv[0];
47  usage += " [in.fst [out.fst]]\n";
48 
49  SET_FLAGS(usage.c_str(), &argc, &argv, true);
50  if (argc > 3) {
51  ShowUsage();
52  return 1;
53  }
54 
55  const std::string in_name =
56  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
57  const std::string out_name =
58  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
59 
60  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
61  if (!ifst) return 1;
62 
63  VectorFstClass ofst(ifst->ArcType());
64 
66  if (!s::GetRandArcSelection(FST_FLAGS_select, &ras)) {
67  LOG(ERROR) << argv[0] << ": Unknown or unsupported select type "
68  << FST_FLAGS_select;
69  return 1;
70  }
71 
72  const auto seed = s::GetSeed(FST_FLAGS_seed);
73  VLOG(1) << argv[0] << ": Seed = " << seed;
74 
75  s::RandGen(*ifst, &ofst,
76  RandGenOptions<s::RandArcSelection>(
77  ras, FST_FLAGS_max_length,
78  FST_FLAGS_npath, FST_FLAGS_weighted,
79  FST_FLAGS_remove_total_weight),
80  seed);
81 
82  return !ofst.Write(out_name);
83 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
bool GetRandArcSelection(std::string_view str, RandArcSelection *ras)
Definition: getters.cc:171
#define LOG(type)
Definition: log.h:53
int fstrandgen_main(int argc, char **argv)
DECLARE_uint64(seed)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
uint64_t GetSeed(uint64_t seed)
Definition: getters.cc:230
void RandGen(const Fst< FromArc > &ifst, MutableFst< ToArc > *ofst, const RandGenOptions< Selector > &opts)
Definition: randgen.h:751
#define VLOG(level)
Definition: log.h:54
DECLARE_string(select)
DECLARE_bool(weighted)
DECLARE_int32(max_length)