GRM-SFST  sfst-1.2.1
OpenGrm SFst Library
sfsttopology.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 // Algorithms for constructing specific FST topologies.
15 
16 #include <cstring>
17 #include <string>
18 
19 #include <fst/flags.h>
20 #include <fst/log.h>
21 #include <fst/arc.h>
22 #include <fst/fst.h>
23 #include <fst/vector-fst.h>
24 #include <sfst/topology.h>
25 
26 DEFINE_int64(phi_label, fst::kNoLabel,
27  "Specifies failure label (default: none)");
28 DEFINE_string(method, "ngram",
29  "Specifies topology method, one of: "
30  "\"ngram\"");
31 DEFINE_int64(order, 3, "Set maximal order of ngram model");
32 
33 int main(int argc, char **argv) {
34  namespace f = fst;
35  std::string usage =
36  "Algorithms for constructing specific FST topologies.\n\n";
37  usage += " Usage: ";
38  usage += argv[0];
39  usage += " [in.fst [out.fst]]\n";
40 
41  SET_FLAGS(usage.c_str(), &argc, &argv, true);
42  if (argc > 3) {
43  ShowUsage();
44  return 1;
45  }
46 
47  std::string in_name =
48  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
49  std::string out_name = argc > 2 ? argv[2] : "";
50 
51  f::StdFst *ifst = f::StdFst::Read(in_name);
52  if (!ifst) return 1;
53 
54  f::StdVectorFst ofst;
55 
56  if (FST_FLAGS_method == "ngram") {
57  sfst::NGramTopology<f::StdArc> ngram(FST_FLAGS_order,
58  FST_FLAGS_phi_label, &ofst);
59  ngram.FindNGrams(*ifst);
60  } else {
61  LOG(ERROR) << argv[0]
62  << ": unknown topology method: " << FST_FLAGS_method;
63  return 1;
64  }
65 
66  if (!ofst.Write(out_name))
67  return 1;
68 
69  return 0;
70 }
Definition: perplexity.h:32
DEFINE_string(method,"ngram","Specifies topology method, one of: ""\"ngram\"")
DEFINE_int64(phi_label, fst::kNoLabel,"Specifies failure label (default: none)")
void FindNGrams(const fst::Fst< Arc > &ifst)
Definition: topology.h:132
int main(int argc, char **argv)
Definition: sfsttopology.cc:33