随笔 - 314  文章 - 1 评论 - 127 阅读 - 135万
< 2025年3月 >
23 24 25 26 27 28 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 31 1 2 3 4 5

『快码』码表生成工具

建立一个基本的单字码表文件,使用UTF8格式保存为文本文件,中间用空格间隔,如base.txt:

中 zlh
华 hkgs
人 rk
民 mbg
共 gtk
和 hfl
国 glah

再建立一个要编码的词库文件,同样使用UTF8格式保存为文本文件,中间用空格间隔,如source.txt:

中国
共和国
中华人民
中华人民共和国

然后运行这个程序。程序为python3所写,大家简单安装个python环境运行即可。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------
# Name:        mbtool
# Purpose:     生成『快码』码表
#
# Author:      Garfield
#
# Created:     09-29-2022
# Copyright:   (c) Garfield 2022
# Licence:     <your licence>
#-------------------------------------------------------------------------------

# base_file = "base.txt"
# source_file = "source.txt"
des_file = "mb.txt"

base_dic = {}
source_dic = []
des_dic = {}

# 读入单字码表
def init_base_dic(base_file):
    
    with open(base_file,'r', encoding='UTF-8') as fr:
        while True:
            lines = fr.readline()
            if not lines:
                break
            wd = lines.split()
            base_dic[wd[0]] = wd[1]

    fr.close()
   
# 读入待处理的词组
def init_source_dic(source_file):
    
    with open(source_file,'r', encoding='UTF-8') as fr:
        while True:
            lines = fr.readline()
            if not lines:
                break
            source_dic.append(lines.rstrip()) # rstrip()是为了却去除后面的换行

    fr.close()    

# 获取单字的指定长度的编码
def get_mb_one_word(w,count):
    try:
        v = base_dic[w]
        return v[0:count]
    except Exception as e:
        return "ERROR"

# 获取词组的编码
def gen_mb(word):
    # 双字,规则[12][12],即取第一个的第1和第2个编码,再取第二个字的第1和第2个编码
    if len(word) == 2:  
       return get_mb_one_word(word[0],2) + get_mb_one_word(word[1],2)  
    # 三个字,规则[1][1][12],即取第一个的第1个编码,再取第二个字的第1个编码,再取第三个字的第1和第2个编码
    elif len(word) == 3:  
       return get_mb_one_word(word[0],1) + get_mb_one_word(word[1],1) + get_mb_one_word(word[2],2)  
    # 四个字以上,规则[1][1][1]...[1],即取前三个的第1个编码,再取最后一个字的第1个编码
    elif len(word) >= 4:  
       return get_mb_one_word(word[0],1) + get_mb_one_word(word[1],1) + get_mb_one_word(word[2],1) + get_mb_one_word(word[len(word)-1],1)
    else:
       return "ERROR" 
    
def main():
    init_base_dic("base.txt")
    init_source_dic("source.txt")
    
    # 要写入的码表文件
    fw = open(des_file,'w', encoding='UTF-8')
    
    # 错误处理
    fe = open("error.txt",'w', encoding='UTF-8')
    for w in source_dic:
        # 将编码写入文件,如果编码方案获取编码失败,则编码置为ERROR
        mb = gen_mb(w)
        # 如果编码时发生错误,则写到错误文件中
        if "ERROR" in mb:
            fe.writelines(w + '\n')
        else:
            fw.writelines(w + ' ' + mb + '\n')
        # print(gen_mb(w))
    fw.close()
    fe.close()
    
    print("成功生成码表文件!")

if __name__ == '__main__':
    main()

这样程序会生成一个mb.txt文件,UTF8格式保存为文本文件,中间用空格间隔:

中国 zlgl
共和国 ghgl
中华人民 zhrm
中华人民共和国 zhrg

如果有无法编码的词组,会保存到error.txt文件中。(略)

生成规则是这样(代码中有注释)

双字,规则[12][12],即取第一个的第1和第2个编码,再取第二个字的第1和第2个编码
三个字,规则[1][1][12],即取第一个的第1个编码,再取第二个字的第1个编码,再取第三个字的第1和第2个编码
四个字以上,规则[1][1][1]...[1],即取前三个的第1个编码,再取最后一个字的第1个编码

大家可以根据自己需要改写,生成的码表格式也可以自行调整,这里抛砖引玉,给大家个思路 😃

posted @ 2022-09-29 16:27 garfieldtom 阅读(135) 评论(0) 推荐(0) 编辑
摘要: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, V 阅读全文
posted @ 2022-04-15 11:03 garfieldtom 阅读(235) 评论(0) 推荐(0) 编辑
摘要: 下面的代码在我写此博客时编辑通过,很简单,如果Bing的背景图片格式发生变化,请修改对应的正则匹配代码即可。 # # Name: BingWallPaper# Purpose:## Author: xxh## Created: 28-03-2022# Copyright: (c) xxh 2022# 阅读全文
posted @ 2022-03-28 08:20 garfieldtom 阅读(85) 评论(0) 推荐(0) 编辑
摘要: Ehlib包中的汉化文件不完整,自己随便翻译了一下,只翻译了常量,窗口没有翻译,那个内容少,用的时候再翻译吧。 英文: {*******************************************************} { } { EhLib 9.0 } { EhLib 9.0.10 阅读全文
posted @ 2021-12-21 19:53 garfieldtom 阅读(208) 评论(0) 推荐(0) 编辑
摘要: location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?s=$1 last; break; } } 阅读全文
posted @ 2021-12-05 20:08 garfieldtom 阅读(20) 评论(0) 推荐(0) 编辑
摘要: 实现如图所是效果: 主从表显示代码: <template> <el-table :data="tableData" stripe style="width: 100%" max-height="500"> <el-table-column type="expand"> <template #defa 阅读全文
posted @ 2021-10-31 11:17 garfieldtom 阅读(1034) 评论(0) 推荐(0) 编辑
摘要: 刚学习弹性盒布局,实现下面的页面规划(故意弄复杂点看看如何实现) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" 阅读全文
posted @ 2021-09-28 16:10 garfieldtom 阅读(119) 评论(0) 推荐(0) 编辑
摘要: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="wi 阅读全文
posted @ 2021-07-17 09:35 garfieldtom 阅读(40) 评论(0) 推荐(0) 编辑
摘要: 跟李炎恢老师的教程一步一步实现的一个mini计算器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta n 阅读全文
posted @ 2021-07-16 14:52 garfieldtom 阅读(356) 评论(0) 推荐(0) 编辑
摘要: 如果数据库字段太多,当某一列的数据超出了定义的长度时,SQL Server会提示: 将截断字符串或二进制数据 但不提示是哪一个字段的数据超出了长度.... SQL Server 2019可以了,在2019中新建数据库,当数据超出长度时会提示: 字符串或二进制数据将在表“Member”,列“UName 阅读全文
posted @ 2021-06-17 17:10 garfieldtom 阅读(4335) 评论(0) 推荐(1) 编辑
点击右上角即可分享
微信分享提示