Py学习记录
Python 常规学习
Python学习笔记(一)
Python学习笔记(二)
Python学习笔记(三)
Python学习笔记(四)
Python学习笔记(五)
Python学习笔记(六)
Python学习笔记(七)
Python习题(一)
Python习题(二)
Python习题(三)
Python习题(四)
Python习题(五)
Python常见Bug
Python编程环境
Python-依赖安装(三方库)
Python-VS Code
pip-换源
py 程序转 exe
Python-打开选择文件对话框
Python 项目
Python-密码学
Python-与佛伦禅
Python-喵语翻译
Python-翻译服务器
Python-邮件发送
Python-自动签到
Python-自动签到(Post请求)
Python-自动签到(模拟操作)
Python-图片添加二维码
Python-数据可视化
Python-端口扫描器
Python-未测试项目
Python-虚拟环境
Python-临时环境
Python-venv虚拟环境
Python-Conda
Python-OpenCV
OpenCV-人脸识别
Python-PyTorch
本文档使用 MrDoc 发布
-
+
首页
Python-喵语翻译
这个是自制Python服务器版,提供了 web 和 api 两种使用方式,由 [简易Python版](/doc/581/) 修改而来 `app.py` ```python import re import base64 import random # 随机函数库 from flask import Flask, render_template, request # 定义基础64编码字符集 b64 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/=' # 定义编码字符集,第一项为分隔字符,第二项为编码字符集 # 没有是有自动生成的,主要是可用的零宽度字符太少了,使用自动生成的会造成位数多的包含位数少相同内容,而造成匹配时出现错误匹配问题 # codes = ['\u200b','\u200c\u200d\u200e\u200f\u2029\ufeff'] # 已测试过Windows下零宽度字符 # codes = ['\u200b', '\u200c\u200d'] # 项目默认使用的零宽度字符 # 定义编码表 table = [] # 编码表预设为空 # 手动设定的编码表,使用\u200b\u200c\u200d这三个字符,\u200b作为分隔字符,\u200c\u200d作为01使用二进制方式标示出0~64,解决自动生成容易有长字符包含短字符内容问题 Defaulttable = ['\u200c\u200c\u200c\u200c\u200c\u200c\u200c','\u200b\u200c\u200c\u200c\u200c\u200c\u200c','\u200b\u200c\u200c\u200c\u200c\u200c\u200d','\u200b\u200c\u200c\u200c\u200c\u200d\u200c','\u200b\u200c\u200c\u200c\u200c\u200d\u200d','\u200b\u200c\u200c\u200c\u200d\u200c\u200c','\u200b\u200c\u200c\u200c\u200d\u200c\u200d','\u200b\u200c\u200c\u200c\u200d\u200d\u200c','\u200b\u200c\u200c\u200c\u200d\u200d\u200d','\u200b\u200c\u200c\u200d\u200c\u200c\u200c','\u200b\u200c\u200c\u200d\u200c\u200c\u200d','\u200b\u200c\u200c\u200d\u200c\u200d\u200c','\u200b\u200c\u200c\u200d\u200c\u200d\u200d','\u200b\u200c\u200c\u200d\u200d\u200c\u200c','\u200b\u200c\u200c\u200d\u200d\u200c\u200d','\u200b\u200c\u200c\u200d\u200d\u200d\u200c','\u200b\u200c\u200c\u200d\u200d\u200d\u200d','\u200b\u200c\u200d\u200c\u200c\u200c\u200c','\u200b\u200c\u200d\u200c\u200c\u200c\u200d','\u200b\u200c\u200d\u200c\u200c\u200d\u200c','\u200b\u200c\u200d\u200c\u200c\u200d\u200d','\u200b\u200c\u200d\u200c\u200d\u200c\u200c','\u200b\u200c\u200d\u200c\u200d\u200c\u200d','\u200b\u200c\u200d\u200c\u200d\u200d\u200c','\u200b\u200c\u200d\u200c\u200d\u200d\u200d','\u200b\u200c\u200d\u200d\u200c\u200c\u200c','\u200b\u200c\u200d\u200d\u200c\u200c\u200d','\u200b\u200c\u200d\u200d\u200c\u200d\u200c','\u200b\u200c\u200d\u200d\u200c\u200d\u200d','\u200b\u200c\u200d\u200d\u200d\u200c\u200c','\u200b\u200c\u200d\u200d\u200d\u200c\u200d','\u200b\u200c\u200d\u200d\u200d\u200d\u200c','\u200b\u200c\u200d\u200d\u200d\u200d\u200d','\u200b\u200d\u200c\u200c\u200c\u200c\u200c','\u200b\u200d\u200c\u200c\u200c\u200c\u200d','\u200b\u200d\u200c\u200c\u200c\u200d\u200c','\u200b\u200d\u200c\u200c\u200c\u200d\u200d','\u200b\u200d\u200c\u200c\u200d\u200c\u200c','\u200b\u200d\u200c\u200c\u200d\u200c\u200d','\u200b\u200d\u200c\u200c\u200d\u200d\u200c','\u200b\u200d\u200c\u200c\u200d\u200d\u200d','\u200b\u200d\u200c\u200d\u200c\u200c\u200c','\u200b\u200d\u200c\u200d\u200c\u200c\u200d','\u200b\u200d\u200c\u200d\u200c\u200d\u200c','\u200b\u200d\u200c\u200d\u200c\u200d\u200d','\u200b\u200d\u200c\u200d\u200d\u200c\u200c','\u200b\u200d\u200c\u200d\u200d\u200c\u200d','\u200b\u200d\u200c\u200d\u200d\u200d\u200c','\u200b\u200d\u200c\u200d\u200d\u200d\u200d','\u200b\u200d\u200d\u200c\u200c\u200c\u200c','\u200b\u200d\u200d\u200c\u200c\u200c\u200d','\u200b\u200d\u200d\u200c\u200c\u200d\u200c','\u200b\u200d\u200d\u200c\u200c\u200d\u200d','\u200b\u200d\u200d\u200c\u200d\u200c\u200c','\u200b\u200d\u200d\u200c\u200d\u200c\u200d','\u200b\u200d\u200d\u200c\u200d\u200d\u200c','\u200b\u200d\u200d\u200c\u200d\u200d\u200d','\u200b\u200d\u200d\u200d\u200c\u200c\u200c','\u200b\u200d\u200d\u200d\u200c\u200c\u200d','\u200b\u200d\u200d\u200d\u200c\u200d\u200c','\u200b\u200d\u200d\u200d\u200c\u200d\u200d','\u200b\u200d\u200d\u200d\u200d\u200c\u200c','\u200b\u200d\u200d\u200d\u200d\u200c\u200d','\u200b\u200d\u200d\u200d\u200d\u200d\u200c','\u200b\u200d\u200d\u200d\u200d\u200d\u200d'] # 生成编码表 # 注意,如果字符集数量中可用字符数量太少会造成长字符包含短字符内容问题,这种的建议手动设置可用二进制方式 def make_table(): sep = codes[0] # 获取编码分隔符 chars = codes[1] # 获取编码字符集 char_count = len(b64) # 获取基础64编码字符集的长度 code_len = len(chars) # 获取编码字符集的长度 # 循环直到编码表长度达到要求 while len(table) < char_count: table_len = len(table) # 遍历编码字符集 for i in range(code_len): c = chars[i] # 如果字符不在编码表中,添加字符 if c not in table: if len(table) >= char_count: break table.append(c) # 生成组合编码 for j in range(table_len): if len(table) >= char_count: break t = "".join([c, table[j]]) # 如果组合编码不在编码表中,添加组合编码 if t not in table: table.append(t) # 在每个编码前添加分隔符 for i in range(len(table)): table[i] = sep + table[i] # 将普通文本转换为喵语言 def human2miao(t, key, options=None): # 每次执行前根据种子重新排序 table = shuffle_array(Defaulttable,key) # 打乱后的输出存入 table # 将文本进行Base64编码 t = base64.b64encode(t.encode('utf-8')).decode('utf-8') arr = [] # 遍历编码后的文本 for c in t: # 查找字符在基础64编码字符集中的索引 n = b64.index(c) # 使用编码表进行替换 arr.append(table[n]) # 生成最终的喵语言文本 data = "".join(arr) return add_punctuations(data, options) # 将喵语言转换为普通文本 def miao2human(t,key): # 每次执行前根据种子重新排序 table = shuffle_array(Defaulttable,key) # 打乱后的输出存入 table # 判断是否是喵语言 if not is_miao(t): return "错误,非本星球语音" # 清理喵语言中的无关字符 t = clean(t) # 从编码表中查找并替换对应的基础64编码字符 for idx in range(len(table)-1, -1, -1): reg = table[idx] t = t.replace(reg, b64[idx]) # 将文本进行Base64解码 t = base64.b64decode(t.encode('utf-8')).decode('utf-8') return t # 添加标点符号,添加喵 def add_punctuations(t, options=None): # 获取调用符号和是否使用半角符号的选项 calls = options.get('calls', '喵') if options is not None else '喵' # 判断是否是使用半角字符(True半角/False全角) halfwidth_symbol = options.get('halfwidthSymbol', False) if options is not None else False a = list(t) idx = 0 # 遍历文本中的字符 while idx < len(a): c = ord(a[idx]) delta = (c % 60) + 1 idx += delta # 如果索引超出范围,结束循环 if idx >= len(a): break # 在指定位置添加调用符号 a[idx] += calls mod = idx % 32 # 根据索引位置添加标点符号 if mod in [0, 1, 2, 3]: a[idx] += ',' if halfwidth_symbol else ',' elif mod == 7: a[idx] += '.' if halfwidth_symbol else '。' elif mod == 8: a[idx] += '?' if halfwidth_symbol else '?' elif mod == 9: a[idx] += '!' if halfwidth_symbol else '!' elif mod == 10: a[idx] += '~' if halfwidth_symbol else '~' idx += 1 # 添加调用符号和结束符号 t = calls + ''.join(a) + calls + ('.' if halfwidth_symbol else '。') return t # 清理字符串中的无关字符 def clean(t): return re.sub(r'[^\u200b\u200c\u200d]', '', t) # 判断一个字符串是否是喵语言 def is_miao(t): if not t: return False return len(clean(t)) > 0 # 随机排序数组,支持传入种子以确保可以还原 def shuffle_array(arr, seed): random.seed(seed) # 设置随机种子 shuffled_arr = random.sample(arr, len(arr)) # 创建一个新的打乱数组 return shuffled_arr # 返回打乱后的数组 # 调用函数生成编码表,如果使用的是自定义编码表那么无需生成 # make_table() # 示例用法 app = Flask(__name__) @app.route('/', methods=['GET','POST']) def index(): if request.method == 'POST': human = request.form.get('human') miao = request.form.get('miao') lingo = request.form.get('lingo') key = request.form.get('key') humantomiao = request.form.get('humantomiao') miaotohuman = request.form.get('miaotohuman') if key == "": key = "zwzw" if lingo == "": lingo = "喵" print(request.form) if 'humantomiao' in request.form: miao = human2miao(human,key,{"calls": lingo}) print(miao) elif 'miaotohuman' in request.form: human = miao2human(miao,key) print(human) return render_template('index.html', miao=miao, human=human,lingo=lingo) return render_template('index.html') # API请求,提供了GET和POST两种方式,方便适配开发板的请求 @app.route('/api', methods=['GET','POST']) def api(): # 判断请求类型,post用于处理json方式 if request.method == 'POST': # 获取json json_data = request.get_json() # web访问属于get方式,http://localhost:5050/api?human=你好呀&lingo=喵 这种格式 elif request.method == 'GET': # 获取参数 json_data = request.args print(json_data) human = json_data.get('human') # 明文 miao = json_data.get('miao') # 密文 lingo = json_data.get('lingo') # 语言 key = json_data.get('key') # key if lingo == None: lingo = "喵" if key == None: key = "zwzw" if not human == None: miao = human2miao(human,key,{"calls": lingo}) print(miao) return miao elif not miao == None: human = miao2human(miao,key) print(human) return human else: return "格式不正确", 404 # 判断当前文件是否作为脚本被执行 print("启动服务...") print("Web访问:http://localhost:5050 \n\ API(Get)使用:http://localhost:5050/api?human=你好呀&lingo=喵 \n\ API(Post)使用:http://localhost:5050,json发送,human(明文),miao(密文),lingo(语言),key(键值)") if __name__ == '__main__': app.run(host='0.0.0.0', port=5050) ``` `templates\index.html`,web主页 ```html <!DOCTYPE html> <html> <head> <!-- <meta name="viewport" content="width=device-width,initial-scale=1.0" /> --> <title>喵语翻译-造物者W</title> <style> body { justify-content: center; } h1 { text-align: center; font-weight: bold; font-size: calc(5vw + 0.5rem); } .container { width: 100%; } .container .control { width: 100%; display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; margin-bottom: 10px; margin-top: 10px; } @media (max-width:400px) { .container .control { display: grid; grid-template-columns: 1fr; } } .container .control .moeBtn { margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; } .form-group { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; border: 2px solid #0089F6; } .form-group textarea { width: 100%; height: calc(50vh - 5vw - 0.5rem - 80px); padding: 5px; font-size: calc(2vw + 0.5rem); } .container .control .textarea { min-height: 30px; font-size: 45px; } </style> </head> <body> <form action="/" method="post"> <div class="container"> <h1>喵语翻译</h1> <div class="form-group"> <textarea id="human" name="human" placeholder="人话">{{ human }}</textarea> </div> <div class="control"> <input name="humantomiao" type="submit" class="moeBtn" value="人话翻译喵语 ↓↓"> <textarea name="lingo" id="lingo" style="height: 2vh;" placeholder="语言(可选)">{{ lingo }}</textarea> <textarea name="key" id="key" style="height: 2vh;" placeholder="键值(可选)">{{ key }}</textarea> <input name="miaotohuman" type="submit" class="moeBtn" value="喵语翻译人话 ↑↑"> </div> <div class="form-group"> <textarea id="miao" name="miao" placeholder="喵~">{{ miao }}</textarea> </div> </div> </form> </body> </html> ``` `Dockerfile` 部署文件 ```bash # 使用 python 作为底层镜像 FROM python:3.8-slim-buster # 设置工作目录 WORKDIR /app # 安装所需环境 RUN pip install flask -i https://pypi.tuna.tsinghua.edu.cn/simple # 复制当前目录所有文件到工作目录 COPY . . # 设置外部访问端口提示(需和内容一致) EXPOSE 5050 # 容器执行时运行命令 CMD ["python", "app.py"] ``` 构建并部署 ```bash docker build -t miaolang:latest . # 构建镜像 docker run -d --name miaolang -p 5050:5050 miaolang:latest # 部署镜像 # 直接使用构建好的 docker run -d --name miaolang -p 5050:5050 nas.918178.xyz:10088/library/miaolang:latest ```
造物者W
2023年6月1日 11:10
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码