BaumWelch  baumwelch-0.3.8
OpenGrm-BaumWelch library
a-star.h
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // Copyright 2017 and onwards Google, Inc.
14 
15 #ifndef NLP_GRM2_BAUMWELCH_A_STAR_H_
16 #define NLP_GRM2_BAUMWELCH_A_STAR_H_
17 
18 #include <vector>
19 
20 #include <fst/arc-map.h>
21 #include <fst/arcfilter.h>
22 #include <fst/determinize.h>
23 #include <fst/fst-decl.h>
24 #include <fst/queue.h>
25 #include <fst/shortest-distance.h>
26 #include <fst/shortest-path.h>
27 #include <fst/weight.h>
28 #include <baumwelch/util.h>
29 
30 namespace fst {
31 
32 // Computes the shortest string over a semiring without the path property:
33 //
34 // * Compute the NFA beta.
35 // * Lazily determinize and compute the DFA beta.
36 // * Lazily cast into the tropical semiring.
37 // * Compute the shortest path using the DFA beta as an A* heuristic.
38 // * Cast back into the input semiring.
39 //
40 // Due to limitations of determinization, this only works for acceptors.
41 template <class Arc>
42 void AStarSingleShortestString(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
43  float delta = kShortestDelta) {
44  static_assert(!IsPath<typename Arc::Weight>::value,
45  "This procedure is only valid for non-path semirings");
46  using StateId = typename Arc::StateId;
47  using Weight = typename Arc::Weight;
48  using PathWeight = TropicalWeightTpl<typename Weight::ValueType>;
49  using PathArc = ArcTpl<PathWeight>;
50  std::vector<Weight> nfa_beta;
51  ShortestDistance(ifst, &nfa_beta, /*reverse=*/true);
52  std::vector<Weight> dfa_beta;
53  static const DeterminizeFstOptions<Arc> dopts;
54  const DeterminizeFst<Arc> dfa(ifst, &nfa_beta, &dfa_beta, dopts);
55  VectorFst<PathArc> path_shortest;
56  {
57  using MyEstimate = NaturalAStarEstimate<StateId, PathWeight>;
58  using MyQueue = NaturalAStarQueue<StateId, PathWeight, MyEstimate>;
59  using MyArcFilter = AnyArcFilter<PathArc>;
60  using MyShortestPathOptions =
61  ShortestPathOptions<PathArc, MyQueue, MyArcFilter>;
62  using ToPathMapper = WeightConvertMapper<Arc, PathArc>;
63  static constexpr ToPathMapper to_mapper;
64  const ArcMapFst path_dfa(dfa, to_mapper);
65  const MyEstimate estimate(
66  reinterpret_cast<const std::vector<PathWeight> &>(dfa_beta));
67  std::vector<PathWeight> dfa_alpha;
68  MyQueue queue(dfa_alpha, estimate);
69  static constexpr MyArcFilter arc_filter;
70  const MyShortestPathOptions sopts(
71  &queue, arc_filter,
72  /*nshortest=*/1, // 1-best.
73  /*unique=*/false, // Lattice is already deterministic.
74  /*has_distance=*/true, // Precomputed..
75  /*delta=*/delta,
76  /*first_path=*/true); // Heuristic is admissible.
77  ShortestPath(path_dfa, &path_shortest, &dfa_alpha, sopts);
78  VLOG(1) << internal::ExploredStates(dfa_alpha) << " states explored";
79  }
80  using FromPathMapper = WeightConvertMapper<PathArc, Arc>;
81  static constexpr FromPathMapper from_mapper;
82  ArcMap(path_shortest, ofst, from_mapper);
83 }
84 
85 } // namespace fst
86 
87 #endif // NLP_GRM2_BAUMWELCH_A_STAR_H_
88 
Definition: a-star.h:30
void AStarSingleShortestString(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, float delta=kShortestDelta)
Definition: a-star.h:42
size_t ExploredStates(const std::vector< Weight > &distance)
Definition: util.h:30