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>
24using std::vector;
25#include <utility>
26using 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
37namespace fst {
38namespace script {
39
40void 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
54void 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
62void 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
68void 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
84void 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
92void 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
102void 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
110REGISTER_FST_PDT_OPERATIONS(StdArc);
111REGISTER_FST_PDT_OPERATIONS(LogArc);
112REGISTER_FST_PDT_OPERATIONS(Log64Arc);
113
114}  // namespace script
115}  // namespace fst
116