python:SL4A ecdict.py 英汉词典查询
安卓手机上安装 sl4a_r6.apk , 可以执行 python 2.6 。
ecdict.py 英汉词典查询程序放在 sl4a/scripts/
# -*- coding: utf-8 -*-
import android
import os,sys
from struct import pack, unpack
reload(sys)
sys.setdefaultencoding('utf-8')
droid = android.Android()menu = [u'简明英汉词典',u'美国传统词典',u'美国传统词典[双解]',]
mlist =['pwqec','pwdeeahd','pwdecahd',]
path = '/mnt/sdcard/dicts/'
dic = 'pwqec'
dict1 ={}
f = path +dic+'.ftr'
f1 = path +dic+'.idx'
f2 = path +dic+'.txt'
alist =[]
blist =[]def read_ftr():''' read ftr file '''global alist,blist,fif not os.path.exists(f):print 'ERROR: %s not found' % freturnfilesize = os.path.getsize(f)print 'ftr file size:',filesizefp = open(f,"rb")buf = fp.read(filesize)fp.close()filesize -=2pos =0offset =0alist =[]blist =[]while pos < filesize:offset, = unpack('>I', buf[pos:pos+4])head = buf[pos+4:pos+6]pos +=6alist.append(head)blist.append(offset)print 'alist length:',len(alist)buf = Nonereturndef read_index(word):''' read index file '''global f1,f2,dict1,alist,blist dict1 ={}offset1 =0offset2 =0fsize =0len1 = len(alist)fp1 = open(f1,"rb")for i in range(0,len1):if alist[i].lower() == word[0]+word[1]:offset1 = blist[i]offset2 = blist[i+1]#assert(offset2>offset1)fsize = offset2-offset1#print 'index file size:',fsizeif fsize<=0: break# read indexfp1.seek(offset1)buf = fp1.read(fsize)fsize -=2pos = 0offset = 0while pos < fsize:len1, = unpack('>H', buf[pos:pos+2])len2, = unpack('>H', buf[pos+2:pos+4])offset, = unpack('>I', buf[pos+4:pos+8])pos +=8key = buf[pos:pos+len1]dict1[ key ] = (offset,len2)pos +=len1buf = Nonefp1.close()print 'dict1 length:',len(dict1)returndef read_dict(key):''' read dictionary file '''global f1,f2,dict1fp2 = open(f2,"rb")text =''if key in dict1.keys():offset,len2 = dict1[ key ]fp2.seek(offset)text = fp2.read(len2)fp2.close()return textdef cha_dict(word):''' 模糊查词典 '''global f1,f2,dict1,alist,blistlen1 = len(word)text =''klist =[]k = Nonem =0if len1 <5: if word in dict1.keys():text = read_dict(word)if word.upper() in dict1.keys():text = read_dict(word.upper())else:for key in dict1.keys():n = compare(word,key)if m<n:k = keym = nif 4<n and n>= len1-3:klist.append(key) #text = read_dict(k)klist = sorted(klist)for key in klist:text += key+'\n'#return textdef compare(word,text):n =0len1 = len(word)len2 = len(text)for i in range(0,len1):if i<len2:if word[i].lower()==text[i].lower():n +=1else:breakreturn ndef cidian():global menu,mlist,path,dic,f,f1,f2droid.dialogCreateAlert(u'请选词典')droid.dialogSetSingleChoiceItems(menu,0)droid.dialogSetPositiveButtonText(u'选')droid.dialogShow()response = droid.dialogGetResponse().result#if not response['which'] == 'positive': returnid = droid.dialogGetSelectedItems().resultdroid.dialogDismiss()if id: dic = mlist[int(id[0])]else: returnprint dicif int(id[0])==1:droid.fullSetProperty("Type","text",u'美国')elif int(id[0])==2:droid.fullSetProperty("Type","text",u'双解')else:droid.fullSetProperty("Type","text",u'英汉')f = path +dic+'.ftr'f1 = path +dic +'.idx'f2 = path +dic +'.txt'read_ftr()def cha():global f1,f2,dict1,alist,blistif len(alist)==0:droid.fullSetProperty("Text2","text",u'请您先选词典')returnif not os.path.exists(f1):print 'ERROR: %s not found' % f1returnif not os.path.exists(f2):print 'ERROR: %s not found' % f2returnword = droid.fullQueryDetail("Input").result["text"]word = word.encode('utf8')if len(word)<2: returnif word in dict1.keys():droid.makeToast("%s in dict1" % word)else:read_index(word)text ='' if word in dict1.keys():text = read_dict(word)elif word.upper() in dict1.keys():text = read_dict(word.upper())else:for key in dict1.keys():if key.startswith(word):text += key+'\n'#display(text)returndef display(text):# 显示文本if text:droid.fullSetProperty("Text2","text",text)else:droid.fullSetProperty("Text2","text",'not found')returndef cha2():global f1,f2,dict1,alist,blistif len(alist)==0:droid.fullSetProperty("Text2","text",u'请您先选词典')returnif not os.path.exists(f1):print 'ERROR: %s not found' % f1returnif not os.path.exists(f2):print 'ERROR: %s not found' % f2returnword = droid.fullQueryDetail("Input").result["text"]word = word.encode('utf8')len1 = len(word)if len1< 5: returnif len1>80: returnif len(dict1)==0:droid.makeToast(u"请先按[查]")if word in dict1.keys():droid.makeToast("%s in dict1" % word)else:read_index(word)text = cha_dict(word)display(text)returntxtsize = ['10','12','14','16','18','20','22']
def textsize():global txtsizedroid.dialogCreateAlert(u'文字大小')droid.dialogSetSingleChoiceItems(txtsize,3)droid.dialogSetPositiveButtonText(u'选')droid.dialogShow()response = droid.dialogGetResponse().resultprint responseid = droid.dialogGetSelectedItems().resultdroid.dialogDismiss()if id: size = txtsize[int(id[0])]else: returndroid.fullSetProperty("Text2","textSize",size)droid.makeToast('text size:'+size)def eventloop():while True:event=droid.eventWait().resultif event["name"]=="click":id=event["data"]["id"]if id=="Input":passif id=="cidian":cidian()if id=="cha":cha()if id=="cha2":cha2()if id=="textSize":textsize()if id=="Exit":returnif id=="Type":txt = droid.fullQueryDetail("Input").result["text"]if txt: droid.ttsSpeak(txt)else: droid.makeToast(u'请您先输入')elif event["name"]=="screen":if event["data"]=="destroy":returnlayout = """<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/background"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent" android:background="#ff000000"><LinearLayout android:id="@+id/linearLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/Exit"android:layout_width="60dp"android:layout_height="wrap_content"android:text="退出"/><Buttonandroid:id="@+id/cidian"android:layout_width="60dp"android:layout_height="wrap_content"android:text="词典"/><Buttonandroid:id="@+id/cha"android:layout_width="60dp"android:layout_height="wrap_content"android:text="查"/><Buttonandroid:id="@+id/cha2"android:layout_width="60dp"android:layout_height="wrap_content"android:text="模糊"/><Buttonandroid:id="@+id/textSize"android:layout_width="60dp"android:layout_height="wrap_content"android:text="size"/></LinearLayout><LinearLayout android:id="@+id/linearLayout2"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/Type"android:layout_width="60dp"android:layout_height="wrap_content"android:text="输入"/><EditTextandroid:id="@+id/Input"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="text"android:hint="input word"/></LinearLayout><ScrollViewandroid:id="@+id/scrollView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:scrollbars="vertical" android:fadingEdge="vertical" ><TextViewandroid:id="@+id/Text2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:singleLine="false"android:textSize="16"android:textColor="#004000"android:background="#FFFFF0"android:padding="10dp"android:hint="请您先选词典"/></ScrollView>
</LinearLayout>
"""droid.fullShow(layout)
eventloop()
droid.fullDismiss()
python:SL4A ecdict.py 英汉词典查询相关推荐
- 通过游戏编程学Python(6)— 英汉词典、背单词
通过游戏编程学Python 通过游戏编程学Python(番外篇)- 乱序成语.猜单词 通过游戏编程学Python(5)- 猜成语(下) 通过游戏编程学Python(4)- 猜成语(上) 文章目录 通过 ...
- Python C/S 网络编程(二)之 UDP 实现英汉词典查询小程序
一.传输层协议 IP协议:支持数据包传输至正确的机器 但若两个应用程序要维护一个会话怎么办? IP层以上的协议,需两个额外特性:多路复用和可靠传输(UDP\TCP) 多路复用即为两台主机间传送的大量数 ...
- python用爬虫制作英汉词典
只是给大家提供思路,可以将爬取的信息,利用tkinter制作gui界面 这是图片,大家也可以换上自己的图片 前面几行需要用到的库要提前下载好 from tkinter import * import ...
- python:mdict + bottle = web 查询英汉词典
pip install readmdict ; 参见:使用Python调用mdx字典文件进行查词 安装 Mdict 去 MDict pip install bottle mdict_bottle.py ...
- python:mdict + flask = web 查询英汉词典,汉英词典
pip install pinyin; pip install snownlp; pip install flask pip install readmdict ; 参见:使用Python调用mdx字 ...
- Python基础笔记_Day14_Python图形化界面、Python GUI、tkinter、Python英汉词典
Day14_Python图形化界面.Python GUI.tkinter.Python英汉词典 14.01_Python语言基础(Python实现英汉字典)(掌握) 14.02_Python语言基础( ...
- python软件界面翻译_python英汉词典,在线翻译器,带GUI界面下载
使用请求模块获得的职位信息有道词典网页,BeautifulSoup获得所需的内容,并生成gui界面通过tkinter模块.2. Python实现翻译工具git源代码地址代码如下:# !\/bin\/e ...
- python英汉词典
# -*- coding: utf-8 -*- """ Created on Tue Dec 18 01:06:45 2018@author: Administrator ...
- Python对英汉词典的若干操作(#10)
从网上下载了一个英汉词典txt版,整理后放在了py文件同一目录下,取名为"英汉词典".后面要用正则表达式处理这些单词. 1.读取英汉词典前10行 f=open("英汉词典 ...
最新文章
- linux分享一:网络设置
- Protobuf3语言指南
- 深入理解分布式系统中的缓存架构(上)
- OSGi环境中的Servlet基本身份验证
- linux降低eigen3版本_玩转 Windows 自带的 Linux 子系统 (图文指南)
- Android_Service组件详解
- Luogu P1039 侦探推理(模拟+枚举)
- Altlas M1 and Tips of Debugger Visualizers
- python 可视化_Python可视化二维高斯分布
- C语言题目——三子棋游戏
- angular应用coreui框架
- html页面颜色排列,HTML前端页面颜色的四种方法,色号表
- 使用快捷指令和carplay发送停车位置(高德地图)
- linux格式化硬盘怎么那么慢,linux系统中怎么格式化硬盘
- Rxjava(Subject)-BehaviorSubject--代码分析
- 【06月21日】北上资金持股比例排名
- 【光照感知子场:差分感知融合模块与中间融合策略相结合】
- 【多项式最小二乘拟合实验】
- MATLAB hilbert谱 纵坐标由归一化频率改为正常频率的方法
- 计算机音乐游山恋,小红书十大歌曲 游山恋垫底,第一你绝对听过
热门文章
- IPguard服务器升级步骤详解
- python实现按键精灵找色点击功能,使用pywin32和Pillow库
- 固态硬盘计算机意外,在SSD上安装win10时是否需要手动执行“ 4K对齐”和“打开AHCI”?...
- 财务大集中虚拟化解决方案
- JS对时间的常用处理
- 百度无人车阿波龙量产背后,还有这些秘密
- Educational Codeforces Round 114 (Rated for Div. 2) 个人题解
- JS下载文件到本地集合
- pythonweb视频播放器_干货分享,Python与PyQT制作视频播放器
- 拓荒“产业AI”,阿里云正式发布ET大脑、金融及航空大脑问世