GRM-SFST  sfst-1.2.1
OpenGrm SFst Library
sfstshortestdistance.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 // Computes the shortest distance with failure transitions in a stochastic FST.
15 
16 #include <cstring>
17 #include <string>
18 #include <vector>
19 
20 #include <fst/flags.h>
21 #include <fst/fst-decl.h>
22 #include <fst/fst.h>
23 #include <fst/shortest-distance.h>
24 #include <sfst/shortest-distance.h>
25 #include <sfst/state-weights.h>
26 
27 DEFINE_int64(phi_label, fst::kNoLabel,
28  "Specifies failure label (default: none)");
29 DEFINE_bool(reverse, false, "Perform in the reverse direction");
30 DEFINE_double(delta, fst::kShortestDelta, "Convergence delta");
31 
32 int main(int argc, char **argv) {
33  namespace f = fst;
34  std::string usage = "Computes the shortest distance with failure transitions";
35  usage += " in a stochastic FST.\n\n Usage: ";
36  usage += argv[0];
37  usage += " [in.fst [out.fst]]\n";
38 
39  SET_FLAGS(usage.c_str(), &argc, &argv, true);
40  if (argc > 3) {
41  ShowUsage();
42  return 1;
43  }
44 
45  const std::string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0))
46  ? argv[1] : "";
47  const std::string out_name = argc > 2 ? argv[2] : "";
48 
49  f::StdFst *fst = f::StdFst::Read(in_name);
50  if (!fst) return 1;
51 
52  std::vector<f::StdArc::Weight> distance;
53  auto total_weight = sfst::ShortestDistance(
54  *fst, &distance, FST_FLAGS_phi_label,
55  FST_FLAGS_reverse, FST_FLAGS_delta);
56  if (!total_weight.Member())
57  return 1;
58 
59  if (!sfst::WriteWeights(out_name, distance))
60  return 1;
61 
62  return 0;
63 }
DEFINE_int64(phi_label, fst::kNoLabel,"Specifies failure label (default: none)")
Definition: perplexity.h:32
int main(int argc, char **argv)
void WriteWeights(std::ostream &strm, const std::vector< Weight > &weights)
Arc::Weight ShortestDistance(const fst::Fst< Arc > &fst, std::vector< typename Arc::Weight > *distance, typename Arc::Label phi_label=fst::kNoLabel, bool reverse=false, float delta=fst::kShortestDelta)
DEFINE_bool(reverse, false,"Perform in the reverse direction")
DEFINE_double(delta, fst::kShortestDelta,"Convergence delta")