Home | History | Annotate | Download | only in pdt
      1 // pdtcompose.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 //
     18 // \file
     19 // Composes a PDT and an FST.
     20 //
     21 
     22 #include <vector>
     23 using std::vector;
     24 #include <utility>
     25 using std::pair; using std::make_pair;
     26 
     27 #include <fst/util.h>
     28 #include <fst/extensions/pdt/pdtscript.h>
     29 #include <fst/script/connect.h>
     30 
     31 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs.");
     32 DEFINE_bool(left_pdt, true, "1st arg is PDT (o.w. 2nd arg).");
     33 DEFINE_bool(connect, true, "Trim output");
     34 DEFINE_string(compose_filter, "paren",
     35               "Composition filter, one of: \"expand\", \"expand_paren\", "
     36               "\"paren\"");
     37 
     38 int main(int argc, char **argv) {
     39   namespace s = fst::script;
     40 
     41   string usage = "Compose a PDT and an FST.\n\n  Usage: ";
     42   usage += argv[0];
     43   usage += " in.pdt in.fst [out.pdt]\n";
     44   usage += " in.fst in.pdt [out.pdt]\n";
     45 
     46   std::set_new_handler(FailedNewHandler);
     47   SET_FLAGS(usage.c_str(), &argc, &argv, true);
     48   if (argc < 3 || argc > 4) {
     49     ShowUsage();
     50     return 1;
     51   }
     52 
     53   string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
     54   string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
     55   string out_name = argc > 3 ? argv[3] : "";
     56 
     57   if (in1_name.empty() && in2_name.empty()) {
     58     LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
     59     return 1;
     60   }
     61 
     62   s::FstClass *ifst1 = s::FstClass::Read(in1_name);
     63   if (!ifst1) return 1;
     64   s::FstClass *ifst2 = s::FstClass::Read(in2_name);
     65   if (!ifst2) return 1;
     66 
     67   if (FLAGS_pdt_parentheses.empty()) {
     68     LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
     69     return 1;
     70   }
     71 
     72   vector<pair<int64, int64> > parens;
     73   fst::ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false);
     74 
     75   s::VectorFstClass ofst(ifst1->ArcType());
     76 
     77   fst::PdtComposeFilter compose_filter;
     78 
     79   if (FLAGS_compose_filter == "expand") {
     80     compose_filter = fst::EXPAND_FILTER;
     81   } else if (FLAGS_compose_filter == "expand_paren") {
     82     compose_filter = fst::EXPAND_PAREN_FILTER;
     83   } else if (FLAGS_compose_filter == "paren") {
     84     compose_filter = fst::PAREN_FILTER;
     85   } else {
     86     LOG(ERROR) << argv[0] << "Unknown compose filter type: "
     87                << FLAGS_compose_filter;
     88     return 1;
     89   }
     90 
     91   fst::PdtComposeOptions copts(false, compose_filter);
     92 
     93   s::PdtCompose(*ifst1, *ifst2, parens, &ofst, copts, FLAGS_left_pdt);
     94 
     95   if (FLAGS_connect)
     96     s::Connect(&ofst);
     97   ofst.Write(out_name);
     98 
     99   return 0;
    100 }
    101