linux下方便的使用有道词典
来源:互联网 发布:伊朗人质危机 知乎 编辑:程序博客网 时间:2024/05/16 19:29
直接上图,觉得合适就用,想查单词时,复制单词粘贴到终端,非常方便。不要骗自己说你想知道音标,想知道详细的例句,大多时候我们是只需要快速知道这个单词的意思。实现方法是大牛实现的,使用python调用有道的接口,但我忘记出处了,抱歉!
我也折腾过有道词典的安装,后来安装好了,但体验非常差,之前是占用资源巨多,后来换了个高配的笔记本,还是感觉不好用。奉劝在折腾的同学一句,不要在折腾有道词典的安装。ubuntu上安装还挺方便的,centos上安装这词典真心不容易。我现在直接使用终端查单词,浏览器直接使用有道的翻译插件。挺方便的!
youdao终端查单词其实很简单,就几行python代码,我直接贴上来吧,复制代码并在/usr/local/bin下建个可执行文件就可以使用了。
sudo gedit /usr/local/bin/dict.py
粘贴代码:
#! /usr/bin/python import re; import urllib; import urllib2; import sys; def debug(): xml = open("word.xml").read(); print get_text(xml); print get_elements_by_path(xml, "custom-translation/content"); #print_translations(xml, False, False); 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): #print 10*"..."+"\n" match = re.search(textre, xml); if not match: return xml; #print 40*"..." #print match #print 40*"..." 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; GREEN = "\033[1;32m"; DEFAULT = "\033[0;49m"; BOLD = "\033[1m"; UNDERLINE = "\033[4m"; NORMAL = "\033[m"; RED = "\033[1;31m" 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 print_translations(xml, with_color, detailed): #print xml; original_query = get_elements(xml, "original-query"); queryword = get_text(original_query[0]); custom_translations = get_elements(xml, "custom-translation"); print BOLD + UNDERLINE + queryword + NORMAL; translated = False; for cus in custom_translations: source = get_elements_by_path(cus, "source/name"); print RED + "Translations from " + source[0] + DEFAULT; contents = get_elements_by_path(cus, "translation/content"); if with_color: for content in contents[0:5]: print GREEN + get_text(content) + DEFAULT; else: for content in contents[0:5]: print get_text(content); translated = True; yodao_translations = get_elements(xml, "yodao-web-dict"); printed = False; for trans in yodao_translations: webtrans = get_elements(trans, "web-translation"); for web in webtrans[0:5]: if not printed: print RED + "Translations from yodao:" + DEFAULT; printed = True; 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(); #summary = summaries[0].strip(); #lines = get_elements(summary, "line"); if with_color: print BOLD + get_text(key) + ":\t" +DEFAULT + GREEN + get_text(value) + NORMAL; #for line in lines: # print GREEN + get_text(line) + DEFAULT; #print get_text(summary) + DEFAULT; else: print get_text(value); #print get_text(summary); #translated = True; #if not detailed: # break def usage(): print "usage: dict.py word_to_translate"; def main(argv): if len(argv) <= 0: usage(); #debug(); sys.exit(1); xml = crawl_xml("".join(argv)); print_translations(xml, True, False); #print_translations(xml, True, True); #print_translations(xml, False, False); if __name__ == "__main__": main(sys.argv[1:]);
gedit /usr/local/bin/youdao
粘贴如下代码:
#!/bin/bashpython /usr/local/bin/dict.py $1;echo -n "input:";while read input;do python /usr/local/bin/dict.py ${input}; echo -n "input:";done
最后一步,给文件添加可执行权限:
sudo chmod + x /usr/local/bin/dict.py sudo chmod + x /usr/local/bin/youdao
随便打开一个终端做个测试,输入:
youdao english
看是不是可以正常查词啦!
祝好运!!
1 0
- linux下方便的使用有道词典
- LInux下的有道词典
- linux下如何使用有道词典
- ubuntu下的字典,使用有道词典
- 有道词典的使用
- Linux下安装有道词典
- linux下安装有道词典
- Linux下的英语软件——有道词典
- Linux下有道词典客户端 openyoudao
- Linux mint 下安装有道词典
- 在ubuntu下使用有道词典
- archlinux下有道词典的依赖
- Ubuntu下有道词典的安装
- 基于pygtk的linux有道词典
- 有道词典的API使用 C#
- linux 词典安装,有道
- 有道词典的问题
- 有道词典的查询
- 《More Effective C++》读书笔记-技术
- Uninstall JDK rpm to reinstall
- poj 3061(尺取法)
- PHP数据库拓展之PDO使用总结
- 注意用getchar()吃掉换行
- linux下方便的使用有道词典
- ZOJ 1586 QS Network(最小生成树)
- Java-Android
- LAMP环境安装之CentOS(二)
- 党课心得四(杂谈)
- 1015.德才论(25)——标准库排序总结(sort)
- 找工作记录(1)
- 42-Remove Nth Node From End of List
- WebRTC手记之初探