FST  openfst-1.8.2
OpenFst Library
print-strings.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Outputs as strings the string FSTs in a finite-state archive.
19 
20 #ifndef FST_EXTENSIONS_FAR_PRINT_STRINGS_H_
21 #define FST_EXTENSIONS_FAR_PRINT_STRINGS_H_
22 
23 #include <cstdint>
24 #include <iomanip>
25 #include <string>
26 #include <vector>
27 
28 #include <fst/flags.h>
29 #include <fst/extensions/far/far.h>
30 #include <fstream>
31 #include <fst/shortest-distance.h>
32 #include <fst/string.h>
33 
34 DECLARE_string(far_field_separator);
35 
36 namespace fst {
37 
38 template <class Arc>
39 void PrintStrings(FarReader<Arc> &reader, FarEntryType entry_type,
40  TokenType token_type, const std::string &begin_key,
41  const std::string &end_key, bool print_key, bool print_weight,
42  const std::string &symbols_source, bool initial_symbols,
43  int32_t generate_sources, const std::string &source_prefix,
44  const std::string &source_suffix) {
45  std::unique_ptr<const SymbolTable> syms;
46  if (!symbols_source.empty()) {
47  // TODO(kbg): Allow negative flag?
48  const SymbolTableTextOptions opts(true);
49  syms.reset(SymbolTable::ReadText(symbols_source, opts));
50  if (!syms) {
51  LOG(ERROR) << "PrintStrings: Error reading symbol table "
52  << symbols_source;
53  return;
54  }
55  }
56  if (!begin_key.empty()) reader.Find(begin_key);
57  std::string okey;
58  int nrep = 0;
59  for (int i = 1; !reader.Done(); reader.Next(), ++i) {
60  const auto &key = reader.GetKey();
61  if (!end_key.empty() && end_key < key) break;
62  if (okey == key) {
63  ++nrep;
64  } else {
65  nrep = 0;
66  }
67  okey = key;
68  const auto *fst = reader.GetFst();
69  if (i == 1 && initial_symbols && !syms && fst->InputSymbols()) {
70  syms.reset(fst->InputSymbols()->Copy());
71  }
72  std::string str;
73  VLOG(2) << "Handling key: " << key;
74  const StringPrinter<Arc> printer(token_type,
75  syms ? syms.get() : fst->InputSymbols(),
76  /*omit_epsilon=*/false);
77  printer(*fst, &str);
78  if (entry_type == FarEntryType::LINE) {
79  if (print_key)
80  std::cout << key << FST_FLAGS_far_field_separator[0];
81  std::cout << str;
82  if (print_weight) {
83  std::cout << FST_FLAGS_far_field_separator[0]
84  << ShortestDistance(*fst);
85  }
86  std::cout << std::endl;
87  } else if (entry_type == FarEntryType::FILE) {
88  std::stringstream sstrm;
89  if (generate_sources) {
90  sstrm.fill('0');
91  sstrm << std::right << std::setw(generate_sources) << i;
92  } else {
93  sstrm << key;
94  if (nrep > 0) sstrm << "." << nrep;
95  }
96  std::string source;
97  source = source_prefix + sstrm.str() + source_suffix;
98  std::ofstream ostrm(source);
99  if (!ostrm) {
100  LOG(ERROR) << "PrintStrings: Can't open file: " << source;
101  return;
102  }
103  ostrm << str;
104  if (token_type == TokenType::SYMBOL) ostrm << "\n";
105  }
106  }
107 }
108 
109 } // namespace fst
110 
111 #endif // FST_EXTENSIONS_FAR_PRINT_STRINGS_H_
virtual const std::string & GetKey() const =0
virtual bool Done() const =0
#define LOG(type)
Definition: log.h:49
virtual const Fst< Arc > * GetFst() const =0
static SymbolTable * ReadText(std::istream &strm, std::string_view name, const SymbolTableTextOptions &opts=SymbolTableTextOptions())
Definition: symbol-table.h:377
#define VLOG(level)
Definition: log.h:50
TokenType
Definition: string.h:47
virtual void Next()=0
void ShortestDistance(const Fst< Arc > &fst, std::vector< typename Arc::Weight > *distance, const ShortestDistanceOptions< Arc, Queue, ArcFilter > &opts)
virtual bool Find(std::string_view key)=0
FarEntryType
Definition: far.h:41
void PrintStrings(FarReader< Arc > &reader, FarEntryType entry_type, TokenType token_type, const std::string &begin_key, const std::string &end_key, bool print_key, bool print_weight, const std::string &symbols_source, bool initial_symbols, int32_t generate_sources, const std::string &source_prefix, const std::string &source_suffix)
Definition: print-strings.h:39