9#include <unordered_map>
25 explicit Vocabulary(std::vector<std::string> ranked_tokens);
28 [[nodiscard]] int64_t
IndexOf(
const std::string& token)
const;
37 [[nodiscard]]
const std::string&
TokenAt(int64_t index)
const;
40 [[nodiscard]] int64_t
size()
const {
return static_cast<int64_t
>(tokens_.size()); }
43 std::vector<std::string> tokens_;
44 std::unordered_map<std::string, int64_t> token_to_index_;
59 int64_t max_vocab_size = -1);
Token<->index lookup table. Index 0 is always the reserved "<unk>" token – guaranteed by construction...
Definition vocabulary.hpp:19
Vocabulary(std::vector< std::string > ranked_tokens)
int64_t size() const
Total token count, including the reserved <unk> at index 0.
Definition vocabulary.hpp:40
const std::string & TokenAt(int64_t index) const
The token at a given index.
int64_t IndexOf(const std::string &token) const
The token's index, or kUnkIndex if the token isn't in this vocabulary.
static constexpr int64_t kUnkIndex
Definition vocabulary.hpp:21
static constexpr const char * kUnkToken
Definition vocabulary.hpp:22
Definition acquisition_functions.hpp:16
Vocabulary BuildVocabulary(const std::vector< std::vector< std::string > > &tokenized_corpus, int64_t max_vocab_size=-1)
Builds a Vocabulary from a tokenized corpus, ranked by descending token frequency (ties broken by fir...