码小品|任意汉字自动做成拼音字典

博思AIPPT
码小品|任意汉字自动做成拼音字典

这个东东有什么用呢?可以生成不支持汉字的AI模型训练文件夹或标签的字典,用映射存储或显示汉字。也可以给小朋友做一个拼音学习汉字程序。

# 汉字自动生成拼音及声调,可用于自动生成拼音标签、语音识别等场景# 安装库:pip install pypinyin# 百香果AI山海 AI生成并调试# 首先尝试导入python-pinyin库python_pinyin_available =Falsefrompypinyinimportpinyinaspy_pinyin, Stylepython_pinyin_available =TrueclassIntelligentPinyinGenerator: def__init__(self): self.custom_mappings = { # 可以根据需要添加映射 }
# 拼音风格设置 self.tone_style = Style.NORMAL # 不带声调 self.use_python_pinyin =True
defadd_custom_mapping(self, chinese_str, pinyin_str): """添加自定义的汉字-拼音映射""" self.custom_mappings[chinese_str] = pinyin_str returnTrue
defremove_custom_mapping(self, chinese_str): """移除自定义的汉字-拼音映射""" ifchinese_strinself.custom_mappings: delself.custom_mappings[chinese_str] returnTrue returnFalse
defget_pinyin(self, chinese_str, use_custom_first=True): """ 生成汉字的拼音
参数: chinese_str: 要转换的汉字字符串 use_custom_first: 是否优先使用自定义映射
返回: 对应的拼音字符串 """ # 优先检查是否有自定义映射 ifuse_custom_firstandchinese_strinself.custom_mappings: returnself.custom_mappings[chinese_str]
# 如果没有自定义映射,则根据使用的库生成拼音 try: ifself.use_python_pinyin: # 使用python-pinyin库 result =''.join(py_pinyin(chinese_str, style=self.tone_style, heteronym=False)[0]) else: # 使用pinyin库 result = pinyin.get(chinese_str, delimiter="").lower() returnresult exceptExceptionase: print(f"转换拼音时出错:{str(e)}") returnNone
defbatch_get_pinyin(self, chinese_list, use_custom_first=True): """批量转换汉字列表为拼音""" return[self.get_pinyin(item, use_custom_first)foriteminchinese_list]
defget_characters_pinyin(self, chinese_str, use_custom_first=True): """ 获取汉字词语中每个字的拼音
参数: chinese_str: 要转换的汉字字符串 use_custom_first: 是否优先使用自定义映射
返回: 包含每个汉字拼音的列表(不再包含汉字本身) """ # 优先检查是否有自定义映射 ifuse_custom_firstandchinese_strinself.custom_mappings: # 对于自定义映射,我们无法拆分到单个汉字,返回整个拼音 return[self.custom_mappings[chinese_str]]
# 尝试获取每个汉字的拼音 try: result = [] ifself.use_python_pinyin: # 使用python-pinyin库获取每个汉字的拼音 pinyin_list = py_pinyin(chinese_str, style=self.tone_style, heteronym=False) forpyinpinyin_list: result.append(py[0]) # 只添加拼音,不添加汉字 else: # 对于pinyin库,我们需要逐个汉字处理 forcharinchinese_str: py = pinyin.get(char, delimiter="").lower() result.append(py) # 只添加拼音,不添加汉字 returnresult exceptExceptionase: print(f"获取汉字拼音时出错:{str(e)}") return[None] *len(chinese_str)# 示例用法if__name__ =="__main__": # 创建拼音生成器实例 pinyin_generator = IntelligentPinyinGenerator()
# 测试词语列表 test_words = [ '扬子鳄','白鱀豚', '中国','人工智能','程序设计' ]
# 演示批量转换 print("首拼转换:") print(pinyin_generator.batch_get_pinyin(test_words))
# 演示显示每个汉字的拼音(只输出拼音,不输出汉字) print("
每个汉字的拼音(只输出拼音):")
print("-"*30) forwordintest_words: char_pinyins = pinyin_generator.get_characters_pinyin(word) char_pinyin_str =" ".join([pyifpyelse"拼音转换失败"forpyinchar_pinyins]) print(f"{word.ljust(6)}: {char_pinyin_str}") # 输出汉字对应拼音的字典 print("
汉字对应拼音的字典:")
word_pinyin_dict = {} forwordintest_words: pinyin_result = pinyin_generator.get_pinyin(word) char_pinyins = pinyin_generator.get_characters_pinyin(word) char_pinyin_str =" ".join([pyifpyelse"拼音转换失败"forpyinchar_pinyins]) ifchar_pinyin_str: word_pinyin_dict[word]=char_pinyin_str.replace(" ","") print(word_pinyin_dict)

#附: 如果使用的是python-pinyin库,可以演示切换拼音风格 ifhasattr(pinyin_generator,'use_python_pinyin')andpinyin_generator.use_python_pinyin: frompypinyinimportStyle pinyin_generator.tone_style = Style.TONE # 带声调 # 演示带声调的每个汉字拼音(只输出拼音) print("
带声调的每个汉字拼音(只输出拼音):")
print("-"*30) forwordintest_words: char_pinyins = pinyin_generator.get_characters_pinyin(word, use_custom_first=False) char_pinyin_str =" ".join([pyifpyelse"拼音转换失败"forpyinchar_pinyins]) print(f"{word.ljust(6)}: {char_pinyin_str}")

© 版权声明

相关文章