python:ETDict.mdx 的 StyleSheet 解析

用chrome 访问 https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-lzo
下载 python_lzo-1.12-cp37-cp37m-win_amd64.whl
pip install /pypi/python_lzo-1.12-cp37-cp37m-win_amd64.whl

pip install readmdict ; pip install zhconv ; 繁体字转简体字

python -m readmdict -x ETDict.mdx

解压缩后生成 ETDict.txt  文件大小:7MB => 21.5MB

StyleSheet : 定义了13种字体颜色

1
<font color="#FF0000" size=5>
</font><br>
2
<font color="#0000FF">
</font><br>
3
<font color="#FB8C42">
</font><br>
4
<font color="#FB8C42">
</font><br>
5
<font color="#04A6B5">
</font><br>
6
<font color="#9900CC">
</font><br>
7
<font color="#F27A04">
</font><br>
8
<font color="#330099">
</font><br>
9
<font color="#046AA4">
</font><br>
10
<font color="#006699">
</font><br>
11
<font color="#9933FF">
</font><br>
12
<font color="#F80AB8">
</font><br>
13
<font color="#4F86B4">
</font><br>

ETDict_bottle.py

# -*- coding: utf-8 -*-
""" web server 用于查询英汉词典 """
import os
import sys
import zhconv
from readmdict import MDX
from bottle import route, run, post, request, static_file

os.chdir("/mydir")
global headwords, items
# 加载.mdx文件
filename = "/mdict/ETDict.mdx"
mdx = MDX(filename)
headwords = [*mdx]       # 单词名列表
items = [*mdx.items()]   # 释义html源码列表
n = len(headwords)
m = len(items)
if n == m:
    print(f'{filename} 加载成功:共{n}条')
else:
    print(f'ERROR:加载失败 {n}!={m}')
    sys.exit(1)

global font_colors # 定义字体颜色
font_colors = {
 "`1`": '<font color="#FF0000" size=5>',
 "`2`": '</font><br>\n<font color="#0000FF">',
 "`3`": '</font><br>\n<font color="#FB8C42">',
 "`4`": '</font><br>\n<font color="#FB8C42">',
 "`5`": '</font><br>\n<font color="#04A6B5">',
 "`6`": '</font><br>\n<font color="#9900CC">',
 "`7`": '</font><br>\n<font color="#F27A04">',
 "`8`": '</font><br>\n<font color="#330099">',
 "`9`": '</font><br>\n<font color="#046AA4">',
 "`10`":'</font><br>\n<font color="#006699">',
 "`11`":'</font><br>\n<font color="#9933FF">',
 "`12`":'</font><br>\n<font color="#F80AB8">',
 "`13`":'</font><br>\n<font color="#4F86B4">'
}

def font_trans(txt):
    for k,v in font_colors.items():
        txt = txt.replace(k,v)
    return txt

@route('/')
def server_static(filepath="index.html"):
    return static_file(filepath, root='./')

@post('/trans')
def trans():
    """ 英译中 """
    try:
        txt = request.forms.get('txt')
    except:
        return '1: get txt error'
    if len(txt.strip()) ==0:
        return 'text is null'
    print(txt)
    if not txt.isascii():
        return 'Maybe text is not english'
    word = txt.encode()
    word1 = txt.capitalize().encode() # 第1个字母变大写
    global headwords, items
    try: # 查词,返回单词和html文件
        if word in headwords:
            wordIndex = headwords.index(word)
        else:
            wordIndex = headwords.index(word1)
        word,html = items[wordIndex]
        txt = html.decode()      #'zh-hant' 转为繁体字
        txt = zhconv.convert(txt, 'zh-hans') # 转为简体字
        result = font_trans(txt)
    except:
        result = f"<h3>{txt} is not in word_list.</h3>"
        if len(txt) > 3:
            alist = []
            word = txt.strip().lower() # 字母变小写
            for hw in headwords:
                hws = hw.decode().lower()
                if hws.startswith(word):
                    alist.append(hw.decode())
            if len(alist) > 0:
                result = '<h3>'+ ', '.join((alist)) +'</h3>'
        else:
            result = f"{txt} length too short.(length>3)"
    return result

run(host='localhost', port=8888, debug=True)

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>查询英汉词典</title>

</head>
<body>
  <form name="form" id="form" action="trans" method="POST" target="iframe">
    请输入:<input type="text" name="txt" size="30">
    <input type="submit" name="eng_han" value="英译汉">
  </form>
  <p></p>
<div id="result">
  <iframe name="iframe" id="iframe" width="90%" height="450"> </iframe>
</div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值