Home | History | Annotate | Download | only in bin
      1 // fstintersect.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 // Intersects two FSTs.
     21 //
     22 
     23 #include <fst/script/intersect.h>
     24 #include <fst/script/connect.h>
     25 
     26 DEFINE_string(compose_filter, "auto",
     27               "Composition filter, one of: \"alt_sequence\", \"auto\", "
     28               "\"match\", \"sequence\"");
     29 DEFINE_bool(connect, true, "Trim output");
     30 
     31 int main(int argc, char **argv) {
     32   namespace s = fst::script;
     33   using fst::script::FstClass;
     34   using fst::script::VectorFstClass;
     35 
     36 
     37   string usage = "Intersects two FSAs.\n\n  Usage: ";
     38   usage += argv[0];
     39   usage += " in1.fst in2.fst [out.fst]\n";
     40   usage += "  Flags: connect\n";
     41 
     42   std::set_new_handler(FailedNewHandler);
     43   SetFlags(usage.c_str(), &argc, &argv, true);
     44   if (argc < 3 || argc > 4) {
     45     ShowUsage();
     46     return 1;
     47   }
     48 
     49   string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
     50   string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
     51   string out_name = argc > 3 ? argv[3] : "";
     52 
     53   if (in1_name.empty() && in2_name.empty()) {
     54     LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
     55     return 1;
     56   }
     57 
     58   FstClass *ifst1 = FstClass::Read(in1_name);
     59   if (!ifst1) return 1;
     60   FstClass *ifst2 = FstClass::Read(in2_name);
     61   if (!ifst2) return 1;
     62 
     63   VectorFstClass ofst(ifst1->ArcType());
     64 
     65   fst::ComposeFilter compose_filter;
     66 
     67   if (FLAGS_compose_filter == "alt_sequence") {
     68     compose_filter = fst::ALT_SEQUENCE_FILTER;
     69   } else if (FLAGS_compose_filter == "auto") {
     70     compose_filter = fst::AUTO_FILTER;
     71   } else if (FLAGS_compose_filter == "match") {
     72     compose_filter = fst::MATCH_FILTER;
     73   } else if (FLAGS_compose_filter == "sequence") {
     74     compose_filter = fst::SEQUENCE_FILTER;
     75   } else {
     76     LOG(ERROR) << argv[0] << "Unknown compose filter type: "
     77                << FLAGS_compose_filter;
     78     return 1;
     79   }
     80 
     81   fst::IntersectOptions opts(FLAGS_connect, compose_filter);
     82 
     83   s::Intersect(*ifst1, *ifst2, &ofst, opts);
     84 
     85   ofst.Write(out_name);
     86 
     87   return 0;
     88 }
     89