1 // pdtreverse.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 // Reverse a PDT.
20 //
21
22 #include <fst/extensions/pdt/pdtscript.h>
23 #include <fst/util.h>
24
25 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs.");
26
main(int argc,char ** argv)27 int main(int argc, char **argv) {
28 namespace s = fst::script;
29
30 string usage = "Reverse a PDT.\n\n Usage: ";
31 usage += argv[0];
32 usage += " in.pdt [out.fst]\n";
33
34 std::set_new_handler(FailedNewHandler);
35 SetFlags(usage.c_str(), &argc, &argv, true);
36 if (argc > 3) {
37 ShowUsage();
38 return 1;
39 }
40
41 string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
42 string out_name = argc > 2 ? argv[2] : "";
43
44 s::FstClass *ifst = s::FstClass::Read(in_name);
45 if (!ifst) return 1;
46
47 if (FLAGS_pdt_parentheses.empty()) {
48 LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
49 return 1;
50 }
51
52 vector<pair<int64, int64> > parens, rparens;
53 fst::ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false);
54
55 s::VectorFstClass ofst(ifst->ArcType());
56 s::PdtReverse(*ifst, parens, &ofst);
57
58 ofst.Write(out_name);
59
60 return 0;
61 }
62