有道辞典linux桌面版
更新版下载地址: http://www.cnblogs.com/dabaopku/archive/2010/08/27/1810040.html
分别保存成两个文件,dict.py是主程序
调用有道的翻译结果
需要 PtQt4支持
窗口为popup,失去焦点后自动结束程序
dict.py
#! /usr/bin/python import sys from PyQt4 import QtGui , QtCore import lookup import commands class WinDict(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) cursor=QtGui.QCursor.pos() self.setGeometry(cursor.x()-150,20,500,500) self.setWindowFlags(QtCore.Qt.Popup) grid=QtGui.QGridLayout() grid.setSpacing(10) grid.addWidget(QtGui.QLabel("Word"), 1, 0) self.txtWord = QtGui.QLineEdit() self.txtRes = QtGui.QTextEdit() self.txtRes.setReadOnly(True) grid.addWidget(self.txtWord, 1, 1) grid.addWidget(self.txtRes, 3, 0, 5, 2) self.setLayout(grid) self.txtWord.returnPressed.connect(self.Translate) self.txtWord.setFocus() def Translate(self): res=lookup.look_up(self.txtWord.text()) reload(sys) sys.setdefaultencoding('utf-8') res=unicode(res) self.txtRes.setText(res) def hideEvent(self,event): print "Goodbye" quit() app = QtGui.QApplication(sys.argv) dict=WinDict() reload(sys) sys.setdefaultencoding('utf-8') dict.show() sys.exit(app.exec_())
lookup.py
#! /usr/bin/python # coding=gbk import re import urllib import urllib2 import sys def look_up(word): xml = crawl_xml(str(word)) res = decorate(xml) return res def get_elements_by_path(xml, elem): if type(xml) == type(''): xml = [xml] if type(elem) == type(''): elem = elem.split('/') if (len(xml) == 0): return [] elif (len(elem) == 0): return xml elif (len(elem) == 1): result = [] for item in xml: result += get_elements(item, elem[0]) return result else: subitems = [] for item in xml: subitems += get_elements(item, elem[0]) return get_elements_by_path(subitems, elem[1:]) textre = re.compile("\!\[CDATA\[(.*?)\]\]", re.DOTALL) def get_text(xml): match = re.search(textre, xml) if not match: return xml return match.group(1) def get_elements(xml, elem): p = re.compile("<" + elem + ">" + "(.*?)</" + elem + ">", re.DOTALL) it = p.finditer(xml) result = [] for m in it: result.append(m.group(1)) return result def crawl_xml(queryword): return urllib2.urlopen("http://dict.yodao.com/search?keyfrom=dict.python&q=" + urllib.quote_plus(queryword) + "&xmlDetail=true&doctype=xml").read(); def decorate(xml): #print xml original_query = get_elements(xml, "original-query") queryword = get_text(original_query[0]) res = u"<div style=\"font-family:微软雅黑\"><center><p><span style=\"font-size:25px; color:#f00;\">" + queryword + "</span>" #prounounce prou=get_elements(xml, "phonetic-symbol") if len(prou)>0: prou=get_text(prou[0]) res+="<span style=\"font-size:15px>\"> ["+prou+"]</span>" res += "</p></center>" #word form wordforms=get_elements(xml, "word-form") if len(wordforms)>0: res+="<p>" for wordform in wordforms: formname=get_elements(wordform, "name") res+=get_text(formname[0])+": " formname=get_elements(wordform, "value") res+=get_text(formname[0])+" " res+="</p>" #ec trans custom_translations = get_elements(xml, "custom-translation") if len(custom_translations)>0: ectrans=custom_translations[0] src=get_elements_by_path(ectrans, "source/name") if len(src)>0: res+="<p style=\"color:#2555B4\">"+get_text(src[0])+"</p>" trans=get_elements_by_path(ectrans, "translation/content") res+="<ul>" for tran in trans: res+="<li>"+get_text(tran)+"</li>" res+="</ul>" # phrase yodao_translations = get_elements(xml, "yodao-web-dict") if len(yodao_translations)>0: res+=u"<p style=\"color:#2555B4\">词组</p><ul>" for trans in yodao_translations: webtrans = get_elements(trans, "web-translation") for web in webtrans[0:50]: keys = get_elements(web, "key") values = get_elements_by_path(web, "trans/value") summaries = get_elements_by_path(web, "trans/summary") key = keys[0].strip() value = values[0].strip() res+= "<li>"+get_text(key) + ":\t" + get_text(value)+"</li>"; res+="</ul>" #sentence sents=get_elements(xml, "sentence-pair") if len(sents)>0: res+=u"<p style=\"color:#2555B4\">句子</p><ul>" for sent in sents: res+="<li>"+get_text(get_elements(sent, "sentence")[0]) res+="<br/>"+get_text(get_elements(sent, "sentence-translation")[0])+"</li>" res+="</ul>" #cc trans custom_translations = get_elements(xml, "custom-translation") if len(custom_translations)>1: ectrans=custom_translations[1] src=get_elements_by_path(ectrans, "source/name") if len(src)>0: res+="<p style=\"color:#2555B4\">"+get_text(src[0])+"</p>" trans=get_elements_by_path(ectrans, "translation/content") res+="<ul>" for tran in trans: res+="<li>"+get_text(tran)+"</li>" res+="</ul>" res+="</div>" return res
我,大宝库博主,本作品的版權持有者,特此声明使用下列协议发表本作品:
1.任何人都可以在自由軟體基金會所公開發行之GNU自由文件協議许可证(GFDL)1.2或者之後的版本的授權下,複製、分發、和/或修改此文件;不附帶恆常章節、封面及封底文字等其他附帶條件。使用此文件時請標示作者姓名,並以相同方式分享。关于GFDL 1.2的协议原文,请参阅 http://www.gnu.org/licenses/fdl-1.2.html。
2.任何人都可以在知识共享组织所公開發行之知识共享 署名-相同方式共享 协议(CC-BY-SA) 3.0版本的授權下,複製、分發、和/或修改此文件。使用此文件時請標示作者姓名,並以相同方式分享。关于CC-BY-SA 3.0的更多信息,请参阅 http://creativecommons.org/licenses/by-sa/3.0/。
任何人都可以根据自己的需要,自由地从上面的两种版权协议里面,選擇其中一种協議使用此文件,也可以同时選擇上面的两种版权协议使用此文件。