diff --git a/native_client/alphabet.h b/native_client/alphabet.h index cf34eebe68..812baedad4 100644 --- a/native_client/alphabet.h +++ b/native_client/alphabet.h @@ -6,6 +6,7 @@ #include #include #include +#include /* * Loads a text file describing a mapping of labels to strings, one string per @@ -17,29 +18,29 @@ class Alphabet { Alphabet(const char *config_file) { std::ifstream in(config_file, std::ios::in); unsigned int label = 0; + space_label_ = -2; for (std::string line; std::getline(in, line);) { if (line.size() == 2 && line[0] == '\\' && line[1] == '#') { line = '#'; } else if (line[0] == '#') { continue; } - label_to_str_[label] = line; + //TODO: we should probably do something more i18n-aware here + if (line == " ") { + space_label_ = label; + } + label_to_str_.push_back(line); str_to_label_[line] = label; ++label; } + label_to_str_.push_back("*"); size_ = label; in.close(); } const std::string& StringFromLabel(unsigned int label) const { assert(label < size_); - auto it = label_to_str_.find(label); - if (it != label_to_str_.end()) { - return it->second; - } else { - // unreachable due to assert above - abort(); - } + return label_to_str_[label]; } unsigned int LabelFromString(const std::string& string) const { @@ -52,19 +53,31 @@ class Alphabet { } } - size_t GetSize() { + size_t GetSize() const { return size_; } bool IsSpace(unsigned int label) const { - //TODO: we should probably do something more i18n-aware here - const std::string& str = StringFromLabel(label); - return str.size() == 1 && str[0] == ' '; + return label == space_label_; + } + + unsigned int GetSpaceLabel() const { + return space_label_; + } + + template + std::string LabelsToString(const std::vector& input) const { + std::string word; + for (auto ind : input) { + word += StringFromLabel(ind); + } + return word; } private: size_t size_; - std::unordered_map label_to_str_; + unsigned int space_label_; + std::vector label_to_str_; std::unordered_map str_to_label_; };