| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #pragma once |
|
|
| #include <string> |
| #include <vector> |
| #include <unordered_map> |
| #include <cstdint> |
|
|
| class Tokenizer { |
| public: |
| Tokenizer(); |
|
|
| |
| |
| |
| int Load(const std::string& token_file); |
|
|
| |
| |
| |
| |
| std::vector<int> TextToTokenIds(const std::string& text) const; |
|
|
| |
| |
| |
| std::vector<std::vector<int>> TextsToTokenIds(const std::vector<std::string>& texts) const; |
|
|
| |
| |
| |
| int GetPadId() const { return m_pad_id; } |
|
|
| |
| |
| |
| int GetVocabSize() const { return static_cast<int>(m_token2id.size()); } |
|
|
| |
| |
| |
| int TokenToId(const std::string& token) const; |
|
|
| |
| |
| |
| void BuildCatTokens(const std::vector<int>& prompt_tokens, |
| const std::vector<int>& text_tokens, |
| int max_tokens, |
| std::vector<int32_t>& out_cat_tokens) const; |
|
|
| bool IsLoaded() const { return m_loaded; } |
|
|
| private: |
| std::unordered_map<std::string, int> m_token2id; |
| std::vector<std::string> m_id2token; |
| int m_pad_id; |
| bool m_loaded; |
|
|
| |
| std::vector<std::string> TokenizeZh(const std::string& text) const; |
| |
| std::vector<std::string> TokenizeEn(const std::string& text) const; |
| |
| std::vector<std::pair<std::string, std::string>> GetSegments(const std::string& text) const; |
|
|
| static bool IsChinese(char c); |
| static bool IsAlphabet(char c); |
| static std::string MapPunctuation(const std::string& text); |
| }; |
|
|