27 int main(
int argc,
char *argv[]) {
29 using namespace kaldi;
31 typedef kaldi::int64 int64;
32 using fst::SymbolTable;
37 "Composes lattices (in transducer form, as type Lattice). Depending\n" 38 "on the command-line arguments, either composes lattices with lattices,\n" 39 "or lattices with FSTs (rspecifiers are assumed to be lattices, and\n" 40 "rxfilenames are assumed to be FSTs, which have their weights interpreted\n" 41 "as \"graph weights\" when converted into the Lattice format.\n" 43 "Usage: lattice-compose [options] lattice-rspecifier1 " 44 "(lattice-rspecifier2|fst-rxfilename2) lattice-wspecifier\n" 45 " e.g.: lattice-compose ark:1.lats ark:2.lats ark:composed.lats\n" 46 " or: lattice-compose ark:1.lats G.fst ark:composed.lats\n";
50 bool write_compact =
true;
51 int32 num_states_cache = 50000;
52 int32 phi_label = fst::kNoLabel;
53 po.
Register(
"write-compact", &write_compact,
"If true, write in normal (compact) form.");
54 po.
Register(
"phi-label", &phi_label,
"If >0, the label on backoff arcs of the LM");
55 po.
Register(
"num-states-cache", &num_states_cache,
56 "Number of states we cache when mapping LM FST to lattice type. " 57 "More -> more memory but faster.");
65 KALDI_ASSERT(phi_label > 0 || phi_label == fst::kNoLabel);
67 std::string lats_rspecifier1 = po.
GetArg(1),
69 lats_wspecifier = po.
GetArg(3);
70 int32 n_done = 0, n_fail = 0;
78 compact_lattice_writer.
Open(lats_wspecifier);
80 lattice_writer.
Open(lats_wspecifier);
83 std::string fst_rxfilename = arg2;
88 if (fst2->Properties(fst::kILabelSorted,
true) == 0) {
90 fst::ILabelCompare<StdArc> ilabel_comp;
91 ArcSort(fst2, ilabel_comp);
96 fst::CacheOptions cache_opts(
true, num_states_cache);
97 fst::MapFstOptions mapfst_opts(cache_opts);
99 fst::MapFst<StdArc, LatticeArc, fst::StdToLatticeMapper<BaseFloat> >
100 mapped_fst2(*fst2, mapper, mapfst_opts);
101 for (; !lattice_reader1.
Done(); lattice_reader1.
Next()) {
102 std::string key = lattice_reader1.
Key();
103 KALDI_VLOG(1) <<
"Processing lattice for key " << key;
105 ArcSort(&lat1, fst::OLabelCompare<LatticeArc>());
107 if (phi_label > 0)
PhiCompose(lat1, mapped_fst2, phi_label, &composed_lat);
108 else Compose(lat1, mapped_fst2, &composed_lat);
109 if (composed_lat.Start() == fst::kNoStateId) {
110 KALDI_WARN <<
"Empty lattice for utterance " << key <<
" (incompatible LM?)";
116 compact_lattice_writer.
Write(key, clat);
118 lattice_writer.
Write(key, composed_lat);
125 std::string lats_rspecifier2 = arg2;
131 for (; !lattice_reader1.
Done(); lattice_reader1.
Next()) {
132 std::string key = lattice_reader1.
Key();
133 KALDI_VLOG(1) <<
"Processing lattice for key " << key;
136 if (!lattice_reader2.
HasKey(key)) {
137 KALDI_WARN <<
"Not producing output for utterance " << key
138 <<
" because not present in second table.";
146 if (lat2.Properties(fst::kILabelSorted,
true) == 0
147 && lat1.Properties(fst::kOLabelSorted,
true) == 0) {
149 fst::ILabelCompare<LatticeArc> ilabel_comp;
150 fst::ArcSort(&lat2, ilabel_comp);
158 Compose(lat1, lat2, &lat_out);
160 if (lat_out.Start() == fst::kNoStateId) {
161 KALDI_WARN <<
"Empty lattice for utterance " << key <<
" (incompatible LM?)";
167 compact_lattice_writer.
Write(key, clat_out);
169 lattice_writer.
Write(key, lat_out);
176 KALDI_LOG <<
"Done " << n_done <<
" lattices; failed for " 178 return (n_done != 0 ? 0 : 1);
179 }
catch(
const std::exception &e) {
180 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool Open(const std::string &wspecifier)
void PropagateFinal(typename Arc::Label phi_label, MutableFst< Arc > *fst)
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void PhiCompose(const Fst< Arc > &fst1, const Fst< Arc > &fst2, typename Arc::Label phi_label, MutableFst< Arc > *ofst)
A templated class for writing objects to an archive or script file; see The Table concept...
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
const T & Value(const std::string &key)
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
fst::VectorFst< LatticeArc > Lattice
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int main(int argc, char *argv[])
bool HasKey(const std::string &key)
fst::VectorFst< CompactLatticeArc > CompactLattice
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Class StdToLatticeMapper maps a normal arc (StdArc) to a LatticeArc by putting the StdArc weight as t...
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)