Home | History | Annotate | Download | only in pdt
      1 // pdtshortestpath.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 // Return the shortest path in a (bounded-stack) PDT.
     21 //
     22 
     23 #include <fst/extensions/pdt/pdtscript.h>
     24 #include <fst/util.h>
     25 
     26 
     27 DEFINE_bool(keep_parentheses, false, "Keep PDT parentheses in result.");
     28 DEFINE_string(queue_type, "fifo", "Queue type: one of: "
     29               "\"fifo\", \"lifo\", \"state\"");
     30 DEFINE_bool(path_gc, true, "Garbage collect shortest path data");
     31 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs.");
     32 
     33 int main(int argc, char **argv) {
     34   namespace s = fst::script;
     35 
     36   string usage = "Shortest path in a (bounded-stack) PDT.\n\n  Usage: ";
     37   usage += argv[0];
     38   usage += " in.pdt [out.fst]\n";
     39 
     40   std::set_new_handler(FailedNewHandler);
     41   SET_FLAGS(usage.c_str(), &argc, &argv, true);
     42   if (argc > 3) {
     43     ShowUsage();
     44     return 1;
     45   }
     46 
     47   string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
     48   string out_name = argc > 2 ? argv[2] : "";
     49 
     50   s::FstClass *ifst = s::FstClass::Read(in_name);
     51   if (!ifst) return 1;
     52 
     53   if (FLAGS_pdt_parentheses.empty()) {
     54     LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
     55     return 1;
     56   }
     57 
     58   vector<pair<int64, int64> > parens, rparens;
     59   fst::ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false);
     60 
     61   s::VectorFstClass ofst(ifst->ArcType());
     62 
     63   fst::QueueType qt;
     64 
     65   if (FLAGS_queue_type == "fifo") {
     66     qt = fst::FIFO_QUEUE;
     67   } else if (FLAGS_queue_type == "lifo") {
     68     qt = fst::LIFO_QUEUE;
     69   } else if (FLAGS_queue_type == "state") {
     70     qt = fst::STATE_ORDER_QUEUE;
     71   } else {
     72     LOG(ERROR) << "Unknown or unsupported queue type: " << FLAGS_queue_type;
     73     return 1;
     74   }
     75 
     76   s::PdtShortestPathOptions opts(qt, FLAGS_keep_parentheses, FLAGS_path_gc);
     77   s::PdtShortestPath(*ifst, parens, &ofst, opts);
     78   ofst.Write(out_name);
     79 
     80   return 0;
     81 }
     82