FST  openfst-1.8.3
OpenFst Library
far-class.cc
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 
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <fst/log.h>
26 #include <fst/extensions/far/far.h>
28 #include <fst/arc.h>
29 #include <fst/error-weight.h>
30 #include <fst/script/script-impl.h>
31 #include <string_view>
32 
33 namespace fst {
34 namespace script {
35 
36 // FarReaderClass.
37 
38 std::unique_ptr<FarReaderClass> FarReaderClass::Open(
39  std::string_view source) {
40  const std::vector<std::string> sources{std::string(source)};
41  return FarReaderClass::Open(sources);
42 }
43 
44 std::unique_ptr<FarReaderClass> FarReaderClass::Open(
45  const std::vector<std::string> &sources) {
46  if (sources.empty()) {
47  LOG(ERROR) << "FarReaderClass::Open: No files specified";
48  return nullptr;
49  }
50  auto arc_type = LoadArcTypeFromFar(sources.front());
51  if (arc_type.empty()) {
52  LOG(ERROR) << "FarReaderClass::Open: File could not be opened: "
53  << sources.front();
54  return nullptr;
55  }
56  // TODO(jrosenstock): What if we have an empty FAR for the first one,
57  // then non-empty? We need to check all input FARs.
58  OpenFarReaderClassArgs args(sources);
59  args.retval = nullptr;
60  Apply<Operation<OpenFarReaderClassArgs>>("OpenFarReaderClass", arc_type,
61  &args);
62  return std::move(args.retval);
63 }
64 
69 
70 // FarWriterClass.
71 
72 std::unique_ptr<FarWriterClass> FarWriterClass::Create(
73  const std::string &source, const std::string &arc_type, FarType type) {
74  CreateFarWriterClassInnerArgs iargs(source, type);
75  CreateFarWriterClassArgs args(iargs);
76  args.retval = nullptr;
77  Apply<Operation<CreateFarWriterClassArgs>>("CreateFarWriterClass", arc_type,
78  &args);
79  return std::move(args.retval);
80 }
81 
88 
89 } // namespace script
90 } // namespace fst
#define LOG(type)
Definition: log.h:53
static std::unique_ptr< FarWriterClass > Create(const std::string &source, const std::string &arc_type, FarType type=FarType::DEFAULT)
Definition: far-class.cc:72
std::pair< const std::string &, FarType > CreateFarWriterClassInnerArgs
Definition: far-class.h:223
FarType
Definition: far.h:51
REGISTER_FST_OPERATION(OpenFarReaderClass, StdArc, OpenFarReaderClassArgs)
std::string LoadArcTypeFromFar(const std::string &far_source)
Definition: script-impl.cc:34
void CreateFarWriterClass(CreateFarWriterClassArgs *args)
Definition: far-class.h:278
friend void OpenFarReaderClass(OpenFarReaderClassArgs *args)
Definition: far-class.h:165
static std::unique_ptr< FarReaderClass > Open(std::string_view source)
Definition: far-class.cc:38