aboutsummaryrefslogtreecommitdiff
path: root/src/bin/fstrelabel.cc
diff options
context:
space:
mode:
authorIan Hodson <idh@google.com>2012-05-30 21:27:06 +0100
committerIan Hodson <idh@google.com>2012-05-30 22:47:36 +0100
commitf4c12fce1ee58e670f9c3fce46c40296ba9ee8a2 (patch)
treeb131ed907f9b2d5af09c0983b651e9e69bc6aab9 /src/bin/fstrelabel.cc
parenta92766f0a6ba4fac46cd6fd3856ef20c3b204f0d (diff)
downloadopenfst-f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2.tar.gz
Moved from GoogleTTS Change-Id: I6bc6bdadaa53bd0f810b88443339f6d899502cc8
Diffstat (limited to 'src/bin/fstrelabel.cc')
-rw-r--r--src/bin/fstrelabel.cc117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/bin/fstrelabel.cc b/src/bin/fstrelabel.cc
new file mode 100644
index 0000000..cc86450
--- /dev/null
+++ b/src/bin/fstrelabel.cc
@@ -0,0 +1,117 @@
+// fstrelabel.cc
+
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Copyright 2005-2010 Google, Inc.
+// Author: johans@google.com (Johan Schalkwyk)
+// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
+//
+// \file
+// Relabel input or output space of Fst
+//
+
+#include <string>
+#include <vector>
+using std::vector;
+#include <utility>
+using std::pair; using std::make_pair;
+
+#include <fst/script/relabel.h>
+#include <fst/script/weight-class.h>
+#include <fst/util.h>
+
+DEFINE_string(isymbols, "", "Input label symbol table");
+DEFINE_string(osymbols, "", "Output label symbol table");
+DEFINE_string(relabel_isymbols, "", "Input symbol set to relabel to");
+DEFINE_string(relabel_osymbols, "", "Ouput symbol set to relabel to");
+DEFINE_string(relabel_ipairs, "", "Input relabel pairs (numeric)");
+DEFINE_string(relabel_opairs, "", "Output relabel pairs (numeric)");
+
+DEFINE_bool(allow_negative_labels, false,
+ "Allow negative labels (not recommended; may cause conflicts)");
+
+int main(int argc, char **argv) {
+ namespace s = fst::script;
+ using fst::SymbolTable;
+ using fst::script::FstClass;
+ using fst::script::MutableFstClass;
+
+ string usage = "Relabels the input and/or the output labels of the FST.\n\n"
+ " Usage: ";
+ usage += argv[0];
+ usage += " [in.fst [out.fst]]\n";
+ usage += " Using SymbolTables flags:\n";
+ usage += " -relabel_isymbols isyms.txt\n";
+ usage += " -relabel_osymbols osyms.txt\n";
+ usage += " Using numeric labels flags:\n";
+ usage += " -relabel_ipairs ipairs.txt\n";
+ usage += " -relabel_opairs opairs.txts\n";
+
+ std::set_new_handler(FailedNewHandler);
+ SetFlags(usage.c_str(), &argc, &argv, true);
+ if (argc > 3) {
+ ShowUsage();
+ return 1;
+ }
+
+ string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
+ string out_name = argc > 2 ? argv[2] : "";
+
+ MutableFstClass *fst = MutableFstClass::Read(in_name, true);
+ if (!fst) return 1;
+
+ // Relabel with symbol tables
+ if (!FLAGS_relabel_isymbols.empty() || !FLAGS_relabel_osymbols.empty()) {
+ bool attach_new_isymbols = (fst->InputSymbols() != 0);
+ const SymbolTable* old_isymbols = FLAGS_isymbols.empty()
+ ? fst->InputSymbols()
+ : SymbolTable::ReadText(FLAGS_isymbols, FLAGS_allow_negative_labels);
+ const SymbolTable* relabel_isymbols = FLAGS_relabel_isymbols.empty()
+ ? NULL
+ : SymbolTable::ReadText(FLAGS_relabel_isymbols,
+ FLAGS_allow_negative_labels);
+
+ bool attach_new_osymbols = (fst->OutputSymbols() != 0);
+ const SymbolTable* old_osymbols = FLAGS_osymbols.empty()
+ ? fst->OutputSymbols()
+ : SymbolTable::ReadText(FLAGS_osymbols, FLAGS_allow_negative_labels);
+ const SymbolTable* relabel_osymbols = FLAGS_relabel_osymbols.empty()
+ ? NULL
+ : SymbolTable::ReadText(FLAGS_relabel_osymbols,
+ FLAGS_allow_negative_labels);
+
+ s::Relabel(fst,
+ old_isymbols, relabel_isymbols, attach_new_isymbols,
+ old_osymbols, relabel_osymbols, attach_new_osymbols);
+ } else {
+ // read in relabel pairs and parse
+ typedef int64 Label;
+ vector<pair<Label, Label> > ipairs;
+ vector<pair<Label, Label> > opairs;
+ if (!FLAGS_relabel_ipairs.empty()) {
+ if(!fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
+ FLAGS_allow_negative_labels))
+ return 1;
+ }
+ if (!FLAGS_relabel_opairs.empty()) {
+ if (!fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
+ FLAGS_allow_negative_labels))
+ return 1;
+ }
+ s::Relabel(fst, ipairs, opairs);
+ }
+
+ fst->Write(out_name);
+
+ return 0;
+}