GRM-SFST  sfst-1.2.1
OpenGrm SFst Library
sfstcount.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 // Algorithm to count from a stochastic FST w.r.t. a backoff-complete FST whose
15 // topology is provided. Result is FST with backoff-complete topology weighted
16 // by expected counts derived from the input stochastic FST.
17 
18 #include <cstring>
19 #include <string>
20 
21 #include <fst/flags.h>
22 #include <fst/log.h>
23 #include <fst/arc.h>
24 #include <fst/fst.h>
25 #include <fst/mutable-fst.h>
26 #include <fst/properties.h>
27 #include <sfst/approx.h>
28 #include <sfst/count.h>
29 #include <sfst/normalize.h>
30 
31 DEFINE_int64(phi_label, fst::kNoLabel,
32  "Specifies failure label (default: none)");
33 DEFINE_double(delta, sfst::kApproxDelta, "Convergence delta");
34 
35 int main(int argc, char **argv) {
36  namespace f = fst;
37  std::string usage = "Algorithm to count from stochastic FST w.r.t. a";
38  usage += " backoff-complete FST whose topology is provided.\n\n Usage: ";
39  usage += argv[0];
40  usage += " sfst.fst top.fst [out.fst]\n";
41 
42  SET_FLAGS(usage.c_str(), &argc, &argv, true);
43  if (argc > 4) {
44  ShowUsage();
45  return 1;
46  }
47 
48  const std::string in1_name = strcmp(argv[1], "-") != 0 ? argv[1] : "";
49  const std::string in2_name =
50  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
51  const std::string out_name = argc > 3 ? argv[3] : "";
52 
53  if (in1_name.empty() && in2_name.empty()) {
54  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
55  return 1;
56  }
57 
58  f::StdFst *ifst = f::StdFst::Read(in1_name);
59  if (!ifst) return 1;
60 
61  f::StdMutableFst *ofst = f::StdMutableFst::Read(in2_name, true);
62  if (!ofst) return 1;
63 
64  if (ifst->Properties(f::kCyclic, true) &&
65  !sfst::IsNormalized(*ifst, FST_FLAGS_phi_label)) {
66  LOG(ERROR) << argv[0] << ": First input is not a normalized stochastic FST";
67  return 1;
68  }
69 
70  sfst::Counter<f::StdArc> counter(FST_FLAGS_phi_label,
71  FST_FLAGS_delta, ofst);
72  counter.Count(*ifst);
73  counter.Finalize();
74 
75  if (ofst->Properties(f::kError, false))
76  return 1;
77 
78  if (!ofst->Write(out_name))
79  return 1;
80 
81  return 0;
82 }
void Finalize()
Definition: count.h:78
Definition: perplexity.h:32
DEFINE_int64(phi_label, fst::kNoLabel,"Specifies failure label (default: none)")
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: sfstcount.cc:35
constexpr float kApproxDelta
Definition: approx.h:30
DEFINE_double(delta, sfst::kApproxDelta,"Convergence delta")
void Count(const fst::Fst< Arc > &ifst)
Definition: count.h:246