-
Notifications
You must be signed in to change notification settings - Fork 12
/
fstprint-nbest-strings.cc
67 lines (63 loc) · 2.47 KB
/
fstprint-nbest-strings.cc
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
#include <fst/fstlib.h>
#include <iostream>
#include <sstream>
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "usage: cat <fst> | " << argv[0] << " <n>\n";
return 1;
}
int n;
std::stringstream reader(argv[1]);
reader >> n;
if(n < 1) {
std::cerr << "error: invalid n = " << n << "\n";
return 2;
}
fst::StdVectorFst* input = fst::StdVectorFst::Read("");
fst::StdVectorFst result;
fst::ShortestPath(*input, &result, n);
fst::Push(&result, fst::REWEIGHT_TO_INITIAL);
const fst::SymbolTable* outputSymbols = result.OutputSymbols();
const fst::SymbolTable* inputSymbols = result.InputSymbols();
for(fst::ArcIterator<fst::StdVectorFst> aiter(result, result.Start()); !aiter.Done(); aiter.Next()) {
const fst::StdArc& path = aiter.Value();
int64 state = path.nextstate;
std::cout << path.weight;
if(path.ilabel == path.olabel) {
if(path.olabel != 0) {
if(outputSymbols) std::cout << " " << outputSymbols->Find(path.olabel);
else std::cout << " " << path.olabel;
}
} else {
if(path.olabel != 0) {
if(inputSymbols) std::cout << " " << inputSymbols->Find(path.ilabel);
else std::cout << " " << path.ilabel;
if(outputSymbols) std::cout << "/" << outputSymbols->Find(path.olabel);
else std::cout << "/" << path.olabel;
}
}
while(result.Final(state) == fst::StdArc::Weight::Zero()) {
fst::ArcIterator<fst::StdVectorFst> nextIter(result, state);
if(nextIter.Done()) {
break;
// this should not happen
}
const fst::StdArc arc = nextIter.Value();
if(arc.ilabel == arc.olabel) {
if(arc.olabel != 0) {
if(outputSymbols) std::cout << " " << outputSymbols->Find(arc.olabel);
else std::cout << " " << arc.olabel;
}
} else {
if(arc.olabel != 0) {
if(inputSymbols) std::cout << " " << inputSymbols->Find(arc.ilabel);
else std::cout << " " << arc.ilabel;
if(outputSymbols) std::cout << "/" << outputSymbols->Find(arc.olabel);
else std::cout << "/" << arc.olabel;
}
}
state = arc.nextstate;
}
std::cout << "\n";
}
}