FST  openfst-1.8.3
OpenFst Library
convert.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 #ifndef FST_EXTENSIONS_FAR_CONVERT_H_
16 #define FST_EXTENSIONS_FAR_CONVERT_H_
17 
18 #include <fst/log.h>
19 #include <fst/extensions/far/far.h>
22 #include <fst/fst.h>
23 #include <fst/register.h>
24 #include <fst/util.h>
25 #include <string_view>
26 
27 namespace fst {
28 
29 template <class Arc>
30 void Convert(FarReader<Arc> &reader, FarWriter<Arc> &writer,
31  std::string_view fst_type) {
32  internal::Map(reader, writer,
33  [&fst_type](std::string_view key, const Fst<Arc> *ifst) {
34  if (fst_type.empty() || ifst->Type() == fst_type) {
35  return fst::WrapUnique(ifst->Copy());
36  }
37  auto ofst = fst::WrapUnique(Convert(*ifst, fst_type));
38  if (!ofst) {
39  FSTERROR() << "FarConvert: Cannot convert FST with key "
40  << key << " to " << fst_type;
41  }
42  return ofst;
43  });
44 }
45 
46 } // namespace fst
47 
48 #endif // FST_EXTENSIONS_FAR_CONVERT_H_
void Convert(FarReader< Arc > &reader, FarWriter< Arc > &writer, std::string_view fst_type)
Definition: convert.h:30
std::unique_ptr< T > WrapUnique(T *ptr)
Definition: compat.h:132
void Map(FarReader< Arc > &reader, FarWriter< Arc > &writer, Functor functor)
Definition: map-reduce.h:49
#define FSTERROR()
Definition: util.h:56
virtual const std::string & Type() const =0
virtual Fst * Copy(bool safe=false) const =0