Home | History | Annotate | Download | only in far
      1 // extract-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 the new arc-dispatch
     18 
     19 // \file
     20 // Extracts component FSTs from an finite-state archive.
     21 //
     22 
     23 #ifndef FST_EXTENSIONS_FAR_EXTRACT_H__
     24 #define FST_EXTENSIONS_FAR_EXTRACT_H__
     25 
     26 #include <string>
     27 #include <vector>
     28 using std::vector;
     29 
     30 #include <fst/extensions/far/far.h>
     31 
     32 namespace fst {
     33 
     34 template<class Arc>
     35 void FarExtract(const vector<string> &ifilenames,
     36                 const int32 &generate_filenames,
     37                 const string &begin_key,
     38                 const string &end_key,
     39                 const string &filename_prefix,
     40                 const string &filename_suffix) {
     41   FarReader<Arc> *far_reader = FarReader<Arc>::Open(ifilenames);
     42   if (!far_reader) return;
     43 
     44   if (!begin_key.empty())
     45     far_reader->Find(begin_key);
     46 
     47   string okey;
     48   int nrep = 0;
     49   for (int i = 1; !far_reader->Done(); far_reader->Next(), ++i) {
     50     string key = far_reader->GetKey();
     51     if (!end_key.empty() && end_key < key)
     52       break;
     53     const Fst<Arc> &fst = far_reader->GetFst();
     54 
     55     if (key == okey)
     56       ++nrep;
     57     else
     58       nrep = 0;
     59 
     60     okey = key;
     61 
     62     string ofilename;
     63     if (generate_filenames) {
     64       ostringstream tmp;
     65       tmp.width(generate_filenames);
     66       tmp.fill('0');
     67       tmp << i;
     68       ofilename = tmp.str();
     69     } else {
     70       if (nrep > 0) {
     71         ostringstream tmp;
     72         tmp << '.' << nrep;
     73         key += tmp.str();
     74       }
     75       ofilename = key;
     76     }
     77     fst.Write(filename_prefix + ofilename + filename_suffix);
     78   }
     79 
     80   return;
     81 }
     82 
     83 }  // namespace fst
     84 
     85 #endif  // FST_EXTENSIONS_FAR_EXTRACT_H__
     86