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