NGram  ngram-1.3.15
OpenGrm-NGram library
ngram-context-merge.h
Go to the documentation of this file.
1 // Copyright 2005-2013 Brian Roark
2 // Copyright 2005-2020 Google LLC
3 //
4 // Licensed under the Apache License, Version 2.0 (the 'License');
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an 'AS IS' BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // NGram model class for merging specified contexts from one FST into another.
17 
18 #ifndef NGRAM_NGRAM_CONTEXT_MERGE_H_
19 #define NGRAM_NGRAM_CONTEXT_MERGE_H_
20 
21 #include <memory>
22 #include <vector>
23 
24 #include <fst/arc.h>
25 #include <ngram/ngram-context.h>
26 #include <ngram/ngram-merge.h>
27 #include <ngram/ngram-model.h>
28 #include <ngram/util.h>
29 
30 namespace ngram {
31 
32 class NGramContextMerge : public NGramMerge<fst::StdArc> {
33  public:
34  typedef fst::StdArc::StateId StateId;
35  typedef fst::StdArc::Label Label;
36 
37  // Constructs an NGramContextMerge object consisting of ngram model to be
38  // merged.
39  // Ownership of FST is retained by the caller.
40  explicit NGramContextMerge(fst::StdMutableFst *infst1,
41  Label backoff_label = 0,
42  double norm_eps = kNormEps,
43  bool check_consistency = false)
44  : NGramMerge(infst1, backoff_label, norm_eps, true) {}
45 
46  // Perform context merger with n-gram model specified by the FST
47  // argument and 'context_pattern' string. These contexts taken from
48  // the second FST and added to the first FST, replacing any existing
49  // shared arcs. See 'ngram-context.h' for meaning of the context
50  // specification.
51  void MergeNGramModels(const fst::StdFst &infst2,
52  std::string_view context_pattern, bool norm = false) {
53  context_ =
54  std::make_unique<NGramExtendedContext>(context_pattern, HiOrder());
55  if (!NGramMerge<fst::StdArc>::MergeNGramModels(infst2, norm)) {
56  NGRAMERROR() << "Context merge failed";
58  }
59  }
60 
61  // Perform context merger with n-gram model specified by the FST argument
62  // and 'context_begin' and 'context_end' vectors. These contexts
63  // taken from the second FST and added to the first FST, replacing any
64  // existing shared arcs. See 'ngram-context.h' for meaning of the
65  // context specification.
66  void MergeNGramModels(const fst::StdFst &infst2,
67  const std::vector<Label> &context_begin,
68  const std::vector<Label> &context_end,
69  bool norm = false) {
70  context_ = std::make_unique<NGramExtendedContext>(context_begin,
71  context_end, HiOrder());
72  if (!NGramMerge<fst::StdArc>::MergeNGramModels(infst2, norm)) {
73  NGRAMERROR() << "Context merge failed";
75  }
76  }
77 
78  protected:
79  // Specifies resultant weight when combining a weight from each FST
80  Weight MergeWeights(StateId s1, StateId s2, Label label, Weight w1, Weight w2,
81  bool in_fst1, bool in_fst2) const override {
82  if (in_fst1 && in_fst2) {
83  // Takes weight from w2 if in both and ngram is strictly in context.
84  const std::vector<Label> &ngram = NGram2().StateNGram(s2);
85  return context_->HasContext(ngram, false) ? w2.Value() : w1.Value();
86  } else if (in_fst1) {
87  return w1.Value();
88  } else {
89  return w2.Value();
90  }
91  }
92 
93  // Specifies if unshared arcs/final weights between the two
94  // FSTs in a merge have a non-trivial merge. In particular, this
95  // means MergeWeights() changes the arc or final weights; any
96  // destination state changes are not relevant here. When false, more
97  // efficient merging may be performed. If the arc/final_weight
98  // comes from the first FST, then 'in_fst1' is true.
99  bool MergeUnshared(bool in_fst1) const override { return false; }
100 
101  private:
102  std::unique_ptr<NGramExtendedContext> context_;
103 };
104 
105 } // namespace ngram
106 
107 #endif // NGRAM_NGRAM_CONTEXT_MERGE_H_
fst::StdArc::Weight Weight
Definition: ngram-merge.h:41
const NGramModel< fst::StdArc > & NGram2() const
Definition: ngram-merge.h:143
const double kNormEps
Definition: ngram-model.h:37
fst::StdArc::StateId StateId
Weight MergeWeights(StateId s1, StateId s2, Label label, Weight w1, Weight w2, bool in_fst1, bool in_fst2) const override
NGramContextMerge(fst::StdMutableFst *infst1, Label backoff_label=0, double norm_eps=kNormEps, bool check_consistency=false)
const std::vector< Label > & StateNGram(StateId state) const
Definition: ngram-model.h:176
void MergeNGramModels(const fst::StdFst &infst2, const std::vector< Label > &context_begin, const std::vector< Label > &context_end, bool norm=false)
void MergeNGramModels(const fst::StdFst &infst2, std::string_view context_pattern, bool norm=false)
#define NGRAMERROR()
Definition: util.h:26
bool MergeUnshared(bool in_fst1) const override