1 // pdtexpand.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 // Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18 //
19 // \file
20 // Expands a (bounded-stack) PDT as an FST.
21 //
22
23 #include <fst/extensions/pdt/pdtscript.h>
24 #include <fst/util.h>
25
26 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs.");
27 DEFINE_bool(connect, true, "Trim output");
28 DEFINE_bool(keep_parentheses, false, "Keep PDT parentheses in result.");
29 DEFINE_string(weight, "", "Weight threshold");
30
31
main(int argc,char ** argv)32 int main(int argc, char **argv) {
33 namespace s = fst::script;
34
35 string usage = "Expand a (bounded-stack) PDT as an FST.\n\n Usage: ";
36 usage += argv[0];
37 usage += " in.pdt [out.fst]\n";
38
39 std::set_new_handler(FailedNewHandler);
40 SET_FLAGS(usage.c_str(), &argc, &argv, true);
41 if (argc > 3) {
42 ShowUsage();
43 return 1;
44 }
45
46 string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
47 string out_name = argc > 2 ? argv[2] : "";
48
49 s::FstClass *ifst = s::FstClass::Read(in_name);
50 if (!ifst) return 1;
51
52 if (FLAGS_pdt_parentheses.empty()) {
53 LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
54 return 1;
55 }
56
57 vector<pair<int64, int64> > parens;
58 fst::ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false);
59
60 s::WeightClass weight_threshold = FLAGS_weight.empty() ?
61 s::WeightClass::Zero() :
62 s::WeightClass(ifst->WeightType(), FLAGS_weight);
63
64 s::VectorFstClass ofst(ifst->ArcType());
65 s::PdtExpand(*ifst, parens, &ofst, s::PdtExpandOptions(
66 FLAGS_connect, FLAGS_keep_parentheses, weight_threshold));
67
68 ofst.Write(out_name);
69
70 return 0;
71 }
72