• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // fstrelabel.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: johans@google.com (Johan Schalkwyk)
17 // Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18 //
19 // \file
20 // Relabel input or output space of Fst
21 //
22 
23 #include <string>
24 #include <vector>
25 using std::vector;
26 #include <utility>
27 using std::pair; using std::make_pair;
28 
29 #include <fst/script/relabel.h>
30 #include <fst/script/weight-class.h>
31 #include <fst/util.h>
32 
33 DEFINE_string(isymbols, "", "Input label symbol table");
34 DEFINE_string(osymbols, "", "Output label symbol table");
35 DEFINE_string(relabel_isymbols, "", "Input symbol set to relabel to");
36 DEFINE_string(relabel_osymbols, "", "Ouput symbol set to relabel to");
37 DEFINE_string(relabel_ipairs, "", "Input relabel pairs (numeric)");
38 DEFINE_string(relabel_opairs, "", "Output relabel pairs (numeric)");
39 
40 DEFINE_bool(allow_negative_labels, false,
41             "Allow negative labels (not recommended; may cause conflicts)");
42 
main(int argc,char ** argv)43 int main(int argc, char **argv) {
44   namespace s = fst::script;
45   using fst::SymbolTable;
46   using fst::script::FstClass;
47   using fst::script::MutableFstClass;
48 
49   string usage = "Relabels the input and/or the output labels of the FST.\n\n"
50       "  Usage: ";
51   usage += argv[0];
52   usage += " [in.fst [out.fst]]\n";
53   usage += " Using SymbolTables flags:\n";
54   usage += "  -relabel_isymbols isyms.txt\n";
55   usage += "  -relabel_osymbols osyms.txt\n";
56   usage += " Using numeric labels flags:\n";
57   usage += "  -relabel_ipairs   ipairs.txt\n";
58   usage += "  -relabel_opairs   opairs.txts\n";
59 
60   std::set_new_handler(FailedNewHandler);
61   SetFlags(usage.c_str(), &argc, &argv, true);
62   if (argc > 3) {
63     ShowUsage();
64     return 1;
65   }
66 
67   string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
68   string out_name = argc > 2 ? argv[2] : "";
69 
70   MutableFstClass *fst = MutableFstClass::Read(in_name, true);
71   if (!fst) return 1;
72 
73   // Relabel with symbol tables
74   if (!FLAGS_relabel_isymbols.empty() || !FLAGS_relabel_osymbols.empty()) {
75     bool attach_new_isymbols = (fst->InputSymbols() != 0);
76     const SymbolTable* old_isymbols = FLAGS_isymbols.empty()
77         ? fst->InputSymbols()
78         : SymbolTable::ReadText(FLAGS_isymbols, FLAGS_allow_negative_labels);
79     const SymbolTable* relabel_isymbols = FLAGS_relabel_isymbols.empty()
80         ? NULL
81         : SymbolTable::ReadText(FLAGS_relabel_isymbols,
82                                 FLAGS_allow_negative_labels);
83 
84     bool attach_new_osymbols = (fst->OutputSymbols() != 0);
85     const SymbolTable* old_osymbols = FLAGS_osymbols.empty()
86         ? fst->OutputSymbols()
87         : SymbolTable::ReadText(FLAGS_osymbols, FLAGS_allow_negative_labels);
88     const SymbolTable* relabel_osymbols = FLAGS_relabel_osymbols.empty()
89         ? NULL
90         : SymbolTable::ReadText(FLAGS_relabel_osymbols,
91                                 FLAGS_allow_negative_labels);
92 
93     s::Relabel(fst,
94                old_isymbols, relabel_isymbols, attach_new_isymbols,
95                old_osymbols, relabel_osymbols, attach_new_osymbols);
96   } else {
97     // read in relabel pairs and parse
98     typedef int64 Label;
99     vector<pair<Label, Label> > ipairs;
100     vector<pair<Label, Label> > opairs;
101     if (!FLAGS_relabel_ipairs.empty()) {
102       if(!fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
103                                   FLAGS_allow_negative_labels))
104         return 1;
105     }
106     if (!FLAGS_relabel_opairs.empty()) {
107       if (!fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
108                                    FLAGS_allow_negative_labels))
109         return 1;
110     }
111     s::Relabel(fst, ipairs, opairs);
112   }
113 
114   fst->Write(out_name);
115 
116   return 0;
117 }
118