FST  openfst-1.8.3
OpenFst Library
map-reduce.h
Go to the documentation of this file.
1 // Copyright 2005-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 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Map helper for FAR processing.
19 
20 #ifndef FST_EXTENSIONS_FAR_MAP_REDUCE_H_
21 #define FST_EXTENSIONS_FAR_MAP_REDUCE_H_
22 
23 #include <string>
24 
25 #include <fst/log.h>
26 #include <fst/extensions/far/far.h>
27 #include <fst/arc.h>
28 #include <fst/fst.h>
29 #include <string_view>
30 
31 namespace fst {
32 namespace internal {
33 
34 // This function applies a functor to each FST in a FAR reader, writing the
35 // resulting FST to a FAR writer. The functor must support the following
36 // interface:
37 //
38 // std::unique_ptr<Fst<Arc>> operator()(std::string_view key,
39 // const Fst<Arc> *fst);
40 //
41 // The functor signals an error by returning a null pointer.
42 //
43 // One can imagine more expressive variants of this: e.g., one which also
44 // transforms the key, but this can be added as needed.
45 //
46 // The caller is responsible for rewinding the reader afterwards, if desired,
47 // and for checking the error bit of the reader and writer.
48 template <class Arc, class Functor>
49 void Map(FarReader<Arc> &reader, FarWriter<Arc> &writer, Functor functor) {
50  for (; !reader.Done(); reader.Next()) {
51  const auto &key = reader.GetKey();
52  const auto fst = functor(key, reader.GetFst());
53  if (!fst) return;
54  writer.Add(key, *fst);
55  }
56 }
57 
58 // This function applies a boolean functor to pairs of FSTs passed via FAR
59 // readers, returning false if any of the keys do not match or if any of the
60 // functor calls are false. The functor must support the following interface:
61 //
62 // bool operator()(std::string_view key, const Fst<Arc> *fst1,
63 // const Fst<Arc> *fst2);
64 //
65 // One can imagine more expressive variants of this, but these can be added as
66 // needed.
67 //
68 // The caller is responsible for rewinding the readers afterwards, if desired,
69 // and for checking the error bits of the readers.
70 template <class Arc, class Functor>
71 bool MapAllReduce(FarReader<Arc> &reader1, FarReader<Arc> &reader2,
72  Functor functor, std::string_view begin_key = "",
73  std::string_view end_key = "") {
74  if (!begin_key.empty()) {
75  const bool find_begin1 = reader1.Find(begin_key);
76  const bool find_begin2 = reader2.Find(begin_key);
77  if (!find_begin1 || !find_begin2) {
78  const bool ret = !find_begin1 && !find_begin2;
79  if (!ret) {
80  LOG(ERROR) << "MapAllReduce: Key " << begin_key << " missing from "
81  << (find_begin1 ? "second" : "first") << " FAR";
82  }
83  return ret;
84  }
85  }
86  for (; !reader1.Done() && !reader2.Done(); reader1.Next(), reader2.Next()) {
87  const auto &key1 = reader1.GetKey();
88  const auto &key2 = reader2.GetKey();
89  if (!end_key.empty() && end_key < key1 && end_key < key2) {
90  return true;
91  }
92  if (key1 != key2) {
93  LOG(ERROR) << "MapAllReduce: Mismatched keys " << key1 << " and " << key2;
94  return false;
95  }
96  if (!functor(key1, reader1.GetFst(), reader2.GetFst())) return false;
97  }
98  if (reader1.Done() && !reader2.Done()) {
99  LOG(ERROR) << "MapAllReduce: Key " << reader2.GetKey()
100  << " missing from first FAR";
101  return false;
102  } else if (reader2.Done() && !reader1.Done()) {
103  LOG(ERROR) << "MapAllReduce: Key " << reader1.GetKey()
104  << " missing from second FAR";
105  return false;
106  }
107  return true;
108 }
109 
110 } // namespace internal
111 } // namespace fst
112 
113 #endif // FST_EXTENSIONS_FAR_MAP_REDUCE_H_
virtual const std::string & GetKey() const =0
bool MapAllReduce(FarReader< Arc > &reader1, FarReader< Arc > &reader2, Functor functor, std::string_view begin_key="", std::string_view end_key="")
Definition: map-reduce.h:71
virtual bool Done() const =0
#define LOG(type)
Definition: log.h:53
virtual const Fst< Arc > * GetFst() const =0
void Map(FarReader< Arc > &reader, FarWriter< Arc > &writer, Functor functor)
Definition: map-reduce.h:49
virtual void Add(std::string_view key, const Fst< Arc > &fst)=0
virtual void Next()=0
virtual bool Find(std::string_view key)=0