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 // Performs operations (set, clear, relabel) on the symbols table attached to an
19 // input FST.
20 
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include <fst/flags.h>
27 #include <fst/types.h>
28 #include <fst/util.h>
29 #include <fst/script/fst-class.h>
30 #include <fst/script/verify.h>
31 
32 DECLARE_string(isymbols);
33 DECLARE_string(osymbols);
34 DECLARE_bool(clear_isymbols);
35 DECLARE_bool(clear_osymbols);
36 DECLARE_string(relabel_ipairs);
37 DECLARE_string(relabel_opairs);
38 DECLARE_string(save_isymbols);
39 DECLARE_string(save_osymbols);
40 DECLARE_bool(allow_negative_labels);
41 DECLARE_bool(verify);
42 
fstsymbols_main(int argc,char ** argv)43 int fstsymbols_main(int argc, char **argv) {
44   namespace s = fst::script;
45   using fst::ReadLabelPairs;
46   using fst::SymbolTable;
47   using fst::SymbolTableTextOptions;
48   using fst::script::MutableFstClass;
49 
50   std::string usage =
51       "Performs operations (set, clear, relabel) on the symbol"
52       " tables attached to an FST.\n\n  Usage: ";
53   usage += argv[0];
54   usage += " [in.fst [out.fst]]\n";
55 
56   std::set_new_handler(FailedNewHandler);
57   SET_FLAGS(usage.c_str(), &argc, &argv, true);
58   if (argc > 3) {
59     ShowUsage();
60     return 1;
61   }
62 
63   const std::string in_name =
64       argc > 1 && strcmp(argv[1], "-") != 0 ? argv[1] : "";
65   const std::string out_name =
66       argc > 2 && strcmp(argv[2], "-") != 0 ? argv[2] : "";
67 
68   std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
69   if (!fst) return 1;
70 
71   if (!FST_FLAGS_save_isymbols.empty()) {
72     const auto *isyms = fst->InputSymbols();
73     if (isyms) {
74       isyms->WriteText(FST_FLAGS_save_isymbols);
75     } else {
76       LOG(ERROR) << argv[0]
77                  << ": Saving isymbols but there are no input symbols.";
78     }
79   }
80 
81   if (!FST_FLAGS_save_osymbols.empty()) {
82     const auto *osyms = fst->OutputSymbols();
83     if (osyms) {
84       osyms->WriteText(FST_FLAGS_save_osymbols);
85     } else {
86       LOG(ERROR) << argv[0]
87                  << ": Saving osymbols but there are no output symbols.";
88     }
89   }
90 
91   const SymbolTableTextOptions opts(FST_FLAGS_allow_negative_labels);
92 
93   std::unique_ptr<SymbolTable> isyms;
94   if (!FST_FLAGS_isymbols.empty()) {
95     isyms.reset(SymbolTable::ReadText(FST_FLAGS_isymbols, opts));
96     fst->SetInputSymbols(isyms.get());
97   } else if (FST_FLAGS_clear_isymbols) {
98     fst->SetInputSymbols(nullptr);
99   }
100   std::unique_ptr<SymbolTable> osyms;
101   if (!FST_FLAGS_osymbols.empty()) {
102     osyms.reset(SymbolTable::ReadText(FST_FLAGS_osymbols, opts));
103     fst->SetOutputSymbols(osyms.get());
104   } else if (FST_FLAGS_clear_osymbols) {
105     fst->SetOutputSymbols(nullptr);
106   }
107 
108   using Label = int64;
109   if (!FST_FLAGS_relabel_ipairs.empty()) {
110     std::vector<std::pair<Label, Label>> ipairs;
111     ReadLabelPairs(FST_FLAGS_relabel_ipairs, &ipairs,
112                    FST_FLAGS_allow_negative_labels);
113     std::unique_ptr<SymbolTable> isyms_relabel(
114         RelabelSymbolTable(fst->InputSymbols(), ipairs));
115     fst->SetInputSymbols(isyms_relabel.get());
116   }
117   if (!FST_FLAGS_relabel_opairs.empty()) {
118     std::vector<std::pair<Label, Label>> opairs;
119     ReadLabelPairs(FST_FLAGS_relabel_opairs, &opairs,
120                    FST_FLAGS_allow_negative_labels);
121     std::unique_ptr<SymbolTable> osyms_relabel(
122         RelabelSymbolTable(fst->OutputSymbols(), opairs));
123     fst->SetOutputSymbols(osyms_relabel.get());
124   }
125 
126   if (FST_FLAGS_verify && !s::Verify(*fst)) return 1;
127 
128   return !fst->Write(out_name);
129 }
130