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