20 #ifndef FST_EXTENSIONS_FAR_FAR_H_ 21 #define FST_EXTENSIONS_FAR_FAR_H_ 37 #include <string_view> 53 inline bool IsFst(
const std::string &source) {
54 std::ifstream strm(source, std::ios_base::in | std::ios_base::binary);
55 if (!strm)
return false;
56 int32_t magic_number = 0;
65 const std::string &
ArcType()
const {
return arctype_; }
69 bool Read(
const std::string &source) {
75 arctype_ =
"standard";
87 }
else if (
IsFst(source)) {
89 std::ifstream istrm(source,
90 std::ios_base::in | std::ios_base::binary);
91 if (!fsthdr.
Read(istrm, source))
return false;
100 std::string arctype_;
115 virtual void Add(std::string_view key,
const Fst<Arc> &
fst) = 0;
117 virtual FarType Type()
const = 0;
119 virtual bool Error()
const = 0;
135 static FarReader *Open(
const std::string &source);
139 static FarReader *Open(
const std::vector<std::string> &sources);
142 virtual void Reset() = 0;
145 virtual bool Find(std::string_view key) = 0;
148 virtual bool Done()
const = 0;
151 virtual void Next() = 0;
155 virtual const std::string &GetKey()
const = 0;
159 virtual const Fst<Arc> *GetFst()
const = 0;
161 virtual FarType Type()
const = 0;
163 virtual bool Error()
const = 0;
190 writer_->Add(key,
fst);
195 bool Error() const final {
return writer_->Error(); }
215 writer_->Add(key,
fst);
220 bool Error() const final {
return writer_->Error(); }
235 : source_(source), error_(false), written_(false) {}
243 LOG(WARNING) <<
"FstFarWriter::Add: only one FST supported," 244 <<
" subsequent entries discarded.";
246 error_ = !
fst.Write(source_);
253 bool Error() const final {
return error_; }
276 LOG(ERROR) <<
"FarWriter::Create: Unknown FAR type";
298 if (!reader || reader->Error())
return nullptr;
305 if (!reader || reader->Error())
return nullptr;
309 void Reset() final { reader_->Reset(); }
311 bool Find(std::string_view key)
final {
return reader_->Find(key); }
313 bool Done() const final {
return reader_->Done(); }
315 void Next() final {
return reader_->Next(); }
317 const std::string &
GetKey() const final {
return reader_->GetKey(); }
323 bool Error() const final {
return reader_->Error(); }
328 : reader_(std::move(reader)) {}
341 if (!reader || reader->Error())
return nullptr;
348 if (!reader || reader->Error())
return nullptr;
352 void Reset() final { reader_->Reset(); }
354 bool Find(std::string_view key)
final {
return reader_->Find(key); }
356 bool Done() const final {
return reader_->Done(); }
358 void Next() final {
return reader_->Next(); }
360 const std::string &
GetKey() const final {
return reader_->GetKey(); }
366 bool Error() const final {
return reader_->Error(); }
371 : reader_(std::move(reader)) {}
382 std::vector<std::string> sources;
383 sources.push_back(source);
392 : keys_(sources), has_stdin_(false), pos_(0), error_(false) {
393 std::sort(keys_.begin(), keys_.end());
394 streams_.resize(keys_.size(),
nullptr);
395 for (
size_t i = 0; i < keys_.size(); ++i) {
396 if (keys_[i].empty()) {
398 streams_[i] = &std::cin;
401 FSTERROR() <<
"FstFarReader::FstFarReader: standard input should " 402 "only appear once in the input file list";
407 streams_[i] =
new std::ifstream(
408 keys_[i], std::ios_base::in | std::ios_base::binary);
409 if (streams_[i]->fail()) {
410 FSTERROR() <<
"FstFarReader::FstFarReader: Error reading file: " 417 if (pos_ >= keys_.size())
return;
424 <<
"FstFarReader::Reset: Operation not supported on standard input";
432 bool Find(std::string_view key)
final {
435 <<
"FstFarReader::Find: Operation not supported on standard input";
444 bool Done() const final {
return error_ || pos_ >= keys_.size(); }
451 const std::string &
GetKey() const final {
return keys_[pos_]; }
457 bool Error() const final {
return error_; }
460 for (
size_t i = 0; i < keys_.size(); ++i) {
461 if (streams_[i] != &std::cin) {
470 if (pos_ >= keys_.size())
return;
471 streams_[pos_]->seekg(0);
474 FSTERROR() <<
"FstFarReader: Error reading Fst from: " << keys_[pos_];
479 std::vector<std::string> keys_;
480 std::vector<std::istream *> streams_;
483 mutable std::unique_ptr<Fst<Arc>> fst_;
495 else if (
IsFst(source))
502 if (!sources.empty() && sources[0].empty())
504 else if (!sources.empty() &&
IsSTTable(sources[0]))
506 else if (!sources.empty() &&
IsSTList(sources[0]))
508 else if (!sources.empty() &&
IsFst(sources[0]))
515 #endif // FST_EXTENSIONS_FAR_FAR_H_
constexpr int32_t kFstMagicNumber
static STListFarReader * Open(const std::vector< std::string > &sources)
bool Find(std::string_view key) final
bool IsFst(const std::string &source)
FarType Type() const final
const Fst< Arc > * GetFst() const final
void Create(const std::vector< std::string > &sources, FarWriter< Arc > &writer, int32_t generate_keys, const std::string &key_prefix, const std::string &key_suffix)
static Fst * Read(std::istream &strm, const FstReadOptions &opts)
static FarReader * Open(const std::string &source)
const std::string & GetKey() const final
virtual bool Write(std::ostream &strm, const FstWriteOptions &opts) const
static STTableFarReader * Open(const std::vector< std::string > &sources)
static const std::string & Type()
static STListFarWriter * Create(const std::string &source)
std::unique_ptr< T > WrapUnique(T *ptr)
FarType Type() const final
static FstFarReader * Open(const std::string &source)
bool ReadSTListHeader(const std::string &source, Header *header)
bool Find(std::string_view key) final
const Fst< Arc > * GetFst() const final
void Add(std::string_view key, const Fst< Arc > &fst) final
Fst< Arc > * operator()(std::istream &strm, const FstReadOptions &options=FstReadOptions()) const
static STListFarReader * Open(const std::string &source)
FarType Type() const final
static FstFarWriter * Create(const std::string &source)
bool Find(std::string_view key) final
bool ReadSTTableHeader(const std::string &source, Header *header)
static STTableFarReader * Open(const std::string &source)
FstFarWriter(const std::string &source)
FarType Type() const final
bool IsSTList(const std::string &source)
bool IsSTTable(const std::string &source)
FarType Type() const final
static FstFarReader * Open(const std::vector< std::string > &sources)
static FarWriter * Create(const std::string &source, FarType type=FarType::DEFAULT)
const Fst< Arc > * GetFst() const final
std::istream & ReadType(std::istream &strm, T *t)
FstFarReader(const std::vector< std::string > &sources)
void Add(std::string_view key, const Fst< A > &fst) final
static STTableFarWriter * Create(const std::string &source)
const std::string & GetKey() const final
void operator()(std::ostream &strm, const Fst< Arc > &fst) const
const std::string & GetKey() const final
void Add(std::string_view key, const Fst< Arc > &fst) final
FarType Type() const final