pulsatrix
Loading...
Searching...
No Matches
vocabulary.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12namespace pulsatrix {
13
20public:
21 static constexpr int64_t kUnkIndex = 0;
22 static constexpr const char* kUnkToken = "<unk>";
23
25 explicit Vocabulary(std::vector<std::string> ranked_tokens);
26
28 [[nodiscard]] int64_t IndexOf(const std::string& token) const;
29
37 [[nodiscard]] const std::string& TokenAt(int64_t index) const;
38
40 [[nodiscard]] int64_t size() const { return static_cast<int64_t>(tokens_.size()); }
41
42private:
43 std::vector<std::string> tokens_;
44 std::unordered_map<std::string, int64_t> token_to_index_;
45};
46
58[[nodiscard]] Vocabulary BuildVocabulary(const std::vector<std::vector<std::string>>& tokenized_corpus,
59 int64_t max_vocab_size = -1);
60
61} // namespace pulsatrix
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...