Home | History | Annotate | Download | only in far
      1 
      2 // Licensed under the Apache License, Version 2.0 (the "License");
      3 // you may not use this file except in compliance with the License.
      4 // You may obtain a copy of the License at
      5 //
      6 //     http://www.apache.org/licenses/LICENSE-2.0
      7 //
      8 // Unless required by applicable law or agreed to in writing, software
      9 // distributed under the License is distributed on an "AS IS" BASIS,
     10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11 // See the License for the specific language governing permissions and
     12 // limitations under the License.
     13 //
     14 // Copyright 2005-2010 Google, Inc.
     15 // Author: allauzen (at) google.com (Cyril Allauzen)
     16 // Modified: jpr (at) google.com (Jake Ratkiewicz)
     17 
     18 #ifndef FST_EXTENSIONS_FAR_INFO_H_
     19 #define FST_EXTENSIONS_FAR_INFO_H_
     20 
     21 #include <iomanip>
     22 #include <set>
     23 #include <string>
     24 #include <vector>
     25 using std::vector;
     26 
     27 #include <fst/extensions/far/far.h>
     28 #include <fst/extensions/far/main.h>  // For FarTypeToString
     29 
     30 namespace fst {
     31 
     32 template <class Arc>
     33 void CountStatesAndArcs(const Fst<Arc> &fst, size_t *nstate, size_t *narc) {
     34   StateIterator<Fst<Arc> > siter(fst);
     35   for (; !siter.Done(); siter.Next(), ++(*nstate)) {
     36     ArcIterator<Fst<Arc> > aiter(fst, siter.Value());
     37     for (; !aiter.Done(); aiter.Next(), ++(*narc)) {}
     38   }
     39 }
     40 
     41 struct KeyInfo {
     42   string key;
     43   string type;
     44   size_t nstate;
     45   size_t narc;
     46 
     47   KeyInfo(string k, string t, int64 ns = 0, int64 na = 0)
     48   : key(k), type(t), nstate(ns), narc(na) {}
     49 };
     50 
     51 template <class Arc>
     52 void FarInfo(const vector<string> &filenames, const string &begin_key,
     53              const string &end_key, const bool list_fsts) {
     54   FarReader<Arc> *far_reader = FarReader<Arc>::Open(filenames);
     55   if (!far_reader) return;
     56 
     57   if (!begin_key.empty())
     58     far_reader->Find(begin_key);
     59 
     60   vector<KeyInfo> *infos = list_fsts ? new vector<KeyInfo>() : 0;
     61   size_t nfst = 0, nstate = 0, narc = 0;
     62   set<string> fst_types;
     63   for (; !far_reader->Done(); far_reader->Next()) {
     64     string key = far_reader->GetKey();
     65     if (!end_key.empty() && end_key < key)
     66       break;
     67     ++nfst;
     68     const Fst<Arc> &fst = far_reader->GetFst();
     69     fst_types.insert(fst.Type());
     70     if (infos) {
     71       KeyInfo info(key, fst.Type());
     72       CountStatesAndArcs(fst, &info.nstate, &info.narc);
     73       nstate += info.nstate;
     74       nstate += info.narc;
     75       infos->push_back(info);
     76     } else {
     77       CountStatesAndArcs(fst, &nstate, &narc);
     78     }
     79   }
     80 
     81   if (!infos) {
     82     cout << std::left << setw(50) << "far type"
     83          << FarTypeToString(far_reader->Type()) << endl;
     84     cout << std::left << setw(50) << "arc type" << Arc::Type() << endl;
     85     cout << std::left << setw(50) << "fst type";
     86     for (set<string>::const_iterator iter = fst_types.begin();
     87          iter != fst_types.end();
     88          ++iter) {
     89       if (iter != fst_types.begin())
     90         cout << ",";
     91       cout << *iter;
     92     }
     93     cout << endl;
     94     cout << std::left << setw(50) << "# of FSTs" << nfst << endl;
     95     cout << std::left << setw(50) << "total # of states" << nstate << endl;
     96     cout << std::left << setw(50) << "total # of arcs" << narc << endl;
     97 
     98   } else  {
     99     int wkey = 10, wtype = 10, wnstate = 16, wnarc = 16;
    100     for (size_t i = 0; i < infos->size(); ++i) {
    101       const KeyInfo &info = (*infos)[i];
    102       if (info.key.size() + 2 > wkey)
    103         wkey = info.key.size() + 2;
    104       if (info.type.size() + 2 > wtype)
    105         wtype = info.type.size() + 2;
    106       if (ceil(log10(info.nstate)) + 2 > wnstate)
    107         wnstate = ceil(log10(info.nstate)) + 2;
    108       if (ceil(log10(info.narc)) + 2 > wnarc)
    109         wnarc = ceil(log10(info.narc)) + 2;
    110     }
    111 
    112     cout << std::left << setw(wkey) << "key" << setw(wtype) << "type"
    113          << std::right << setw(wnstate) << "# of states"
    114          << setw(wnarc) << "# of arcs" << endl;
    115 
    116     for (size_t i = 0; i < infos->size(); ++i) {
    117       const KeyInfo &info = (*infos)[i];
    118       cout << std::left << setw(wkey) << info.key << setw(wtype) << info.type
    119            << std::right << setw(wnstate) << info.nstate
    120            << setw(wnarc) << info.narc << endl;
    121     }
    122   }
    123 }
    124 
    125 }  // namespace fst
    126 
    127 
    128 #endif  // FST_EXTENSIONS_FAR_INFO_H_
    129