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