FST  openfst-1.7.2
OpenFst Library
pdtscript.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Definitions of 'scriptable' versions of pdt operations, that is,
5 // those that can be called with FstClass-type arguments.
6 //
7 // See comments in nlp/fst/script/script-impl.h for how the registration
8 // mechanism allows these to work with various arc types.
9 
10 #include <string>
11 #include <vector>
12 
19 #include <fst/script/script-impl.h>
20 
21 namespace fst {
22 namespace script {
23 
24 void PdtCompose(const FstClass &ifst1, const FstClass &ifst2,
25  const std::vector<LabelPair> &parens,
26  MutableFstClass *ofst, const PdtComposeOptions &copts,
27  bool left_pdt) {
28  if (!internal::ArcTypesMatch(ifst1, ifst2, "PdtCompose") ||
29  !internal::ArcTypesMatch(ifst1, *ofst, "PdtCompose"))
30  return;
31  PdtComposeArgs args(ifst1, ifst2, parens, ofst, copts, left_pdt);
32  Apply<Operation<PdtComposeArgs>>("PdtCompose", ifst1.ArcType(), &args);
33 }
34 
35 void PdtExpand(const FstClass &ifst,
36  const std::vector<LabelPair> &parens,
37  MutableFstClass *ofst, const PdtExpandOptions &opts) {
38  PdtExpandArgs args(ifst, parens, ofst, opts);
39  Apply<Operation<PdtExpandArgs>>("PdtExpand", ifst.ArcType(), &args);
40 }
41 
42 void PdtExpand(const FstClass &ifst,
43  const std::vector<std::pair<int64, int64>> &parens,
44  MutableFstClass *ofst, bool connect, bool keep_parentheses,
45  const WeightClass &weight_threshold) {
46  PdtExpand(ifst, parens, ofst,
47  PdtExpandOptions(connect, keep_parentheses, weight_threshold));
48 }
49 
50 void PdtReplace(const std::vector<LabelFstClassPair> &pairs,
51  MutableFstClass *ofst, std::vector<LabelPair> *parens,
52  int64 root, PdtParserType parser_type, int64 start_paren_labels,
53  const string &left_paren_prefix,
54  const string &right_paren_prefix) {
55  for (size_t i = 1; i < pairs.size(); ++i) {
56  if (!internal::ArcTypesMatch(*pairs[i - 1].second, *pairs[i].second,
57  "PdtReplace"))
58  return;
59  }
60  if (!internal::ArcTypesMatch(*pairs[0].second, *ofst, "PdtReplace")) return;
61  PdtReplaceArgs args(pairs, ofst, parens, root, parser_type,
62  start_paren_labels, left_paren_prefix,
63  right_paren_prefix);
64  Apply<Operation<PdtReplaceArgs>>("PdtReplace", ofst->ArcType(), &args);
65 }
66 
67 void PdtReverse(const FstClass &ifst,
68  const std::vector<LabelPair> &parens,
69  MutableFstClass *ofst) {
70  PdtReverseArgs args(ifst, parens, ofst);
71  Apply<Operation<PdtReverseArgs>>("PdtReverse", ifst.ArcType(), &args);
72 }
73 
74 void PdtShortestPath(const FstClass &ifst,
75  const std::vector<LabelPair> &parens,
76  MutableFstClass *ofst,
77  const PdtShortestPathOptions &opts) {
78  PdtShortestPathArgs args(ifst, parens, ofst, opts);
79  Apply<Operation<PdtShortestPathArgs>>("PdtShortestPath", ifst.ArcType(),
80  &args);
81 }
82 
83 void PrintPdtInfo(const FstClass &ifst,
84  const std::vector<LabelPair> &parens) {
85  PrintPdtInfoArgs args(ifst, parens);
86  Apply<Operation<PrintPdtInfoArgs>>("PrintPdtInfo", ifst.ArcType(), &args);
87 }
88 
89 // Register operations for common arc types.
90 
94 
95 } // namespace script
96 } // namespace fst
void PdtReplace(const std::vector< LabelFstClassPair > &pairs, MutableFstClass *ofst, std::vector< LabelPair > *parens, int64 root, PdtParserType parser_type, int64 start_paren_labels, const string &left_paren_prefix, const string &right_paren_prefix)
Definition: pdtscript.cc:50
std::tuple< const FstClass &, const FstClass &, const std::vector< LabelPair > &, MutableFstClass *, const PdtComposeOptions &, bool > PdtComposeArgs
Definition: pdtscript.h:35
std::tuple< const std::vector< LabelFstClassPair > &, MutableFstClass *, std::vector< LabelPair > *, int64, PdtParserType, int64, const string &, const string & > PdtReplaceArgs
Definition: pdtscript.h:102
std::tuple< const FstClass &, const std::vector< LabelPair > &, MutableFstClass *, const PdtShortestPathOptions & > PdtShortestPathArgs
Definition: pdtscript.h:168
void PdtReverse(const FstClass &ifst, const std::vector< LabelPair > &parens, MutableFstClass *ofst)
Definition: pdtscript.cc:67
void PdtCompose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< LabelPair > &parens, MutableFstClass *ofst, const PdtComposeOptions &copts, bool left_pdt)
Definition: pdtscript.cc:24
const string & ArcType() const final
Definition: fst-class.h:290
REGISTER_FST_PDT_OPERATIONS(StdArc)
bool ArcTypesMatch(const M &m, const N &n, const string &op_name)
Definition: script-impl.h:176
int64_t int64
Definition: types.h:27
void PdtExpand(const FstClass &ifst, const std::vector< LabelPair > &parens, MutableFstClass *ofst, const PdtExpandOptions &opts)
Definition: pdtscript.cc:35
std::tuple< const FstClass &, const std::vector< LabelPair > &, MutableFstClass * > PdtReverseArgs
Definition: pdtscript.h:135
void PrintPdtInfo(const FstClass &ifst, const std::vector< LabelPair > &parens)
Definition: pdtscript.cc:83
void PdtShortestPath(const FstClass &ifst, const std::vector< LabelPair > &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
Definition: pdtscript.cc:74
PdtParserType
Definition: replace.h:43
std::tuple< const FstClass &, const std::vector< LabelPair > &, MutableFstClass *, const PdtExpandOptions & > PdtExpandArgs
Definition: pdtscript.h:72
std::pair< const FstClass &, const std::vector< LabelPair > & > PrintPdtInfoArgs
Definition: pdtscript.h:216