FST  openfst-1.8.3
OpenFst Library
fst.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 //
18 // FST definitions.
19 
20 #include <fst/fst.h>
21 
22 #include <cstdint>
23 #include <istream>
24 #include <ostream>
25 #include <sstream>
26 #include <string>
27 
28 #include <fst/flags.h>
29 #include <fst/log.h>
30 #include <fst/symbol-table.h>
31 #include <fst/util.h>
32 #include <string_view>
33 
34 // FST flag definitions.
35 
36 DEFINE_bool(fst_verify_properties, false,
37  "Verify FST properties queried by TestProperties");
38 
39 DEFINE_bool(fst_default_cache_gc, true, "Enable garbage collection of cache");
40 
41 DEFINE_int64(fst_default_cache_gc_limit, 1 << 20LL,
42  "Cache byte size that triggers garbage collection");
43 
44 DEFINE_bool(fst_align, false, "Write FST data aligned where appropriate");
45 
46 DEFINE_string(save_relabel_ipairs, "", "Save input relabel pairs to file");
47 DEFINE_string(save_relabel_opairs, "", "Save output relabel pairs to file");
48 
49 DEFINE_string(fst_read_mode, "read",
50  "Default file reading mode for mappable files");
51 
52 namespace fst {
53 
54 // Checks FST magic number and reads in the header; if rewind = true,
55 // the stream is repositioned before call if possible.
56 bool FstHeader::Read(std::istream &strm, const std::string &source,
57  bool rewind) {
58  int64_t pos = 0;
59  if (rewind) pos = strm.tellg();
60  int32_t magic_number = 0;
61  ReadType(strm, &magic_number);
62  if (magic_number != kFstMagicNumber) {
63  LOG(ERROR) << "FstHeader::Read: Bad FST header: " << source
64  << ". Magic number not matched. Got: " << magic_number;
65  if (rewind) strm.seekg(pos);
66  return false;
67  }
68  ReadType(strm, &fsttype_);
69  ReadType(strm, &arctype_);
70  ReadType(strm, &version_);
71  ReadType(strm, &flags_);
72  ReadType(strm, &properties_);
73  ReadType(strm, &start_);
74  ReadType(strm, &numstates_);
75  ReadType(strm, &numarcs_);
76  if (!strm) {
77  LOG(ERROR) << "FstHeader::Read: Read failed: " << source;
78  return false;
79  }
80  if (rewind) strm.seekg(pos);
81  return true;
82 }
83 
84 // Writes FST magic number and FST header.
85 bool FstHeader::Write(std::ostream &strm, std::string_view) const {
87  WriteType(strm, fsttype_);
88  WriteType(strm, arctype_);
89  WriteType(strm, version_);
90  WriteType(strm, flags_);
91  WriteType(strm, properties_);
92  WriteType(strm, start_);
93  WriteType(strm, numstates_);
94  WriteType(strm, numarcs_);
95  return true;
96 }
97 
98 std::string FstHeader::DebugString() const {
99  std::ostringstream ostrm;
100  ostrm << "fsttype: \"" << fsttype_ << "\" arctype: \"" << arctype_
101  << "\" version: \"" << version_ << "\" flags: \"" << flags_
102  << "\" properties: \"" << properties_ << "\" start: \"" << start_
103  << "\" numstates: \"" << numstates_ << "\" numarcs: \"" << numarcs_
104  << "\"";
105  return ostrm.str();
106 }
107 
108 FstReadOptions::FstReadOptions(const std::string_view source,
109  const FstHeader * header,
110  const SymbolTable * isymbols,
111  const SymbolTable * osymbols)
112  : source(source),
113  header(header),
114  isymbols(isymbols),
115  osymbols(osymbols),
116  read_isymbols(true),
117  read_osymbols(true) {
118  mode = ReadMode(FST_FLAGS_fst_read_mode);
119 }
120 
121 FstReadOptions::FstReadOptions(const std::string_view source,
122  const SymbolTable *isymbols,
123  const SymbolTable *osymbols)
124  : FstReadOptions(source, /*header=*/nullptr, isymbols, osymbols) {}
125 
127  if (mode == "read") return READ;
128  if (mode == "map") return MAP;
129  LOG(ERROR) << "Unknown file read mode " << mode;
130  return READ;
131 }
132 
133 std::string FstReadOptions::DebugString() const {
134  std::ostringstream ostrm;
135  ostrm << "source: \"" << source << "\" mode: \""
136  << (mode == READ ? "READ" : "MAP") << "\" read_isymbols: \""
137  << (read_isymbols ? "true" : "false") << "\" read_osymbols: \""
138  << (read_osymbols ? "true" : "false") << "\" header: \""
139  << (header ? "set" : "null") << "\" isymbols: \""
140  << (isymbols ? "set" : "null") << "\" osymbols: \""
141  << (osymbols ? "set" : "null") << "\"";
142  return ostrm.str();
143 }
144 
145 } // namespace fst
bool Read(std::istream &strm, const std::string &source, bool rewind=false)
Definition: fst.cc:56
constexpr int32_t kFstMagicNumber
Definition: fst.h:57
const FstHeader * header
Definition: fst.h:74
std::string source
Definition: fst.h:73
bool Write(std::ostream &strm, std::string_view source) const
Definition: fst.cc:85
#define LOG(type)
Definition: log.h:53
bool read_isymbols
Definition: fst.h:81
const SymbolTable * osymbols
Definition: fst.h:78
FstReadOptions(const std::string_view source="<unspecified>", const FstHeader *header=nullptr, const SymbolTable *isymbols=nullptr, const SymbolTable *osymbols=nullptr)
Definition: fst.cc:108
std::string DebugString() const
Definition: fst.cc:98
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:228
std::string DebugString() const
Definition: fst.cc:133
static FileReadMode ReadMode(std::string_view mode)
Definition: fst.cc:126
DEFINE_string(save_relabel_ipairs,"","Save input relabel pairs to file")
DEFINE_int64(fst_default_cache_gc_limit, 1<< 20LL,"Cache byte size that triggers garbage collection")
FileReadMode mode
Definition: fst.h:80
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:80
const SymbolTable * isymbols
Definition: fst.h:76
DEFINE_bool(fst_verify_properties, false,"Verify FST properties queried by TestProperties")
bool read_osymbols
Definition: fst.h:82