GRM-SFST  sfst-1.2.1
OpenGrm SFst Library
sfstrandgen.cc
Go to the documentation of this file.
1 // Copyright 2018-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 // Generates random paths through a stochastic FST. The FST must be a
15 // normalized.
16 
17 #include <time.h>
18 
19 #include <climits>
20 #include <cstring>
21 #include <string>
22 
23 #include <fst/flags.h>
24 #include <fst/log.h>
25 #include <fst/arc.h>
26 #include <fst/fst.h>
27 #include <fst/randgen.h>
28 #include <fst/vector-fst.h>
29 #include <sfst/normalize.h>
30 #include <sfst/randgen.h>
31 
32 DEFINE_int64(phi_label, fst::kNoLabel,
33  "Specifies failure label (default: none)");
34 DEFINE_int32(max_length, INT_MAX, "Maximum path length");
35 DEFINE_int64(npath, 1, "Number of paths to generate");
36 DEFINE_int32(seed, time(0), "Random seed");
37 DEFINE_bool(weighted, false,
38  "Output tree weighted by path count vs. unweighted paths");
39 DEFINE_bool(remove_total_weight, false,
40  "Remove total weight when output weighted");
41 DEFINE_bool(minimal, false,
42  "Epsilon/phi-remove and minimize when output is weighted");
43 DEFINE_bool(stochastic, false,
44  "Same as --weighted --remove_total_weight --minimal");
45 
46 
47 int main(int argc, char **argv) {
48  namespace f = fst;
49  std::string usage = "Generates random paths through an LM.\n\n Usage: ";
50  usage += argv[0];
51  usage += " [in.fst [out.fst]]\n";
52 
53  SET_FLAGS(usage.c_str(), &argc, &argv, true);
54  if (FST_FLAGS_stochastic) {
55  }
56 
57  if (argc > 3) {
58  ShowUsage();
59  return 1;
60  }
61 
62  VLOG(1) << argv[0] << ": Seed = " << FST_FLAGS_seed;
63 
64  std::string in_name =
65  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
66  std::string out_name = argc > 2 ? argv[2] : "";
67 
68  f::StdFst *ifst = f::StdFst::Read(in_name);
69  if (!ifst) return 1;
70 
71  if (!sfst::IsNormalized(*ifst, FST_FLAGS_phi_label)) {
72  LOG(ERROR) << argv[0] << ": Input is not a normalized stochastic FST";
73  return 1;
74  }
75 
76  f::StdVectorFst ofst;
77 
78  sfst::SFstArcSelector<f::StdArc> selector(FST_FLAGS_seed,
79  FST_FLAGS_phi_label);
80  f::RandGenOptions<sfst::SFstArcSelector<f::StdArc>> opts(
81  selector, FST_FLAGS_max_length, FST_FLAGS_npath,
82  FST_FLAGS_weighted, FST_FLAGS_remove_total_weight);
83  f::RandGen(*ifst, &ofst, opts);
84  if (FST_FLAGS_weighted && FST_FLAGS_minimal)
85  f::RandMinimize(&ofst, FST_FLAGS_phi_label);
86  if (!ofst.Write(out_name))
87  return 1;
88 
89  return 0;
90 }
DEFINE_bool(weighted, false,"Output tree weighted by path count vs. unweighted paths")
Definition: perplexity.h:32
void RandMinimize(MutableFst< A > *fst, typename A::Label phi_label)
Definition: randgen.h:365
DEFINE_int32(max_length, INT_MAX,"Maximum path length")
bool IsNormalized(const fst::Fst< Arc > &fst, typename Arc::Label phi_label=fst::kNoLabel, float delta=fst::kDelta)
Definition: normalize.h:159
int main(int argc, char **argv)
Definition: sfstrandgen.cc:47
DEFINE_int64(phi_label, fst::kNoLabel,"Specifies failure label (default: none)")