15 #ifndef TASK_DUMPNBESTFEATURES_HPP 16 #define TASK_DUMPNBESTFEATURES_HPP 31 template<
class Data,
class Arc = fst::StdArc>
44 std::vector<float>& scales_;
47 const std::string sparseweightlatticekey_;
51 const unsigned trigger_;
56 const unsigned offset_;
58 const std::string wordmapkey_;
70 const unsigned offset = 1,
71 const std::string& sparseweightlatticekey =
73 const std::string& wordmapkey =
77 sparseweightlatticekey_ ( sparseweightlatticekey ),
78 fdir_ ( rg.get<std::string>
80 nbdir_ ( rg.get<std::string>
83 scales_ (
fst::TropicalSparseTupleWeight<float>::Params() ),
84 trigger_ ( rg.get<unsigned>
86 wordmapkey_ ( wordmapkey ) {
92 const unsigned offset = 1 ,
93 const std::string& sparseweightlatticekey =
105 if ( d.fsts.find ( sparseweightlatticekey_ ) == d.fsts.end() ) {
106 LWARN (
"No sparse tuple lattice to dump!" );
109 fst::VectorFst<TupleArc32> *vectorlattice =
110 static_cast<fst::VectorFst<TupleArc32> *
> ( d.fsts[sparseweightlatticekey_] );
111 if ( nbdir_() !=
"" ) {
112 if ( d.wm.find ( wordmapkey_ ) != d.wm.end() )
113 wm_ = d.wm[wordmapkey_];
124 const std::string& filename ) {
126 ,
"Attempting to write an nbest file from an empty lattice!" );
127 unordered_map<std::string, float> hyps;
128 unordered_map<std::string, std::string> fhyps;
129 fst::VectorFst<Arc> lattice;
135 fst::printstrings<Arc> ( lattice, hyps );
136 std::priority_queue<struct fst::hypcost, std::vector< struct fst::hypcost >,
fst::CompareHyp>
138 for ( unordered_map<std::string, float>::iterator itx = hyps.begin()
142 if (wm_ != NULL) (*wm_) (itx->first, &hc.
hyp,
false);
143 else hc.
hyp = itx->first;
144 boost::algorithm::trim ( hc.
hyp );
145 hc.
cost = itx->second;
148 FORCELINFO (
"Write Nbest File to " << filename );
150 while ( !pq.empty() ) {
152 file << hc.
hyp <<
"\t" << hc.
cost << endl;
160 const std::string& filename ) {
162 "Attempting to write feature file for an empty lattice!!!!" );
163 unordered_map<std::string, float> hyps;
164 unordered_map<std::string, std::string> fhyps;
165 fst::VectorFst<Arc> lattice;
169 fst::printstrings<Arc> ( lattice, hyps );
170 fst::VectorFst<TupleArc32> vl ( vectorlattice );
171 unordered_set<unsigned> latticefeatures;
173 LINFO (
"Number of Active features:" << latticefeatures.size() );
174 for (
unsigned k = 0; k < scales_.size(); ++k ) {
175 if ( k + 1 > trigger_
176 && latticefeatures.find ( k + 1 ) == latticefeatures.end() )
continue;
177 fst::VectorFst<Arc> dimfst;
183 unordered_map<std::string, float> dhyps;
184 fst::printstrings<Arc> ( dimfst, dhyps );
186 for ( unordered_map<std::string, float>::iterator itx = dhyps.begin();
187 itx != dhyps.end(); ++itx ) {
188 if ( k + 1 > trigger_
189 && itx->second == 0.0000f )
continue;
190 if ( fhyps.find ( itx->first ) != fhyps.end() ) fhyps[itx->first] +=
"\t" +
191 toString<float> ( itx->second, 4 );
192 else fhyps[itx->first] = toString<float> ( itx->second, 4 );
193 if ( k + 1 > trigger_ ) fhyps[itx->first] +=
"@" + toString<unsigned>
197 std::priority_queue<struct fst::hypcost, std::vector< struct fst::hypcost >,
fst::CompareHyp>
199 for ( unordered_map<std::string, float>::iterator itx = hyps.begin()
203 hc.
hyp = fhyps[itx->first];
204 hc.
cost = itx->second;
207 FORCELINFO (
"Write Feature File to " << filename );
209 while ( !pq.empty() ) {
211 file << hc.
hyp << endl;
221 #endif // TASK_DUMPNBESTFEATURES_HPP Wrapper stream class that writes to pipes, text files or gzipped files.
class that expands a wildcard into its actual value. This is useful e.g. for filenames ranging severa...
std::string toString(const T &x, uint pr=2)
Converts an arbitrary type to string Converts to string integers, floats, doubles Quits execution if ...
bool run(Data &d)
Dumps nbest or features from sparse tuple weight lattice in the data object d, if it exists...
fst::TropicalWeightTpl< F > Map(double)
static DumpNbestFeaturesTask * init(const ucam::util::RegistryPO &rg, const unsigned offset=1, const std::string &sparseweightlatticekey=HifstConstants::kSparseweightvectorlatticeStore)
const std::string kSparseweightvectorlatticeStorenbestfile
void writeNbestFile(const fst::VectorFst< TupleArc32 > &vectorlattice, const std::string &filename)
Specific method to write nbest list from the sparse vector weight lattice.
Templated (hybrid) Interface for Task classes.
templated Mapper that modifies weights when copying from one FST to another, passing through the othe...
Functor to convert sparse tuple weight to tropical (single weight)
const std::string kSparseweightvectorlatticeWordmap
const std::string kSparseweightvectorlatticeStorefeaturefile
void listSparseFeatureIndices(VectorFst< TupleArc32 > &myfst, unordered_set< uint > &idx)
Traverses a machine and returns the indices actually used for the sparse vector weight tropical semir...
DumpNbestFeaturesTask(const ucam::util::RegistryPO &rg, const unsigned offset=1, const std::string &sparseweightlatticekey=HifstConstants::kSparseweightvectorlatticeStore, const std::string &wordmapkey=HifstConstants::kSparseweightvectorlatticeWordmap)
Constructor.
const std::string kSparseweightvectorlatticeStore
bool exists(const std::string &key) const
Determines whether a program option (key) has been defined by the user.
Struct for priority queue comparison.
const std::string kSparseweightvectorlatticeFirstsparsefeatureatindex
Class used by priority queue to compare two hypotheses and decide which one wins. ...
Task that dumps nbest and feature file. Templated on specific Data object and Fst Arc...
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
Loads efficiently a wordmap file and provides methods to map word-to-integer or integer-to-word. To avoid memory footprint issues, hashing the wordmap entries is avoided.
Map functor used with generic weight mapper.
void writeFeatureFile(const fst::VectorFst< TupleArc32 > &vectorlattice, const std::string &filename)
Specific method to dump feature file from sparse vector weight lattice.
void close()
Closes the file.