FST  openfst-1.7.2
OpenFst Library
fstmap-main.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Applies an operation to each arc of an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/log.h>
13 #include <fst/script/getters.h>
14 #include <fst/script/map.h>
15 
16 DECLARE_double(delta);
17 DECLARE_string(map_type);
18 DECLARE_double(power);
19 DECLARE_string(weight);
20 
21 int fstmap_main(int argc, char **argv) {
22  namespace s = fst::script;
25 
26  string usage = "Applies an operation to each arc of an FST.\n\n Usage: ";
27  usage += argv[0];
28  usage += " [in.fst [out.fst]]\n";
29 
30  std::set_new_handler(FailedNewHandler);
31  SET_FLAGS(usage.c_str(), &argc, &argv, true);
32  if (argc > 3) {
33  ShowUsage();
34  return 1;
35  }
36 
37  const string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
38  const string out_name = argc > 2 ? argv[2] : "";
39 
40  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
41  if (!ifst) return 1;
42 
43  s::MapType map_type;
44  if (!s::GetMapType(FLAGS_map_type, &map_type)) {
45  LOG(ERROR) << argv[0] << ": Unknown or unsupported map type "
46  << FLAGS_map_type;
47  return 1;
48  }
49 
50  const auto weight_param =
51  !FLAGS_weight.empty()
52  ? WeightClass(ifst->WeightType(), FLAGS_weight)
53  : (FLAGS_map_type == "times" ? WeightClass::One(ifst->WeightType())
54  : WeightClass::Zero(ifst->WeightType()));
55 
56  std::unique_ptr<FstClass> ofst(
57  s::Map(*ifst, map_type, FLAGS_delta, FLAGS_power, weight_param));
58 
59  return !ofst->Write(out_name);
60 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
MapType
Definition: map.h:37
DECLARE_double(delta)
#define LOG(type)
Definition: log.h:48
DECLARE_string(map_type)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
void Map(MutableFst< A > *fst, C *mapper)
Definition: map.h:17
int fstmap_main(int argc, char **argv)
Definition: fstmap-main.cc:21
bool GetMapType(const string &str, MapType *map_type)
Definition: getters.cc:51