ubuntu下使用有道词典

转自 http://blog.csdn.net/bg2bkk/article/details/8053653

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, TrueFalse);  
  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,这样感觉很方便,虽然乱了点。

  1. #!/bin/bash  
  2.   
  3. python /bin/dict.py $@  


$@是shell脚本中,参数的意思,这样的话,使youdao这个shell文件可执行,chmod +x youdao,然后就可以在任意路径下以任意用户使用了,例如,查询presentation

  1. huang@huang-Lenovo:~/Desktop/apue$ youdao presentation  
  2. presentation  
  3. Translations from 有道词典  
  4. n. 描述,陈述;介绍;赠送  
  5. Translations from WordNet  
  6. the activity of formally presenting something (as a prize or reward)  
  7. the act of making something publicly available; presenting news or other information by broadcasting or printing it  
  8. a show or display; the act of presenting something to sight or view  
  9. the act of presenting a proposal  
  10. a visual representation of something  
  11. Translations from yodao:  
  12. Presentation:   表现  
  13. face presentation:  面先露  

 

最后向大家推荐三个linux下的非图形化界面的文件管理器,vifm,lfm和ranger

转载于:https://www.cnblogs.com/Amagasaki/articles/3447170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值