有道辞典linux桌面版
更新版下载地址: http://www.cnblogs.com/dabaopku/archive/2010/08/27/1810040.html
分别保存成两个文件,dict.py是主程序
调用有道的翻译结果
需要 PtQt4支持
窗口为popup,失去焦点后自动结束程序
dict.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #! /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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | #! /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 | <br> |
1 | 我,大宝库博主,本作品的版權持有者,特此声明使用下列协议发表本作品:<br> 1. 任何人都可以在自由軟體基金會所公開發行之GNU自由文件協議许可证(GFDL) 1.2 或者之後的版本的授權下,複製、分發、和/或修改此文件;不附帶恆常章節、封面及封底文字等其他附帶條件。使用此文件時請標示作者姓名,並以相同方式分享。关于GFDL 1.2 的协议原文,请参阅 <a rel = "noopener nofollow" class = "external free" href = "http://www.gnu.org/licenses/fdl-1.2.html" >http: / / www.gnu.org / licenses / fdl - 1.2 .html< / a>。<br> 2. 任何人都可以在知识共享组织所公開發行之知识共享 署名 - 相同方式共享 协议(CC - BY - SA) 3.0 版本的授權下,複製、分發、和/或修改此文件。使用此文件時請標示作者姓名,並以相同方式分享。关于CC - BY - SA 3.0 的更多信息,请参阅 <a rel = "noopener nofollow" class = "external free" href = "http://creativecommons.org/licenses/by-sa/3.0/" >http: / / creativecommons.org / licenses / by - sa / 3.0 / < / a>。<br>任何人都可以根据自己的需要,自由地从上面的两种版权协议里面,選擇其中一种協議使用此文件,也可以同时選擇上面的两种版权协议使用此文件。<br> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架