本文最后更新于 2024年12月31日 晚上
前言
老婆在学校打人名拼音很麻烦,于是想着用学生姓名创建词库导入输入法,但发现词频不够高不能自定义,还是不方便,于是采用了搜狗输入法的短语功能,通过输入姓名拼音缩写,快速打出姓名,节省时间。
搜狗输入法短语格式
搜狗输入法的短语功能可以自定义短语,格式如下:
缩写,序号=姓名
序号是指缩写相同情况下,该姓名的顺序,序号越小,排在越前面。
批量生成短语
输入是姓名txt文件,每行一个姓名,names.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import pinyin
def get_pinyin_abbreviation(name): pinyin_list = pinyin.get(name, format="strip", delimiter=" ") abbreviation = "" for p in pinyin_list.split(" "): abbreviation += p[:1] return abbreviation
def main(): result_dict = {} with open("names.txt", "r", encoding="utf-8") as file: lines = file.readlines() with open("results.txt", "w", encoding="utf-8") as output_file: for line in lines: name = line.strip() abbreviation = get_pinyin_abbreviation(name) if abbreviation in result_dict: result_dict[abbreviation][1] += 1 output_line = f"{abbreviation},{result_dict[abbreviation][1]}={name}\n" else: result_dict[abbreviation] = [name, 1] output_line = f"{abbreviation},1={name}\n" output_file.write(output_line)
if __name__ == "__main__": main()
|