Phonetisaurus  1.0
FST-based Grapheme-to-Phoneme conversion
feature-reader.cc
Go to the documentation of this file.
1 #include <fst/fstlib.h>
2 #include "LegacyRnnLMHash.h"
3 #include "RnnLMDecoder.h"
4 #include "LegacyRnnLMDecodable.h"
5 #include "LegacyRnnLMReader.h"
6 using namespace fst;
7 
8 //typedef std::unordered_map<std::string, std::vector<int> > FMAP;
9 typedef std::unordered_map<int, std::vector<int> > FMAP;
10 
11 template<class H>
12 void LoadFeatureConf (const H&h, FMAP* fmap, std::string& featurefilename) {
13  std::ifstream ifp (featurefilename.c_str ());
14  std::string prefix = "#";
15  std::string line;
16 
17  if (ifp.is_open ()) {
18  while (ifp.good ()) {
19  getline (ifp, line);
20  if (line.empty ())
21  continue;
22 
23  std::vector<int> ids;
24  int id;
25  std::string word;
26  if (!line.compare (0, prefix.size (), prefix))
27  continue;
28 
29  std::stringstream ss (line);
30  ss >> word;
31  while (ss >> id)
32  ids.push_back (id);
33  cout << "Item: " << word << " " << h.GetWordId (word) << endl;
34  (*fmap) [h.GetWordId (word)] = ids;
35  }
36  ifp.close ();
37  }
38 }
39 
41 DEFINE_string (rnnlm, "", "The input RnnLM model.");
42 DEFINE_string (feats, "", "Auxiliary features conf file.");
43 
44 int main (int argc, char* argv []) {
45  string usage = "feature-reader --rnnlm=test.rnnlm --feats=features.conf\n\n Usage: ";
46  set_new_handler (FailedNewHandler);
47  SetFlags (usage.c_str (), &argc, &argv, false);
48 
50  LegacyRnnLMHash h = reader.CopyVocabHash ();
51 
52  FMAP fmap;
53 
54  LoadFeatureConf (h, &fmap, FLAGS_feats);
55 
56  for (FMAP::iterator it = fmap.begin (); it != fmap.end (); ++it) {
57  std::cout << it->first << "\t";
58  const std::vector<int>& feats = (*it).second;
59  for (int i = 0; i < feats.size (); i++)
60  cout << feats [i] << ((i == feats.size ()) ? "" : " ");
61  cout << endl;
62  }
63 
64  return 0;
65 }
void LoadFeatureConf(const H &h, FMAP *fmap, std::string &featurefilename)
DEFINE_string(rnnlm,"","The input RnnLM model.")
int main(int argc, char *argv[])
std::unordered_map< int, std::vector< int > > FMAP
LegacyRnnLMDecodable< Token, LegacyRnnLMHash > Decodable