fst-utils.cc
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// sherpa-onnx/csrc/fst-utils.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/fst-utils.h"
#include "sherpa-onnx/csrc/macros.h"
namespace sherpa_onnx {
// This function is copied from kaldi.
//
// @param filename Path to a StdVectorFst or StdConstFst graph
// @return The caller should free the returned pointer using `delete` to
// avoid memory leak.
fst::Fst<fst::StdArc> *ReadGraph(const std::string &filename) {
// read decoding network FST
std::ifstream is(filename, std::ios::binary);
if (!is.good()) {
SHERPA_ONNX_LOGE("Could not open decoding-graph FST %s", filename.c_str());
}
fst::FstHeader hdr;
if (!hdr.Read(is, "<unknown>")) {
SHERPA_ONNX_LOGE("Reading FST: error reading FST header.");
}
if (hdr.ArcType() != fst::StdArc::Type()) {
SHERPA_ONNX_LOGE("FST with arc type %s not supported",
hdr.ArcType().c_str());
}
fst::FstReadOptions ropts("<unspecified>", &hdr);
fst::Fst<fst::StdArc> *decode_fst = nullptr;
if (hdr.FstType() == "vector") {
decode_fst = fst::VectorFst<fst::StdArc>::Read(is, ropts);
} else if (hdr.FstType() == "const") {
decode_fst = fst::ConstFst<fst::StdArc>::Read(is, ropts);
} else {
SHERPA_ONNX_LOGE("Reading FST: unsupported FST type: %s",
hdr.FstType().c_str());
}
if (decode_fst == nullptr) { // fst code will warn.
SHERPA_ONNX_LOGE("Error reading FST (after reading header).");
return nullptr;
} else {
return decode_fst;
}
}
} // namespace sherpa_onnx