ubuntu下的字典,使用有道词典
ubuntu下的字典实在不知道咋整,不会弄stardict,看到sourceforge上有python写的有道词典的脚本,只要联网,就可以在终端查询,用了之后,感觉很方便,所以推荐给大家。可以从http://sourceforge.net/projects/yodao-free/files/yodao-dict/这个站点下载,就一个python脚本,下载下来就可以。若是不想下载,就复制下述代码,以dict.py存储。
下述代码就是sourceforge的这个project的源码,版权属于这个youdao-free project所有,我只是推荐,不拥有版权,特此声明。
1 #! /usr/bin/python 2 import re; 3 import urllib; 4 import urllib2; 5 import sys; 6 def debug(): 7 xml = open("word.xml").read(); 8 print get_text(xml); 9 print get_elements_by_path(xml, "custom-translation/content"); 10 #print_translations(xml, False, False); 11 def get_elements_by_path(xml, elem): 12 if type(xml) == type(''): 13 xml = [xml]; 14 if type(elem) == type(''): 15 elem = elem.split('/'); 16 if (len(xml) == 0): 17 return []; 18 elif (len(elem) == 0): 19 return xml; 20 elif (len(elem) == 1): 21 result = []; 22 for item in xml: 23 result += get_elements(item, elem[0]); 24 return result; 25 else: 26 subitems = []; 27 for item in xml: 28 subitems += get_elements(item, elem[0]); 29 return get_elements_by_path(subitems, elem[1:]); 30 textre = re.compile("\!\[CDATA\[(.*?)\]\]", re.DOTALL); 31 def get_text(xml): 32 match = re.search(textre, xml); 33 if not match: 34 return xml; 35 return match.group(1); 36 def get_elements(xml, elem): 37 p = re.compile("<" + elem + ">" + "(.*?)</" + elem + ">", re.DOTALL); 38 it = p.finditer(xml); 39 result = []; 40 for m in it: 41 result.append(m.group(1)); 42 return result; 43 GREEN = "\033[1;32m"; 44 DEFAULT = "\033[0;49m"; 45 BOLD = "\033[1m"; 46 UNDERLINE = "\033[4m"; 47 NORMAL = "\033[m"; 48 RED = "\033[1;31m" 49 def crawl_xml(queryword): 50 return urllib2.urlopen("http://dict.yodao.com/search?keyfrom=dict.python&q=" 51 + urllib.quote_plus(queryword) + "&xmlDetail=true&doctype=xml").read(); 52 def print_translations(xml, with_color, detailed): 53 #print xml; 54 original_query = get_elements(xml, "original-query"); 55 queryword = get_text(original_query[0]); 56 custom_translations = get_elements(xml, "custom-translation"); 57 print BOLD + UNDERLINE + queryword + NORMAL; 58 translated = False; 59 60 for cus in custom_translations: 61 source = get_elements_by_path(cus, "source/name"); 62 63 print RED + "Translations from " + source[0] + DEFAULT; 64 contents = get_elements_by_path(cus, "translation/content"); 65 if with_color: 66 for content in contents[0:5]: 67 print GREEN + get_text(content) + DEFAULT; 68 else: 69 for content in contents[0:5]: 70 print get_text(content); 71 translated = True; 72 73 yodao_translations = get_elements(xml, "yodao-web-dict"); 74 printed = False; 75 for trans in yodao_translations: 76 webtrans = get_elements(trans, "web-translation"); 77 for web in webtrans[0:5]: 78 if not printed: 79 print RED + "Translations from yodao:" + DEFAULT; 80 printed = True; 81 keys = get_elements(web, "key"); 82 values = get_elements_by_path(web, "trans/value"); 83 summaries = get_elements_by_path(web, "trans/summary"); 84 key = keys[0].strip(); 85 value = values[0].strip(); 86 #summary = summaries[0].strip(); 87 #lines = get_elements(summary, "line"); 88 if with_color: 89 print BOLD + get_text(key) + ":\t" +DEFAULT + GREEN + get_text(value) + NORMAL; 90 #for line in lines: 91 # print GREEN + get_text(line) + DEFAULT; 92 #print get_text(summary) + DEFAULT; 93 else: 94 print get_text(value); 95 #print get_text(summary); 96 #translated = True; 97 #if not detailed: 98 # break 99 100 def usage(): 101 print "usage: dict.py word_to_translate"; 102 def main(argv): 103 if len(argv) <= 0: 104 usage(); 105 #debug(); 106 sys.exit(1); 107 xml = crawl_xml(" ".join(argv)); 108 print_translations(xml, True, False); 109 110 if __name__ == "__main__": 111 main(sys.argv[1:]);
关于使用方法,下载下dict.py后,在终端下输入
python dict.py word,这个word就是要查询的单词,中文英文都可以。为了方便使用,可以写成脚本youdao 或者yd或者 youdao.sh。
假设将dict.py和youdao存在/bin这个文件夹下,之所以存在这里,是因为大部分linux系统都将这个目录写到了系统路径里,不用再export,这样感觉很方便,虽然乱了点。
from:http://blog.csdn.net/bg2bkk/article/details/8053653