GRM-SFST  sfst-1.2.1
OpenGrm SFst Library
intersect.h
Go to the documentation of this file.
1 // Copyright 2018-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
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 // Algorithm to intersect two canonical stochastic FSAs.
15 // The second FSA should be input-epsilon free (when phi_label != 0).
16 
17 #ifndef NLP_GRM2_SFST_INTERSECT_H_
18 #define NLP_GRM2_SFST_INTERSECT_H_
19 
20 #include <sys/types.h>
21 
22 #include <fst/compose.h>
23 #include <fst/fst.h>
24 #include <fst/mutable-fst.h>
25 #include <sfst/phi2matcher.h>
26 #include <sfst/trim.h>
27 
28 namespace sfst {
29 
30 // Intersects two canonical stochastic FSAs.
31 // The second FSA must be input-epsilon free (when phi_label != 0).
32 template <class Arc>
33 bool Intersect(const fst::Fst<Arc> &ifst1,
34  const fst::Fst<Arc> &ifst2,
35  fst::MutableFst<Arc> *ofst,
36  typename Arc::Label phi_label = fst::kNoLabel,
37  bool trim = true,
38  TrimType trim_type = TRIM_NEEDED_FINAL) {
39  namespace f = fst;
41  using PF = Phi2Filter<PM>;
42  f::ComposeFstOptions<Arc, PM, PF> copts;
43  copts.gc_limit = 0;
44  copts.matcher1 = new PM(ifst1, f::MATCH_OUTPUT, phi_label);
45  copts.matcher2 = new PM(ifst2, f::MATCH_INPUT, phi_label);
46  *ofst = f::ComposeFst<Arc>(ifst1, ifst2, copts);
47 
48  if (trim && !Trim(ofst, phi_label, trim_type))
49  return false;
50  return true;
51 }
52 
53 } // namespace sfst
54 
55 #endif // NLP_GRM2_SFST_INTERSECT_H_
Definition: perplexity.h:32
bool Intersect(const fst::Fst< Arc > &ifst1, const fst::Fst< Arc > &ifst2, fst::MutableFst< Arc > *ofst, typename Arc::Label phi_label=fst::kNoLabel, bool trim=true, TrimType trim_type=TRIM_NEEDED_FINAL)
Definition: intersect.h:33
Definition: sfstinfo.cc:39
bool Trim(fst::MutableFst< Arc > *fst, typename Arc::Label phi_label=fst::kNoLabel, TrimType trim_type=TRIM_NEEDED_FINAL)
Definition: trim.h:707
TrimType
Definition: trim.h:431