1 // fstsymbols.cc
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: allauzen@google.com (Cyril Allauzen)
17 // Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18 //
19 // \file
20 // Performs operations (set, clear, relabel) on the symbols table
21 // attached to the input Fst.
22 //
23
24 #include <fst/script/fst-class.h>
25 #include <fst/script/script-impl.h>
26 #include <fst/script/verify.h>
27 #include <fst/util.h>
28
29 DEFINE_string(isymbols, "", "Input label symbol table");
30 DEFINE_string(osymbols, "", "Output label symbol table");
31 DEFINE_bool(clear_isymbols, false, "Clear input symbol table");
32 DEFINE_bool(clear_osymbols, false, "Clear output symbol table");
33 DEFINE_string(relabel_ipairs, "", "Input relabel pairs (numeric)");
34 DEFINE_string(relabel_opairs, "", "Output relabel pairs (numeric)");
35 DEFINE_string(save_isymbols, "", "Save fst file's input symbol table to file");
36 DEFINE_string(save_osymbols, "", "Save fst file's output symbol table to file");
37 DEFINE_bool(allow_negative_labels, false,
38 "Allow negative labels (not recommended; may cause conflicts)");
39 DEFINE_bool(verify, false, "Verify fst properities before saving");
40
main(int argc,char ** argv)41 int main(int argc, char **argv) {
42 namespace s = fst::script;
43 using fst::SymbolTable;
44
45 string usage = "Performs operations (set, clear, relabel) on the symbol"
46 " tables attached to an FST.\n\n Usage: ";
47 usage += argv[0];
48 usage += " [in.fst [out.fst]]\n";
49
50 std::set_new_handler(FailedNewHandler);
51 SetFlags(usage.c_str(), &argc, &argv, true);
52 if (argc > 3) {
53 ShowUsage();
54 return 1;
55 }
56
57 string in_fname = argc > 1 && strcmp(argv[1], "-") != 0 ? argv[1] : "";
58 string out_fname = argc > 2 ? argv[2] : "";
59
60 s::MutableFstClass *fst = s::MutableFstClass::Read(in_fname, true);
61 if (!fst) return 1;
62
63 if (!FLAGS_save_isymbols.empty()) {
64 const SymbolTable *isyms = fst->InputSymbols();
65 if (isyms) {
66 isyms->WriteText(FLAGS_save_isymbols);
67 } else {
68 LOG(ERROR) << "save isymbols requested but there are no input symbols.";
69 }
70 }
71
72 if (!FLAGS_save_osymbols.empty()) {
73 const SymbolTable *osyms = fst->OutputSymbols();
74 if (osyms) {
75 osyms->WriteText(FLAGS_save_osymbols);
76 } else {
77 LOG(ERROR) << "save osymbols requested but there are no output symbols.";
78 }
79 }
80
81 if (FLAGS_clear_isymbols)
82 fst->SetInputSymbols(0);
83 else if (!FLAGS_isymbols.empty())
84 fst->SetInputSymbols(
85 SymbolTable::ReadText(FLAGS_isymbols, FLAGS_allow_negative_labels));
86
87 if (FLAGS_clear_osymbols)
88 fst->SetOutputSymbols(0);
89 else if (!FLAGS_osymbols.empty())
90 fst->SetOutputSymbols(
91 SymbolTable::ReadText(FLAGS_osymbols, FLAGS_allow_negative_labels));
92
93 if (!FLAGS_relabel_ipairs.empty()) {
94 typedef int64 Label;
95 vector<pair<Label, Label> > ipairs;
96 fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
97 FLAGS_allow_negative_labels);
98 SymbolTable *isyms = RelabelSymbolTable(fst->InputSymbols(), ipairs);
99 fst->SetInputSymbols(isyms);
100 delete isyms;
101 }
102
103 if (!FLAGS_relabel_opairs.empty()) {
104 typedef int64 Label;
105 vector<pair<Label, Label> > opairs;
106 fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
107 FLAGS_allow_negative_labels);
108 SymbolTable *osyms = RelabelSymbolTable(fst->OutputSymbols(), opairs);
109 fst->SetOutputSymbols(osyms);
110 delete osyms;
111 }
112
113 if (FLAGS_verify && !s::Verify(*fst))
114 return 1;
115 fst->Write(out_fname);
116 return 0;
117 }
118