/************************************************************************************************** * ZipVoice AXERA C++ Port * * Tokenizer: loads tokens.txt and maps token strings to IDs. * * For board deployment, tokenization should ideally be done on a host machine * and token IDs passed directly. This class provides basic tokenization * capability for convenience. **************************************************************************************************/ #pragma once #include #include #include #include class Tokenizer { public: Tokenizer(); /** * Load tokens.txt file. Format: token\tid per line. */ int Load(const std::string& token_file); /** * Convert a single text to token IDs using simplified rules. * For production use, pre-compute token IDs with the Python tokenizer. */ std::vector TextToTokenIds(const std::string& text) const; /** * Convert multiple texts to token IDs. */ std::vector> TextsToTokenIds(const std::vector& texts) const; /** * Get the pad token ID ("_" token). */ int GetPadId() const { return m_pad_id; } /** * Get vocabulary size. */ int GetVocabSize() const { return static_cast(m_token2id.size()); } /** * Lookup a single token. Returns pad_id if not found. */ int TokenToId(const std::string& token) const; /** * Build concatenated token array [prompt_tokens + text_tokens + pad] padded to max_tokens. */ void BuildCatTokens(const std::vector& prompt_tokens, const std::vector& text_tokens, int max_tokens, std::vector& out_cat_tokens) const; bool IsLoaded() const { return m_loaded; } private: std::unordered_map m_token2id; std::vector m_id2token; int m_pad_id; bool m_loaded; // Simplified text→tokens for Chinese (char-by-char pinyin-like tokens) std::vector TokenizeZh(const std::string& text) const; // Simplified text→tokens for English (basic phoneme rules) std::vector TokenizeEn(const std::string& text) const; // Split text into language segments std::vector> GetSegments(const std::string& text) const; static bool IsChinese(char c); static bool IsAlphabet(char c); static std::string MapPunctuation(const std::string& text); };