• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 //
14 // Copyright 2005-2010 Google, Inc.
15 // Author: jpr@google.com (Jake Ratkiewicz)
16 
17 // Definitions of 'scriptable' versions of pdt operations, that is,
18 // those that can be called with FstClass-type arguments.
19 
20 // See comments in nlp/fst/script/script-impl.h for how the registration
21 // mechanism allows these to work with various arc types.
22 
23 #include <vector>
24 using std::vector;
25 #include <utility>
26 using std::pair; using std::make_pair;
27 
28 
29 #include <fst/extensions/pdt/compose.h>
30 #include <fst/extensions/pdt/expand.h>
31 #include <fst/extensions/pdt/pdtscript.h>
32 #include <fst/extensions/pdt/replace.h>
33 #include <fst/extensions/pdt/reverse.h>
34 #include <fst/extensions/pdt/shortest-path.h>
35 #include <fst/script/script-impl.h>
36 
37 namespace fst {
38 namespace script {
39 
PdtCompose(const FstClass & ifst1,const FstClass & ifst2,const vector<pair<int64,int64>> & parens,MutableFstClass * ofst,const ComposeOptions & copts,bool left_pdt)40 void PdtCompose(const FstClass &ifst1,
41                 const FstClass &ifst2,
42                 const vector<pair<int64, int64> > &parens,
43                 MutableFstClass *ofst,
44                 const ComposeOptions &copts,
45                 bool left_pdt) {
46   if (!ArcTypesMatch(ifst1, ifst2, "PdtCompose") ||
47       !ArcTypesMatch(ifst1, *ofst, "PdtCompose")) return;
48 
49   PdtComposeArgs args(ifst1, ifst2, parens, ofst, copts, left_pdt);
50 
51   Apply<Operation<PdtComposeArgs> >("PdtCompose", ifst1.ArcType(), &args);
52 }
53 
PdtExpand(const FstClass & ifst,const vector<pair<int64,int64>> & parens,MutableFstClass * ofst,const PdtExpandOptions & opts)54 void PdtExpand(const FstClass &ifst,
55                const vector<pair<int64, int64> > &parens,
56                MutableFstClass *ofst, const PdtExpandOptions &opts) {
57   PdtExpandArgs args(ifst, parens, ofst, opts);
58 
59   Apply<Operation<PdtExpandArgs> >("PdtExpand", ifst.ArcType(), &args);
60 }
61 
PdtExpand(const FstClass & ifst,const vector<pair<int64,int64>> & parens,MutableFstClass * ofst,bool connect)62 void PdtExpand(const FstClass &ifst,
63                const vector<pair<int64, int64> > &parens,
64                MutableFstClass *ofst, bool connect) {
65   PdtExpand(ifst, parens, ofst, PdtExpandOptions(connect));
66 }
67 
PdtReplace(const vector<pair<int64,const FstClass * >> & fst_tuples,MutableFstClass * ofst,vector<pair<int64,int64>> * parens,const int64 & root)68 void PdtReplace(const vector<pair<int64, const FstClass*> > &fst_tuples,
69                 MutableFstClass *ofst,
70                 vector<pair<int64, int64> > *parens,
71                 const int64 &root) {
72   for (unsigned i = 0; i < fst_tuples.size() - 1; ++i) {
73     if (!ArcTypesMatch(*(fst_tuples[i].second),
74                        *(fst_tuples[i+1].second), "PdtReplace")) return;
75   }
76 
77   if (!ArcTypesMatch((*fst_tuples[0].second), *ofst, "PdtReplace")) return;
78 
79   PdtReplaceArgs args(fst_tuples, ofst, parens, root);
80 
81   Apply<Operation<PdtReplaceArgs> >("PdtReplace", ofst->ArcType(), &args);
82 }
83 
PdtReverse(const FstClass & ifst,const vector<pair<int64,int64>> & parens,MutableFstClass * ofst)84 void PdtReverse(const FstClass &ifst,
85                 const vector<pair<int64, int64> > &parens,
86                 MutableFstClass *ofst) {
87   PdtReverseArgs args(ifst, parens, ofst);
88 
89   Apply<Operation<PdtReverseArgs> >("PdtReverse", ifst.ArcType(), &args);
90 }
91 
PdtShortestPath(const FstClass & ifst,const vector<pair<int64,int64>> & parens,MutableFstClass * ofst,const PdtShortestPathOptions & opts)92 void PdtShortestPath(const FstClass &ifst,
93                      const vector<pair<int64, int64> > &parens,
94                      MutableFstClass *ofst,
95                      const PdtShortestPathOptions &opts) {
96   PdtShortestPathArgs args(ifst, parens, ofst, opts);
97 
98   Apply<Operation<PdtShortestPathArgs> >("PdtShortestPath",
99                                          ifst.ArcType(), &args);
100 }
101 
PrintPdtInfo(const FstClass & ifst,const vector<pair<int64,int64>> & parens)102 void PrintPdtInfo(const FstClass &ifst,
103                   const vector<pair<int64, int64> > &parens) {
104   PrintPdtInfoArgs args(ifst, parens);
105   Apply<Operation<PrintPdtInfoArgs> >("PrintPdtInfo", ifst.ArcType(), &args);
106 }
107 
108 // Register operations for common arc types.
109 
110 REGISTER_FST_PDT_OPERATIONS(StdArc);
111 REGISTER_FST_PDT_OPERATIONS(LogArc);
112 REGISTER_FST_PDT_OPERATIONS(Log64Arc);
113 
114 }  // namespace script
115 }  // namespace fst
116