• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@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 
main(int argc,char ** argv)35 int main(int argc, char **argv) {
36   namespace s = fst::script;
37 
38   string usage = "Compose a PDT and an FST.\n\n  Usage: ";
39   usage += argv[0];
40   usage += " in.pdt in.fst [out.pdt]\n";
41   usage += " in.fst in.pdt [out.pdt]\n";
42 
43   std::set_new_handler(FailedNewHandler);
44   SetFlags(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 = 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   s::FstClass *ifst1 = s::FstClass::Read(in1_name);
60   if (!ifst1) return 1;
61   s::FstClass *ifst2 = s::FstClass::Read(in2_name);
62   if (!ifst2) return 1;
63 
64   if (FLAGS_pdt_parentheses.empty()) {
65     LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
66     return 1;
67   }
68 
69   vector<pair<int64, int64> > parens;
70   fst::ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false);
71 
72   s::VectorFstClass ofst(ifst1->ArcType());
73   fst::ComposeOptions copts(false);
74 
75   s::PdtCompose(*ifst1, *ifst2, parens, &ofst, copts, FLAGS_left_pdt);
76 
77   if (FLAGS_connect)
78     s::Connect(&ofst);
79   ofst.Write(out_name);
80 
81   return 0;
82 }
83