安卓手机上安装 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,f
if not os.path.exists(f):
print 'ERROR: %s not found' % f
return
filesize = os.path.getsize(f)
print 'ftr file size:',filesize
fp = open(f,"rb")
buf = fp.read(filesize)
fp.close()
filesize -=2
pos =0
offset =0
alist =[]
blist =[]
while pos < filesize:
offset, = unpack('>I', buf[pos:pos+4])
head = buf[pos+4:pos+6]
pos +=6
alist.append(head)
blist.append(offset)
print 'alist length:',len(alist)
buf = None
return
def read_index(word):
''' read index file '''
global f1,f2,dict1,alist,blist
dict1 ={}
offset1 =0
offset2 =0
fsize =0
len1 = 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:',fsize
if fsize<=0: break
# read index
fp1.seek(offset1)
buf = fp1.read(fsize)
fsize -=2
pos = 0
offset = 0
while 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 +=8
key = buf[pos:pos+len1]
dict1[ key ] = (offset,len2)
pos +=len1
buf = None
fp1.close()
print 'dict1 length:',len(dict1)
return
def read_dict(key):
''' read dictionary file '''
global f1,f2,dict1
fp2 = open(f2,"rb")
text =''
if key in dict1.keys():
offset,len2 = dict1[ key ]
fp2.seek(offset)
text = fp2.read(len2)
fp2.close()
return text
def cha_dict(word):
''' 模糊查词典 '''
global f1,f2,dict1,alist,blist
len1 = len(word)
text =''
klist =[]
k = None
m =0
if 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 = key
m = n
if 4<n and n>= len1-3:
klist.append(key)
#
text = read_dict(k)
klist = sorted(klist)
for key in klist:
text += key+'\n'
#
return text
def compare(word,text):
n =0
len1 = len(word)
len2 = len(text)
for i in range(0,len1):
if i<len2:
if word[i].lower()==text[i].lower():
n +=1
else:
break
return n
def cidian():
global menu,mlist,path,dic,f,f1,f2
droid.dialogCreateAlert(u'请选词典')
droid.dialogSetSingleChoiceItems(menu,0)
droid.dialogSetPositiveButtonText(u'选')
droid.dialogShow()
response = droid.dialogGetResponse().result
#if not response['which'] == 'positive': return
id = droid.dialogGetSelectedItems().result
droid.dialogDismiss()
if id: dic = mlist[int(id[0])]
else: return
print dic
if 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,blist
if len(alist)==0:
droid.fullSetProperty("Text2","text",u'请您先选词典')
return
if not os.path.exists(f1):
print 'ERROR: %s not found' % f1
return
if not os.path.exists(f2):
print 'ERROR: %s not found' % f2
return
word = droid.fullQueryDetail("Input").result["text"]
word = word.encode('utf8')
if len(word)<2: return
if 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)
return
def display(text):
# 显示文本
if text:
droid.fullSetProperty("Text2","text",text)
else:
droid.fullSetProperty("Text2","text",'not found')
return
def cha2():
global f1,f2,dict1,alist,blist
if len(alist)==0:
droid.fullSetProperty("Text2","text",u'请您先选词典')
return
if not os.path.exists(f1):
print 'ERROR: %s not found' % f1
return
if not os.path.exists(f2):
print 'ERROR: %s not found' % f2
return
word = droid.fullQueryDetail("Input").result["text"]
word = word.encode('utf8')
len1 = len(word)
if len1< 5: return
if len1>80: return
if 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)
return
txtsize = ['10','12','14','16','18','20','22']
def textsize():
global txtsize
droid.dialogCreateAlert(u'文字大小')
droid.dialogSetSingleChoiceItems(txtsize,3)
droid.dialogSetPositiveButtonText(u'选')
droid.dialogShow()
response = droid.dialogGetResponse().result
print response
id = droid.dialogGetSelectedItems().result
droid.dialogDismiss()
if id: size = txtsize[int(id[0])]
else: return
droid.fullSetProperty("Text2","textSize",size)
droid.makeToast('text size:'+size)
def eventloop():
while True:
event=droid.eventWait().result
if event["name"]=="click":
id=event["data"]["id"]
if id=="Input":
pass
if id=="cidian":
cidian()
if id=="cha":
cha()
if id=="cha2":
cha2()
if id=="textSize":
textsize()
if id=="Exit":
return
if 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":
return
layout = """<?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">
<Button
android:id="@+id/Exit"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="退出"
/>
<Button
android:id="@+id/cidian"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="词典"
/>
<Button
android:id="@+id/cha"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="查"
/>
<Button
android:id="@+id/cha2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="模糊"
/>
<Button
android: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">
<Button
android:id="@+id/Type"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="输入"
/>
<EditText
android:id="@+id/Input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="input word"
/>
</LinearLayout>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical" >
<TextView
android: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()