• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // pdtreplace.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 
19 #include <utility>
20 using std::pair; using std::make_pair;
21 #include <vector>
22 using std::vector;
23 #include <fst/extensions/pdt/pdtscript.h>
24 #include <fst/vector-fst.h>
25 #include <fst/util.h>
26 
27 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs.");
28 
main(int argc,char ** argv)29 int main(int argc, char **argv) {
30   namespace s = fst::script;
31 
32   string usage = "Recursively replace Fst arcs with other Fst(s).\n";
33   usage += " Usage: ";
34   usage += argv[0];
35   usage += " root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
36 
37   std::set_new_handler(FailedNewHandler);
38   SetFlags(usage.c_str(), &argc, &argv, true);
39   if (argc < 4) {
40     ShowUsage();
41     return 1;
42   }
43 
44   string in_fname = argv[1];
45   string out_fname = argc % 2 == 0 ? argv[argc - 1] : "";
46 
47   s::FstClass *ifst = s::FstClass::Read(in_fname);
48   if (!ifst) return 1;
49 
50   typedef int64 Label;
51   typedef pair<Label, const s::FstClass* > FstTuple;
52   vector<FstTuple> fst_tuples;
53   Label root = atoll(argv[2]);
54   fst_tuples.push_back(make_pair(root, ifst));
55 
56   for (size_t i = 3; i < argc - 1; i += 2) {
57     ifst = s::FstClass::Read(argv[i]);
58     if (!ifst) return 1;
59     Label lab = atoll(argv[i + 1]);
60     fst_tuples.push_back(make_pair(lab, ifst));
61   }
62 
63   s::VectorFstClass ofst(ifst->ArcType());
64   vector<pair<int64, int64> > parens;
65   s::PdtReplace(fst_tuples, &ofst, &parens, root);
66 
67   if (!FLAGS_pdt_parentheses.empty())
68     fst::WriteLabelPairs(FLAGS_pdt_parentheses, parens);
69 
70   ofst.Write(out_fname);
71 
72   return 0;
73 }
74