from pypinyin import pinyin, Style import argparse import re def chinese_to_pinyin(text): # Use regular expression to extract Chinese characters chinese_text = ''.join(re.findall(r'[\u4e00-\u9fa5]', text)) out = "static const sr_cmd_t sr_commands[] = {\n" enum = "enum {\n" cmd_id = 0 text_list = chinese_text.split(";") for item in text_list: item = item.strip() # Remove leading and trailing spaces if not item: continue item = item.split(",") phrase_id = 0 pinyin_list_all = [] for phrase in item: phrase = phrase.strip() # Remove leading and trailing spaces if not phrase: continue # Convert Chinese phrase to pinyin with spaces between each syllable pinyin_list = pinyin(phrase, style=Style.NORMAL, heteronym=False) # Join pinyin list with spaces phoneme = ' '.join([py[0] for py in pinyin_list]) out += f' {{ {cmd_id}, "{phoneme}", "{phoneme}" }},\n' if phrase_id == 0: enum += f' SR_CMD_{phoneme.upper().replace(" ", "_")},\n' phrase_id += 1 cmd_id += 1 out += "};\n" enum += "};\n" print(enum) print(out) return out if __name__ == "__main__": parser = argparse.ArgumentParser(prog="Chinese Speech Commands to Pinyin") parser.add_argument("text", type=str, default=None, help="input text") args = parser.parse_args() if args.text is not None: chinese_to_pinyin(args.text)