1 // fstcompose.cc 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 // Copyright 2005-2010 Google, Inc. 16 // Author: riley (at) google.com (Michael Riley) 17 // Modified: jpr (at) google.com (Jake Ratkiewicz) to use FstClass 18 // 19 // \file 20 // Composes two FSTs. 21 // 22 23 #include <fst/script/compose.h> 24 #include <fst/script/connect.h> 25 26 27 DEFINE_string(compose_filter, "auto", 28 "Composition filter, one of: \"alt_sequence\", \"auto\", " 29 "\"match\", \"sequence\""); 30 DEFINE_bool(connect, true, "Trim output"); 31 32 33 int main(int argc, char **argv) { 34 namespace s = fst::script; 35 using fst::script::FstClass; 36 using fst::script::MutableFstClass; 37 using fst::script::VectorFstClass; 38 39 string usage = "Composes two FSTs.\n\n Usage: "; 40 usage += argv[0]; 41 usage += " in1.fst in2.fst [out.fst]\n"; 42 43 std::set_new_handler(FailedNewHandler); 44 SET_FLAGS(usage.c_str(), &argc, &argv, true); 45 if (argc < 3 || argc > 4) { 46 ShowUsage(); 47 return 1; 48 } 49 50 string in1_name = strcmp(argv[1], "-") != 0 ? argv[1] : ""; 51 string in2_name = (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : ""; 52 string out_name = argc > 3 ? argv[3] : ""; 53 54 if (in1_name.empty() && in2_name.empty()) { 55 LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input."; 56 return 1; 57 } 58 59 FstClass *ifst1 = FstClass::Read(in1_name); 60 if (!ifst1) return 1; 61 62 FstClass *ifst2 = FstClass::Read(in2_name); 63 if (!ifst2) return 1; 64 65 if (ifst1->ArcType() != ifst2->ArcType()) { 66 LOG(ERROR) << argv[0] << ": Input FSTs must have the same arc type."; 67 return 1; 68 } 69 70 VectorFstClass ofst(ifst1->ArcType()); 71 72 fst::ComposeFilter compose_filter; 73 74 if (FLAGS_compose_filter == "alt_sequence") { 75 compose_filter = fst::ALT_SEQUENCE_FILTER; 76 } else if (FLAGS_compose_filter == "auto") { 77 compose_filter = fst::AUTO_FILTER; 78 } else if (FLAGS_compose_filter == "match") { 79 compose_filter = fst::MATCH_FILTER; 80 } else if (FLAGS_compose_filter == "sequence") { 81 compose_filter = fst::SEQUENCE_FILTER; 82 } else { 83 LOG(ERROR) << argv[0] << "Unknown compose filter type: " 84 << FLAGS_compose_filter; 85 return 1; 86 } 87 88 fst::ComposeOptions opts(FLAGS_connect, compose_filter); 89 90 s::Compose(*ifst1, *ifst2, &ofst, opts); 91 92 ofst.Write(out_name); 93 94 return 0; 95 } 96