FST  openfst-1.7.1
OpenFst Library
loglinear-apply.h
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 #ifndef FST_EXTENSIONS_LINEAR_LOGLINEAR_APPLY_H_
5 #define FST_EXTENSIONS_LINEAR_LOGLINEAR_APPLY_H_
6 
7 #include <fst/compat.h>
8 #include <fst/arc.h>
9 #include <fst/arc-map.h>
10 #include <fst/compose.h>
11 #include <fst/determinize.h>
12 #include <fst/float-weight.h>
13 #include <fst/fst.h>
14 #include <fst/minimize.h>
15 #include <fst/mutable-fst.h>
16 #include <fst/project.h>
17 #include <fst/rmepsilon.h>
18 #include <fst/vector-fst.h>
19 
20 namespace fst {
21 
22 // Applies a FST model as a discriminative model to weighted input
23 // `ifst`. `A` is an arc type with tropical weight of all the
24 // input/output FSTs.
25 //
26 // In general, consider `ifst` an unnormalized probability
27 // distribution between its input X and output Y, P(X, Y); and `lfst`
28 // a group of unnormalized probability distributions of all its output
29 // Z for every input Y, Q(Z|Y). `normalize` controls whether Q is
30 // normalized for every Y before chaining with P(X, Y). I.e., for a
31 // path (X, Y, Z) in `ofst` (where Y is hidden),
32 //
33 // - When `normalize` is true, its weight is P(X, Y) Q(Z|Y) / sum_z Q(z|Y);
34 // - When `normalize` is false, its weight is P(X, Y) Q(Z|Y).
35 template <class A>
36 void LogLinearApply(const Fst<A> &ifst, const Fst<A> &lfst, MutableFst<A> *ofst,
37  bool normalize = true) {
38  LogLinearApply<A, LogArc>(ifst, lfst, ofst, normalize);
39 }
40 
41 // This version gives finer control over the arc type (`B`) to be used
42 // in normalization. `B` is an arc type with log weight (e.g. `LogArc`
43 // or `Log64Arc`).
44 template <class A, class B>
45 void LogLinearApply(const Fst<A> &ifst, const Fst<A> &lfst, MutableFst<A> *ofst,
46  bool normalize = true) {
47  if (normalize) {
48  VectorFst<A> unnormalized_ofst, rescored_ifsa;
49  Compose(ifst, lfst, &unnormalized_ofst);
50  {
51  VectorFst<A> tropical_ifsa(unnormalized_ofst);
52  Project(&tropical_ifsa, PROJECT_INPUT);
53  {
54  VectorFst<B> minimal_log_ifsa;
55  {
56  VectorFst<B> log_ifsa;
57  ArcMap(tropical_ifsa, &log_ifsa, WeightConvertMapper<A, B>());
58  RmEpsilon(&log_ifsa);
59  Determinize(log_ifsa, &minimal_log_ifsa);
60  }
61  Minimize(&minimal_log_ifsa);
62  ArcMap(&minimal_log_ifsa, InvertWeightMapper<B>());
63  ArcMap(minimal_log_ifsa, &tropical_ifsa, WeightConvertMapper<B, A>());
64  }
65  ArcSort(&tropical_ifsa, OLabelCompare<A>());
66  Compose(tropical_ifsa, ifst, &rescored_ifsa);
67  }
68  ArcSort(&rescored_ifsa, OLabelCompare<A>());
69  Compose(rescored_ifsa, unnormalized_ofst, ofst);
70  } else {
71  Compose(ifst, lfst, ofst);
72  }
73 }
74 
75 } // namespace fst
76 
77 #endif // FST_EXTENSIONS_LINEAR_LOGLINEAR_APPLY_H_
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:94
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:192
void Determinize(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, const DeterminizeOptions< Arc > &opts=DeterminizeOptions< Arc >())
Definition: determinize.h:1061
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:87
void Compose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const ComposeOptions &opts=ComposeOptions())
Definition: compose.h:980
void Project(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, ProjectType project_type)
Definition: project.h:65
void Minimize(MutableFst< Arc > *fst, MutableFst< Arc > *sfst=nullptr, float delta=kShortestDelta, bool allow_nondet=false)
Definition: minimize.h:495