FST  openfst-1.8.3 OpenFst Library
loglinear-apply.h
Go to the documentation of this file.
2 //
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
8 //
9 // Unless required by applicable law or agreed to in writing, software
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17
18 #ifndef FST_EXTENSIONS_LINEAR_LOGLINEAR_APPLY_H_
19 #define FST_EXTENSIONS_LINEAR_LOGLINEAR_APPLY_H_
20
21 #include <fst/compat.h>
22 #include <fst/arc-map.h>
23 #include <fst/arc.h>
24 #include <fst/arcsort.h>
25 #include <fst/compose.h>
26 #include <fst/determinize.h>
27 #include <fst/float-weight.h>
28 #include <fst/fst.h>
29 #include <fst/minimize.h>
30 #include <fst/mutable-fst.h>
31 #include <fst/project.h>
32 #include <fst/rmepsilon.h>
33 #include <fst/vector-fst.h>
34
35 namespace fst {
36
37 // Applies a FST model as a discriminative model to weighted input
38 // `ifst`. `A` is an arc type with tropical weight of all the
39 // input/output FSTs.
40 //
41 // In general, consider `ifst` an unnormalized probability
42 // distribution between its input X and output Y, P(X, Y); and `lfst`
43 // a group of unnormalized probability distributions of all its output
44 // Z for every input Y, Q(Z|Y). `normalize` controls whether Q is
45 // normalized for every Y before chaining with P(X, Y). I.e., for a
46 // path (X, Y, Z) in `ofst` (where Y is hidden),
47 //
48 // - When `normalize` is true, its weight is P(X, Y) Q(Z|Y) / sum_z Q(z|Y);
49 // - When `normalize` is false, its weight is P(X, Y) Q(Z|Y).
50 template <class A>
51 void LogLinearApply(const Fst<A> &ifst, const Fst<A> &lfst, MutableFst<A> *ofst,
52  bool normalize = true) {
53  LogLinearApply<A, LogArc>(ifst, lfst, ofst, normalize);
54 }
55
56 // This version gives finer control over the arc type (`B`) to be used
57 // in normalization. `B` is an arc type with log weight (e.g. `LogArc`
58 // or `Log64Arc`).
59 template <class A, class B>
60 void LogLinearApply(const Fst<A> &ifst, const Fst<A> &lfst, MutableFst<A> *ofst,
61  bool normalize = true) {
62  if (normalize) {
63  VectorFst<A> unnormalized_ofst, rescored_ifsa;
64  Compose(ifst, lfst, &unnormalized_ofst);
65  {
66  VectorFst<A> tropical_ifsa(unnormalized_ofst);
67  Project(&tropical_ifsa, ProjectType::INPUT);
68  {
69  VectorFst<B> minimal_log_ifsa;
70  {
71  VectorFst<B> log_ifsa;
72  ArcMap(tropical_ifsa, &log_ifsa, WeightConvertMapper<A, B>());
73  RmEpsilon(&log_ifsa);
74  Determinize(log_ifsa, &minimal_log_ifsa);
75  }
76  Minimize(&minimal_log_ifsa);
77  ArcMap(&minimal_log_ifsa, InvertWeightMapper<B>());
78  ArcMap(minimal_log_ifsa, &tropical_ifsa, WeightConvertMapper<B, A>());
79  }
80  ArcSort(&tropical_ifsa, OLabelCompare<A>());
81  Compose(tropical_ifsa, ifst, &rescored_ifsa);
82  }
83  ArcSort(&rescored_ifsa, OLabelCompare<A>());
84  Compose(rescored_ifsa, unnormalized_ofst, ofst);
85  } else {
86  Compose(ifst, lfst, ofst);
87  }
88 }
89
90 } // namespace fst
91
92 #endif // FST_EXTENSIONS_LINEAR_LOGLINEAR_APPLY_H_
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:120
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:217
void Determinize(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, const DeterminizeOptions< Arc > &opts=DeterminizeOptions< Arc >())
Definition: determinize.h:1095
void LogLinearApply(const Fst< A > &ifst, const Fst< A > &lfst, MutableFst< A > *ofst, bool normalize=true)
void ArcSort(MutableFst< Arc > *fst, Compare comp)
Definition: arcsort.h:109
void Compose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const ComposeOptions &opts=ComposeOptions())
Definition: compose.h:1005
void Project(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, ProjectType project_type)
Definition: project.h:89
void Minimize(MutableFst< Arc > *fst, MutableFst< Arc > *sfst=nullptr, float delta=kShortestDelta, bool allow_nondet=false)
Definition: minimize.h:512