diff options
author | Ian Hodson <idh@google.com> | 2012-05-30 21:27:06 +0100 |
---|---|---|
committer | Ian Hodson <idh@google.com> | 2012-05-30 22:47:36 +0100 |
commit | f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2 (patch) | |
tree | b131ed907f9b2d5af09c0983b651e9e69bc6aab9 /src/include/fst/symbol-table-ops.h | |
parent | a92766f0a6ba4fac46cd6fd3856ef20c3b204f0d (diff) | |
download | openfst-jb-mr1-dev.tar.gz |
Add openfst to external, as used by GoogleTTSandroid-sdk-support_r11android-cts-4.2_r2android-cts-4.2_r1android-cts-4.1_r4android-cts-4.1_r2android-cts-4.1_r1android-4.2_r1android-4.2.2_r1.2android-4.2.2_r1.1android-4.2.2_r1android-4.2.1_r1.2android-4.2.1_r1.1android-4.2.1_r1android-4.1.2_r2.1android-4.1.2_r2android-4.1.2_r1android-4.1.1_r6.1android-4.1.1_r6android-4.1.1_r5android-4.1.1_r4android-4.1.1_r3android-4.1.1_r2android-4.1.1_r1.1android-4.1.1_r1tools_r22tools_r21jb-releasejb-mr1.1-releasejb-mr1.1-dev-plus-aospjb-mr1.1-devjb-mr1-releasejb-mr1-dev-plus-aospjb-mr1-devjb-mr0-releasejb-dev
Moved from GoogleTTS
Change-Id: I6bc6bdadaa53bd0f810b88443339f6d899502cc8
Diffstat (limited to 'src/include/fst/symbol-table-ops.h')
-rw-r--r-- | src/include/fst/symbol-table-ops.h | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/include/fst/symbol-table-ops.h b/src/include/fst/symbol-table-ops.h new file mode 100644 index 0000000..e46c4c2 --- /dev/null +++ b/src/include/fst/symbol-table-ops.h @@ -0,0 +1,91 @@ + +// 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: sorenj@google.com (Jeffrey Sorensen) + +#ifndef FST_LIB_SYMBOL_TABLE_OPS_H_ +#define FST_LIB_SYMBOL_TABLE_OPS_H_ + +#include <vector> +using std::vector; +#include <string> +#include <unordered_set> +using std::tr1::unordered_set; +using std::tr1::unordered_multiset; + + +#include <fst/fst.h> +#include <fst/symbol-table.h> + + +namespace fst { + +// Returns a minimal symbol table containing only symbols referenced by the +// passed fst. Symbols preserve their original numbering, so fst does not +// require relabeling. +template<class Arc> +SymbolTable *PruneSymbolTable(const Fst<Arc> &fst, const SymbolTable &syms, + bool input) { + unordered_set<typename Arc::Label> seen; + seen.insert(0); // Always keep epslion + StateIterator<Fst<Arc> > siter(fst); + for (; !siter.Done(); siter.Next()) { + ArcIterator<Fst<Arc> > aiter(fst, siter.Value()); + for (; !aiter.Done(); aiter.Next()) { + typename Arc::Label sym = (input) ? aiter.Value().ilabel : + aiter.Value().olabel; + seen.insert(sym); + } + } + SymbolTable *pruned = new SymbolTable(syms.Name() + "_pruned"); + for (SymbolTableIterator stiter(syms); !stiter.Done(); stiter.Next()) { + typename Arc::Label label = stiter.Value(); + if (seen.find(label) != seen.end()) { + pruned->AddSymbol(stiter.Symbol(), stiter.Value()); + } + } + return pruned; +} + +// Relabels a symbol table to make it a contiguous mapping. +SymbolTable *CompactSymbolTable(const SymbolTable &syms); + +// Merges two SymbolTables, all symbols from left will be merged into right +// with the same ids. Symbols in right that have conflicting ids with those +// in left will be assigned to value assigned from the left SymbolTable. +// The returned symbol table will never modify symbol assignments from the left +// side, but may do so on the right. If right_relabel_output is non-NULL, it +// will be assigned true if the symbols from the right table needed to be +// reassigned. +// A potential use case is to Compose two Fst's that have different symbol +// tables. You can reconcile them in the following way: +// Fst<Arc> a, b; +// bool relabel; +// SymbolTable *bnew = MergeSymbolTable(a.OutputSymbols(), +// b.InputSymbols(), &relabel); +// if (relabel) { +// Relabel(b, bnew, NULL); +// } +// b.SetInputSymbols(bnew); +// delete bnew; +SymbolTable *MergeSymbolTable(const SymbolTable &left, const SymbolTable &right, + bool *right_relabel_output = 0); + +// Read the symbol table from any Fst::Read()able file, without loading the +// corresponding Fst. Returns NULL if the Fst does not contain a symbol table +// or the symbol table cannot be read. +SymbolTable *FstReadSymbols(const string &filename, bool input); + +} // namespace fst +#endif // FST_LIB_SYMBOL_TABLE_OPS_H_ |