FST  openfst-1.8.3
OpenFst Library
fstloglinearapply-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 #include <cstring>
19 #include <string>
20 
21 #include <fst/flags.h>
22 #include <fst/log.h>
24 #include <fst/arc.h>
25 #include <fst/cache.h>
26 #include <fst/fst.h>
27 #include <fst/pair-weight.h>
28 #include <fst/vector-fst.h>
29 
30 DECLARE_bool(normalize);
31 
32 int fstloglinearapply_main(int argc, char **argv) {
33  std::string usage =
34  "Applies an FST to another FST, treating the second as a log-linear "
35  "model.\n\n "
36  "Usage: ";
37  usage += argv[0];
38  usage += " in.fst linear.fst [out.fst]\n";
39 
40  SET_FLAGS(usage.c_str(), &argc, &argv, true);
41  if (argc < 3 || argc > 4) {
42  ShowUsage();
43  return 1;
44  }
45 
46  std::string in_name = strcmp(argv[1], "-") != 0 ? argv[1] : "";
47  std::string linear_name =
48  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
49  std::string out_name =
50  (argc > 3 && (strcmp(argv[3], "-") != 0)) ? argv[3] : "";
51 
52  if (in_name.empty() && linear_name.empty()) {
53  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
54  return 1;
55  }
56 
57  fst::StdFst *ifst1 = fst::StdFst::Read(in_name);
58  if (!ifst1) return 1;
59 
60  fst::StdFst *ifst2 = fst::StdFst::Read(linear_name);
61  if (!ifst2) return 1;
62 
63  fst::StdVectorFst ofst;
64 
65  fst::LogLinearApply(*ifst1, *ifst2, &ofst,
66  FST_FLAGS_normalize);
67 
68  return !ofst.Write(out_name);
69 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
static Fst * Read(std::istream &strm, const FstReadOptions &opts)
Definition: fst.h:257
bool Write(std::ostream &strm, const FstWriteOptions &opts) const override
Definition: vector-fst.h:585
#define LOG(type)
Definition: log.h:53
void LogLinearApply(const Fst< A > &ifst, const Fst< A > &lfst, MutableFst< A > *ofst, bool normalize=true)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
int fstloglinearapply_main(int argc, char **argv)
DECLARE_bool(normalize)