1 // create-main.h 2 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Copyright 2005-2010 Google, Inc. 16 // Author: riley (at) google.com (Michael Riley) 17 // Modified: jpr (at) google.com (Jake Ratkiewicz) to use new dispatch 18 // 19 // \file 20 // Creates a finite-state archive from component FSTs. Includes 21 // helper function for farcreate.cc that templates the main on the arc 22 // type to support multiple and extensible arc types. 23 // 24 25 #ifndef FST_EXTENSIONS_FAR_CREATE_H__ 26 #define FST_EXTENSIONS_FAR_CREATE_H__ 27 28 #include <libgen.h> 29 #include <string> 30 #include <vector> 31 using std::vector; 32 33 #include <fst/extensions/far/far.h> 34 35 namespace fst { 36 37 template <class Arc> 38 void FarCreate(const vector<string> &in_fnames, 39 const string &out_fname, 40 const int32 generate_keys, 41 const bool file_list_input, 42 const FarType &far_type, 43 const string &key_prefix, 44 const string &key_suffix) { 45 FarWriter<Arc> *far_writer = 46 FarWriter<Arc>::Create(out_fname, far_type); 47 if (!far_writer) return; 48 49 vector<string> inputs; 50 if (file_list_input) { 51 for (int i = 1; i < in_fnames.size(); ++i) { 52 ifstream istrm(in_fnames[i].c_str()); 53 string str; 54 while (getline(istrm, str)) 55 inputs.push_back(str); 56 } 57 } else { 58 inputs = in_fnames; 59 } 60 61 for (int i = 0; i < inputs.size(); ++i) { 62 Fst<Arc> *ifst = Fst<Arc>::Read(inputs[i]); 63 if (!ifst) return; 64 string key; 65 if (generate_keys > 0) { 66 ostringstream keybuf; 67 keybuf.width(generate_keys); 68 keybuf.fill('0'); 69 keybuf << i + 1; 70 key = keybuf.str(); 71 } else { 72 char* filename = new char[inputs[i].size() + 1]; 73 strcpy(filename, inputs[i].c_str()); 74 key = basename(filename); 75 delete[] filename; 76 } 77 78 far_writer->Add(key_prefix + key + key_suffix, *ifst); 79 delete ifst; 80 } 81 82 delete far_writer; 83 } 84 85 } // namespace fst 86 87 #endif // FST_EXTENSIONS_FAR_CREATE_H__ 88