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
29DEFINE_string(isymbols, "", "Input label symbol table");
30DEFINE_string(osymbols, "", "Output label symbol table");
31DEFINE_bool(clear_isymbols, false, "Clear input symbol table");
32DEFINE_bool(clear_osymbols, false, "Clear output symbol table");
33DEFINE_string(relabel_ipairs, "", "Input relabel pairs (numeric)");
34DEFINE_string(relabel_opairs, "", "Output relabel pairs (numeric)");
35DEFINE_string(save_isymbols, "", "Save fst file's input symbol table to file");
36DEFINE_string(save_osymbols, "", "Save fst file's output symbol table to file");
37DEFINE_bool(allow_negative_labels, false,
38            "Allow negative labels (not recommended; may cause conflicts)");
39DEFINE_bool(verify, false, "Verify fst properities before saving");
40
41int 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  SET_FLAGS(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  fst::SymbolTableTextOptions opts;
82  opts.allow_negative = FLAGS_allow_negative_labels;
83
84  if (FLAGS_clear_isymbols)
85    fst->SetInputSymbols(0);
86  else if (!FLAGS_isymbols.empty())
87    fst->SetInputSymbols(SymbolTable::ReadText(FLAGS_isymbols, opts));
88
89  if (FLAGS_clear_osymbols)
90    fst->SetOutputSymbols(0);
91  else if (!FLAGS_osymbols.empty())
92    fst->SetOutputSymbols(SymbolTable::ReadText(FLAGS_osymbols, opts));
93
94  if (!FLAGS_relabel_ipairs.empty()) {
95    typedef int64 Label;
96    vector<pair<Label, Label> > ipairs;
97    fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
98                            FLAGS_allow_negative_labels);
99    SymbolTable *isyms = RelabelSymbolTable(fst->InputSymbols(), ipairs);
100    fst->SetInputSymbols(isyms);
101    delete isyms;
102  }
103
104  if (!FLAGS_relabel_opairs.empty()) {
105    typedef int64 Label;
106    vector<pair<Label, Label> > opairs;
107    fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
108                            FLAGS_allow_negative_labels);
109    SymbolTable *osyms = RelabelSymbolTable(fst->OutputSymbols(), opairs);
110    fst->SetOutputSymbols(osyms);
111    delete osyms;
112  }
113
114  if (FLAGS_verify && !s::Verify(*fst))
115    return 1;
116  fst->Write(out_fname);
117  return 0;
118}
119