FST  openfst-1.8.3
OpenFst Library
fstreweight-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 // Reweights an FST.
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
26 #include <fst/reweight.h>
27 #include <fst/script/fst-class.h>
28 #include <fst/script/getters.h>
29 #include <fst/script/reweight.h>
30 #include <fst/script/text-io.h>
32 
33 DECLARE_string(reweight_type);
34 
35 int fstreweight_main(int argc, char **argv) {
36  namespace s = fst::script;
37  using fst::ReweightType;
40 
41  std::string usage = "Reweights an FST.\n\n Usage: ";
42  usage += argv[0];
43  usage += " in.fst potential.txt [out.fst]\n";
44 
45  SET_FLAGS(usage.c_str(), &argc, &argv, true);
46  if (argc < 3 || argc > 4) {
47  ShowUsage();
48  return 1;
49  }
50 
51  const std::string in_name = argv[1];
52  const std::string potentials_name = argv[2];
53  const std::string out_name = argc > 3 ? argv[3] : "";
54 
55  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
56  if (!fst) return 1;
57 
58  std::vector<WeightClass> potential;
59  if (!s::ReadPotentials(fst->WeightType(), potentials_name, &potential)) {
60  return 1;
61  }
62 
63  ReweightType reweight_type;
64  if (!s::GetReweightType(FST_FLAGS_reweight_type, &reweight_type)) {
65  LOG(ERROR) << argv[0] << ": Unknown or unsupported reweight type: "
66  << FST_FLAGS_reweight_type;
67  return 1;
68  }
69 
70  s::Reweight(fst.get(), potential, reweight_type);
71 
72  return !fst->Write(out_name);
73 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
ReweightType
Definition: reweight.h:35
#define LOG(type)
Definition: log.h:53
int fstreweight_main(int argc, char **argv)
DECLARE_string(reweight_type)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
bool ReadPotentials(std::string_view weight_type, const std::string &source, std::vector< WeightClass > *potentials)
Definition: text-io.cc:38
bool GetReweightType(std::string_view str, ReweightType *reweight_type)
Definition: getters.cc:219
void Reweight(MutableFst< Arc > *fst, const std::vector< typename Arc::Weight > &potential, ReweightType type)
Definition: reweight.h:46