11#pragma once
2-
32#include < string>
43#include < vector>
54#include < fstream>
98#include < cassert>
109#include < iostream>
1110#include " ../../../../../SDK/components/utilities/include/sample_log.h"
12-
11+ // Debug logging switch - set to true to enable debug logs
12+ static bool DEBUG_LOGGING = false ;
13+ // Macro for debug logging
14+ #define DEBUG_LOG (fmt, ...) \
15+ do { \
16+ if (DEBUG_LOGGING) { \
17+ SLOGI (fmt, ##__VA_ARGS__); \
18+ } \
19+ } while (0 )
1320std::vector<std::string> split (const std::string& s, char delim)
1421{
1522 std::vector<std::string> result;
@@ -30,8 +37,16 @@ class Lexicon {
3037 std::unordered_map<int , std::string> reverse_tokens;
3138
3239public:
40+ // Setter for debug logging
41+ static void setDebugLogging (bool enable)
42+ {
43+ DEBUG_LOGGING = enable;
44+ }
3345 Lexicon (const std::string& lexicon_filename, const std::string& tokens_filename) : max_phrase_length(0 )
3446 {
47+ DEBUG_LOG (" Dictionary loading: %s Pronunciation table loading: %s" , tokens_filename.c_str (),
48+ lexicon_filename.c_str ());
49+
3550 std::unordered_map<std::string, int > tokens;
3651 std::ifstream ifs (tokens_filename);
3752 assert (ifs.is_open ());
@@ -82,8 +97,10 @@ class Lexicon {
8297 lexicon[" 。" ] = lexicon[" ." ];
8398 lexicon[" !" ] = lexicon[" !" ];
8499 lexicon[" ?" ] = lexicon[" ?" ];
85- SLOGI (" 词典加载完成,包含 %zu 个条目,最长词组长度: %zu" , lexicon.size (), max_phrase_length);
100+ DEBUG_LOG (" Dictionary loading complete, containing %zu entries, longest phrase length: %zu" , lexicon.size (),
101+ max_phrase_length);
86102 }
103+
87104 std::vector<std::string> splitEachChar (const std::string& text)
88105 {
89106 std::vector<std::string> words;
@@ -94,93 +111,77 @@ class Lexicon {
94111 if ((text[i] & 0x80 ) == 0x00 ) {
95112 // ASCII
96113 } else if ((text[i] & 0xE0 ) == 0xC0 ) {
97- next = 2 ; // 2字节UTF -8
114+ next = 2 ; // 2-byte UTF -8
98115 } else if ((text[i] & 0xF0 ) == 0xE0 ) {
99- next = 3 ; // 3字节UTF -8
116+ next = 3 ; // 3-byte UTF -8
100117 } else if ((text[i] & 0xF8 ) == 0xF0 ) {
101- next = 4 ; // 4字节UTF -8
118+ next = 4 ; // 4-byte UTF -8
102119 }
103120 words.push_back (text.substr (i, next));
104121 i += next;
105122 }
106123 return words;
107124 }
125+
108126 bool is_english (const std::string& s)
109127 {
110128 return s.size () == 1 && ((s[0 ] >= ' A' && s[0 ] <= ' Z' ) || (s[0 ] >= ' a' && s[0 ] <= ' z' ));
111129 }
112-
113130 bool is_english_token_char (const std::string& s)
114131 {
115132 if (s.size () != 1 ) return false ;
116133 char c = s[0 ];
117134 return (c >= ' A' && c <= ' Z' ) || (c >= ' a' && c <= ' z' ) || (c >= ' 0' && c <= ' 9' ) || c == ' -' || c == ' _' ;
118135 }
119-
120136 void process_unknown_english (const std::string& word, std::vector<int >& phones, std::vector<int >& tones)
121137 {
122- SLOGI (" Processing unknown term: %s" , word.c_str ());
123-
138+ DEBUG_LOG (" Processing unknown term: %s" , word.c_str ());
124139 std::string orig_word = word;
125140 std::vector<std::string> parts;
126141 std::vector<std::string> phonetic_parts;
127-
128142 size_t start = 0 ;
129143 while (start < word.size ()) {
130144 bool matched = false ;
131-
132145 for (size_t len = std::min (word.size () - start, (size_t )10 ); len > 0 && !matched; --len) {
133146 std::string sub_word = word.substr (start, len);
134147 std::string lower_sub_word = sub_word;
135148 std::transform (lower_sub_word.begin (), lower_sub_word.end (), lower_sub_word.begin (),
136149 [](unsigned char c) { return std::tolower (c); });
137-
138150 if (lexicon.find (lower_sub_word) != lexicon.end ()) {
139151 // Substring found in lexicon
140152 auto & [sub_phones, sub_tones] = lexicon[lower_sub_word];
141153 phones.insert (phones.end (), sub_phones.begin (), sub_phones.end ());
142154 tones.insert (tones.end (), sub_tones.begin (), sub_tones.end ());
143-
144155 parts.push_back (sub_word);
145156 phonetic_parts.push_back (phonesToString (sub_phones));
146-
147- SLOGI (" Matched: '%s' -> %s" , sub_word.c_str (), phonesToString (sub_phones).c_str ());
148-
157+ DEBUG_LOG (" Matched: '%s' -> %s" , sub_word.c_str (), phonesToString (sub_phones).c_str ());
149158 start += len;
150159 matched = true ;
151160 break ;
152161 }
153162 }
154-
155163 if (!matched) {
156164 std::string single_char = word.substr (start, 1 );
157165 std::string lower_char = single_char;
158166 std::transform (lower_char.begin (), lower_char.end (), lower_char.begin (),
159167 [](unsigned char c) { return std::tolower (c); });
160-
161168 if (lexicon.find (lower_char) != lexicon.end ()) {
162169 auto & [char_phones, char_tones] = lexicon[lower_char];
163170 phones.insert (phones.end (), char_phones.begin (), char_phones.end ());
164171 tones.insert (tones.end (), char_tones.begin (), char_tones.end ());
165-
166172 parts.push_back (single_char);
167173 phonetic_parts.push_back (phonesToString (char_phones));
168-
169- SLOGI (" Single char: '%s' -> %s" , single_char.c_str (), phonesToString (char_phones).c_str ());
174+ DEBUG_LOG (" Single char: '%s' -> %s" , single_char.c_str (), phonesToString (char_phones).c_str ());
170175 } else {
171176 phones.insert (phones.end (), unknown_token.first .begin (), unknown_token.first .end ());
172177 tones.insert (tones.end (), unknown_token.second .begin (), unknown_token.second .end ());
173-
174178 parts.push_back (single_char);
175179 phonetic_parts.push_back (" _unknown_" );
176-
177- SLOGI (" Unknown: '%s'" , single_char.c_str ());
180+ DEBUG_LOG (" Unknown: '%s'" , single_char.c_str ());
178181 }
179-
180182 start++;
181183 }
182184 }
183-
184185 std::string parts_str, phonetic_str;
185186 for (size_t i = 0 ; i < parts.size (); i++) {
186187 if (i > 0 ) {
@@ -190,20 +191,20 @@ class Lexicon {
190191 parts_str += parts[i];
191192 phonetic_str += phonetic_parts[i];
192193 }
193-
194- SLOGI ( " %s \t | \t Decomposed: %s \t | \t Phonetics: %s " , orig_word. c_str (), parts_str. c_str (), phonetic_str.c_str ());
194+ DEBUG_LOG ( " %s \t | \t Decomposed: %s \t | \t Phonetics: %s " , orig_word. c_str (), parts_str. c_str (),
195+ phonetic_str.c_str ());
195196 }
197+
196198 void convert (const std::string& text, std::vector<int >& phones, std::vector<int >& tones)
197199 {
198- SLOGI (" \n 开始处理文本 : \" %s\" " , text.c_str ());
199- SLOGI (" =======匹配结果 =======" );
200- SLOGI ( " 单元 \t |\t 音素 \t |\t 声调 " );
201- SLOGI (" -----------------------------" );
200+ DEBUG_LOG (" \n Starting text processing : \" %s\" " , text.c_str ());
201+ DEBUG_LOG (" =======Matching Results =======" );
202+ DEBUG_LOG ( " Unit \t |\t Phonemes \t |\t Tones " );
203+ DEBUG_LOG (" -----------------------------" );
202204 phones.insert (phones.end (), unknown_token.first .begin (), unknown_token.first .end ());
203205 tones.insert (tones.end (), unknown_token.second .begin (), unknown_token.second .end ());
204-
205- SLOGI (" <BOS>\t |\t %s\t |\t %s" , phonesToString (unknown_token.first ).c_str (),
206- tonesToString (unknown_token.second ).c_str ());
206+ DEBUG_LOG (" <BOS>\t |\t %s\t |\t %s" , phonesToString (unknown_token.first ).c_str (),
207+ tonesToString (unknown_token.second ).c_str ());
207208 auto chars = splitEachChar (text);
208209 int i = 0 ;
209210 while (i < chars.size ()) {
@@ -220,8 +221,8 @@ class Lexicon {
220221 auto & [eng_phones, eng_tones] = lexicon[eng_word];
221222 phones.insert (phones.end (), eng_phones.begin (), eng_phones.end ());
222223 tones.insert (tones.end (), eng_tones.begin (), eng_tones.end ());
223- SLOGI (" %s\t |\t %s\t |\t %s" , orig_word.c_str (), phonesToString (eng_phones).c_str (),
224- tonesToString (eng_tones).c_str ());
224+ DEBUG_LOG (" %s\t |\t %s\t |\t %s" , orig_word.c_str (), phonesToString (eng_phones).c_str (),
225+ tonesToString (eng_tones).c_str ());
225226 } else {
226227 process_unknown_english (orig_word, phones, tones);
227228 }
@@ -240,8 +241,8 @@ class Lexicon {
240241 auto & [phrase_phones, phrase_tones] = lexicon[phrase];
241242 phones.insert (phones.end (), phrase_phones.begin (), phrase_phones.end ());
242243 tones.insert (tones.end (), phrase_tones.begin (), phrase_tones.end ());
243- SLOGI (" %s\t |\t %s\t |\t %s" , phrase.c_str (), phonesToString (phrase_phones).c_str (),
244- tonesToString (phrase_tones).c_str ());
244+ DEBUG_LOG (" %s\t |\t %s\t |\t %s" , phrase.c_str (), phonesToString (phrase_phones).c_str (),
245+ tonesToString (phrase_tones).c_str ());
245246 i += len;
246247 matched = true ;
247248 break ;
@@ -263,25 +264,25 @@ class Lexicon {
263264 auto & [char_phones, char_tones] = lexicon[s];
264265 phones.insert (phones.end (), char_phones.begin (), char_phones.end ());
265266 tones.insert (tones.end (), char_tones.begin (), char_tones.end ());
266- SLOGI (" %s\t |\t %s\t |\t %s" , orig_char.c_str (), phonesToString (char_phones).c_str (),
267- tonesToString (char_tones).c_str ());
267+ DEBUG_LOG (" %s\t |\t %s\t |\t %s" , orig_char.c_str (), phonesToString (char_phones).c_str (),
268+ tonesToString (char_tones).c_str ());
268269 } else {
269270 phones.insert (phones.end (), unknown_token.first .begin (), unknown_token.first .end ());
270271 tones.insert (tones.end (), unknown_token.second .begin (), unknown_token.second .end ());
271- SLOGI (" %s\t |\t %s (未匹配 )\t |\t %s" , orig_char. c_str (), phonesToString (unknown_token. first ) .c_str (),
272- tonesToString (unknown_token.second ).c_str ());
272+ DEBUG_LOG (" %s\t |\t %s (Not matched )\t |\t %s" , orig_char.c_str (),
273+ phonesToString (unknown_token. first ). c_str (), tonesToString (unknown_token.second ).c_str ());
273274 }
274275 }
275276 }
276277 phones.insert (phones.end (), unknown_token.first .begin (), unknown_token.first .end ());
277278 tones.insert (tones.end (), unknown_token.second .begin (), unknown_token.second .end ());
278- SLOGI (" <EOS>\t |\t %s\t |\t %s" , phonesToString (unknown_token.first ).c_str (),
279- tonesToString (unknown_token.second ).c_str ());
280- SLOGI (" \n 处理结果汇总 :" );
281- SLOGI ( " 原文 : %s" , text.c_str ());
282- SLOGI ( " 音素 : %s" , phonesToString (phones).c_str ());
283- SLOGI ( " 声调 : %s" , tonesToString (tones).c_str ());
284- SLOGI (" ====================" );
279+ DEBUG_LOG (" <EOS>\t |\t %s\t |\t %s" , phonesToString (unknown_token.first ).c_str (),
280+ tonesToString (unknown_token.second ).c_str ());
281+ DEBUG_LOG (" \n Processing Summary :" );
282+ DEBUG_LOG ( " Original text : %s" , text.c_str ());
283+ DEBUG_LOG ( " Phonemes : %s" , phonesToString (phones).c_str ());
284+ DEBUG_LOG ( " Tones : %s" , tonesToString (tones).c_str ());
285+ DEBUG_LOG (" ====================" );
285286 }
286287
287288private:
0 commit comments