12 bool *right_relabel_output) {
16 std::unique_ptr<SymbolTable> merged(
19 bool left_has_all =
true;
20 bool right_has_all =
true;
23 merged->AddSymbol(liter.Symbol(), liter.Value());
27 right_has_all =
false;
28 }
else if (!relabel && key != liter.Value()) {
34 if (right_relabel_output) *right_relabel_output = relabel;
38 std::vector<string> conflicts;
43 if (key != riter.Value()) relabel =
true;
48 if (!merged->Find(riter.Value()).empty()) {
50 conflicts.push_back(riter.Symbol());
54 merged->AddSymbol(riter.Symbol(), riter.Value());
56 if (right_relabel_output) *right_relabel_output = relabel;
57 if (left_has_all)
return left.
Copy();
59 for (
const auto &conflict : conflicts) merged->
AddSymbol(conflict);
60 return merged.release();
64 std::map<int64, string> sorted;
66 for (; !stiter.
Done(); stiter.
Next()) {
71 for (
const auto &kv : sorted) compact->
AddSymbol(kv.second, newkey++);
76 std::ifstream in(filename, std::ios_base::in | std::ios_base::binary);
78 LOG(ERROR) <<
"FstReadSymbols: Can't open file " << filename;
82 if (!hdr.
Read(in, filename)) {
83 LOG(ERROR) <<
"FstReadSymbols: Couldn't read header from " << filename;
88 if (isymbols ==
nullptr) {
89 LOG(ERROR) <<
"FstReadSymbols: Couldn't read input symbols from " 93 if (input_symbols)
return isymbols.release();
97 if (osymbols ==
nullptr) {
98 LOG(ERROR) <<
"FstReadSymbols: Couldn't read output symbols from " 102 if (!input_symbols)
return osymbols.release();
104 LOG(ERROR) <<
"FstReadSymbols: The file " << filename
105 <<
" doesn't contain the requested symbols";
111 for (
int64 i = 0; i < nlabels; ++i) {
112 auto index = i + start_label;
113 if (index != syms->
AddSymbol(prefix + std::to_string(i), index)) {
114 FSTERROR() <<
"AddAuxiliarySymbols: Symbol table clash";
virtual SymbolTable * Copy() const
SymbolTable * CompactSymbolTable(const SymbolTable &syms)
static SymbolTable * Read(std::istream &strm, const SymbolTableReadOptions &opts)
SymbolTable * FstReadSymbols(const string &filename, bool input)
bool AddAuxiliarySymbols(const string &prefix, int64 start_label, int64 nlabels, SymbolTable *syms)
virtual int64 AddSymbol(const string &symbol, int64 key)
virtual string Find(int64 key) const
virtual const string & Name() const
SymbolTable * MergeSymbolTable(const SymbolTable &left, const SymbolTable &right, bool *right_relabel_output=nullptr)