onlinebin-util.cc
2.49 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// online2/onlinebin-util.cc
// Copyright 2012 Cisco Systems (author: Matthias Paulik)
// Modifications to the original contribution by Cisco Systems made by:
// Vassil Panayotov
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "online2/onlinebin-util.h"
namespace kaldi {
fst::Fst<fst::StdArc> *ReadDecodeGraph(std::string filename) {
// read decoding network FST
Input ki(filename); // use ki.Stream() instead of is.
if (!ki.Stream().good()) KALDI_ERR << "Could not open decoding-graph FST "
<< filename;
fst::FstHeader hdr;
if (!hdr.Read(ki.Stream(), "<unknown>")) {
KALDI_ERR << "Reading FST: error reading FST header.";
}
if (hdr.ArcType() != fst::StdArc::Type()) {
KALDI_ERR << "FST with arc type " << hdr.ArcType() << " not supported.\n";
}
fst::FstReadOptions ropts("<unspecified>", &hdr);
fst::Fst<fst::StdArc> *decode_fst = NULL;
if (hdr.FstType() == "vector") {
decode_fst = fst::VectorFst<fst::StdArc>::Read(ki.Stream(), ropts);
} else if (hdr.FstType() == "const") {
decode_fst = fst::ConstFst<fst::StdArc>::Read(ki.Stream(), ropts);
} else {
KALDI_ERR << "Reading FST: unsupported FST type: " << hdr.FstType();
}
if (decode_fst == NULL) { // fst code will warn.
KALDI_ERR << "Error reading FST (after reading header).";
return NULL;
} else {
return decode_fst;
}
}
void PrintPartialResult(const std::vector<int32>& words,
const fst::SymbolTable *word_syms,
bool line_break) {
KALDI_ASSERT(word_syms != NULL);
for (size_t i = 0; i < words.size(); i++) {
std::string word = word_syms->Find(words[i]);
if (word == "")
KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
std::cout << word << ' ';
}
if (line_break)
std::cout << "\n\n";
else
std::cout.flush();
}
} // namespace kaldi