FST  openfst-1.8.3
OpenFst Library
fstmap-main.cc
Go to the documentation of this file.
1 // Copyright 2005-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 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Applies an operation to each arc of an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
26 #include <fst/script/fst-class.h>
27 #include <fst/script/getters.h>
28 #include <fst/script/map.h>
30 
31 DECLARE_double(delta);
32 DECLARE_string(map_type);
33 DECLARE_double(power);
34 DECLARE_string(weight);
35 
36 int fstmap_main(int argc, char **argv) {
37  namespace s = fst::script;
40 
41  std::string usage =
42  "Applies an operation to each arc of an FST.\n\n Usage: ";
43  usage += argv[0];
44  usage += " [in.fst [out.fst]]\n";
45 
46  SET_FLAGS(usage.c_str(), &argc, &argv, true);
47  if (argc > 3) {
48  ShowUsage();
49  return 1;
50  }
51 
52  const std::string in_name =
53  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
54  const std::string out_name =
55  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
56 
57  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
58  if (!ifst) return 1;
59 
60  s::MapType map_type;
61  if (!s::GetMapType(FST_FLAGS_map_type, &map_type)) {
62  LOG(ERROR) << argv[0] << ": Unknown or unsupported map type "
63  << FST_FLAGS_map_type;
64  return 1;
65  }
66 
67  const auto weight_param =
68  !FST_FLAGS_weight.empty()
69  ? WeightClass(ifst->WeightType(), FST_FLAGS_weight)
70  : (FST_FLAGS_map_type == "times"
71  ? WeightClass::One(ifst->WeightType())
72  : WeightClass::Zero(ifst->WeightType()));
73 
74  std::unique_ptr<FstClass> ofst(
75  s::Map(*ifst, map_type, FST_FLAGS_delta,
76  FST_FLAGS_power, weight_param));
77 
78  return !ofst->Write(out_name);
79 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
bool GetMapType(std::string_view str, MapType *map_type)
Definition: getters.cc:123
MapType
Definition: map.h:56
DECLARE_double(delta)
#define LOG(type)
Definition: log.h:53
DECLARE_string(map_type)
void Map(FarReader< Arc > &reader, FarWriter< Arc > &writer, Functor functor)
Definition: map-reduce.h:49
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
int fstmap_main(int argc, char **argv)
Definition: fstmap-main.cc:36