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_string(call_arc_labeling, "input",
22 "Which labels to make non-epsilon on the call arc. "
23 "One of: \"input\" (default), \"output\", \"both\", \"neither\"");
24 DEFINE_string(return_arc_labeling, "neither",
25 "Which labels to make non-epsilon on the return arc. "
26 "One of: \"input\", \"output\", \"both\", \"neither\" (default)");
27 DEFINE_int64(return_label, 0, "Label to put on return arc");
28 DEFINE_bool(epsilon_on_replace, false,
29 "For backward compatability: call/return arcs are epsilon arcs");
30
31 // Assigns replace_label_type from enum values based on command line switches
replace_type(string * arc_labeling,char * binname,string errmsg,bool epsilon_on_replace)32 fst::ReplaceLabelType replace_type(string *arc_labeling, char *binname,
33 string errmsg, bool epsilon_on_replace) {
34 fst::ReplaceLabelType replace_label_type;
35 if ((*arc_labeling) == "neither" || epsilon_on_replace) {
36 replace_label_type = fst::REPLACE_LABEL_NEITHER;
37 } else if ((*arc_labeling) == "input") {
38 replace_label_type = fst::REPLACE_LABEL_INPUT;
39 } else if ((*arc_labeling) == "output") {
40 replace_label_type = fst::REPLACE_LABEL_OUTPUT;
41 } else if ((*arc_labeling) == "both") {
42 replace_label_type = fst::REPLACE_LABEL_BOTH;
43 } else {
44 LOG(ERROR) << binname << errmsg
45 << "arc labeling option: " << (*arc_labeling);
46 exit(1);
47 }
48 return replace_label_type;
49 }
50
main(int argc,char ** argv)51 int main(int argc, char **argv) {
52 namespace s = fst::script;
53 using fst::script::FstClass;
54 using fst::script::VectorFstClass;
55
56 string usage = "Recursively replaces FST arcs with other FST(s).\n\n"
57 " Usage: ";
58 usage += argv[0];
59 usage += " root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
60
61 std::set_new_handler(FailedNewHandler);
62 SET_FLAGS(usage.c_str(), &argc, &argv, true);
63 if (argc < 4) {
64 ShowUsage();
65 return 1;
66 }
67
68 string in_fname = argv[1];
69 string out_fname = argc % 2 == 0 ? argv[argc - 1] : "";
70
71 FstClass *ifst = FstClass::Read(in_fname);
72 if (!ifst) return 1;
73
74 typedef int64 Label;
75 typedef pair<Label, const s::FstClass* > FstTuple;
76 vector<FstTuple> fst_tuples;
77 Label root = atoll(argv[2]);
78 fst_tuples.push_back(make_pair(root, ifst));
79
80 for (size_t i = 3; i < argc - 1; i += 2) {
81 ifst = s::FstClass::Read(argv[i]);
82 if (!ifst) return 1;
83 Label lab = atoll(argv[i + 1]);
84 fst_tuples.push_back(make_pair(lab, ifst));
85 }
86
87 fst::ReplaceLabelType call_label_type =
88 replace_type(&FLAGS_call_arc_labeling, argv[0], ": bad call ",
89 FLAGS_epsilon_on_replace);
90 fst::ReplaceLabelType return_label_type =
91 replace_type(&FLAGS_return_arc_labeling, argv[0], ": bad return ",
92 FLAGS_epsilon_on_replace);
93
94 VectorFstClass ofst(ifst->ArcType());
95 s::ReplaceOptions opts(root, call_label_type, return_label_type,
96 FLAGS_return_label);
97 s::Replace(fst_tuples, &ofst, opts);
98
99 ofst.Write(out_fname);
100
101 return 0;
102 }
103