1
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 //
14 // Copyright 2005-2010 Google, Inc.
15 // Author: johans@google.com (Johan Schalkwyk)
16 // Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
17 //
18
19 #include <fst/script/replace.h>
20
21 DEFINE_bool(epsilon_on_replace, false, "Create an espilon arc when recursing");
22
main(int argc,char ** argv)23 int main(int argc, char **argv) {
24 namespace s = fst::script;
25 using fst::script::FstClass;
26 using fst::script::VectorFstClass;
27
28 string usage = "Recursively replaces FST arcs with other FST(s).\n\n"
29 " Usage: ";
30 usage += argv[0];
31 usage += " root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
32
33 std::set_new_handler(FailedNewHandler);
34 SET_FLAGS(usage.c_str(), &argc, &argv, true);
35 if (argc < 4) {
36 ShowUsage();
37 return 1;
38 }
39
40 string in_fname = argv[1];
41 string out_fname = argc % 2 == 0 ? argv[argc - 1] : "";
42
43 FstClass *ifst = FstClass::Read(in_fname);
44 if (!ifst) return 1;
45
46 typedef int64 Label;
47 typedef pair<Label, const s::FstClass* > FstTuple;
48 vector<FstTuple> fst_tuples;
49 Label root = atoll(argv[2]);
50 fst_tuples.push_back(make_pair(root, ifst));
51
52 for (size_t i = 3; i < argc - 1; i += 2) {
53 ifst = s::FstClass::Read(argv[i]);
54 if (!ifst) return 1;
55 Label lab = atoll(argv[i + 1]);
56 fst_tuples.push_back(make_pair(lab, ifst));
57 }
58
59 VectorFstClass ofst(ifst->ArcType());
60 Replace(fst_tuples, &ofst, root, FLAGS_epsilon_on_replace);
61
62 ofst.Write(out_fname);
63
64 return 0;
65 }
66