ubuntu下的字典,使用有道词典

ubuntu下的字典实在不知道咋整,不会弄stardict,看到sourceforge上有python写的有道词典的脚本,只要联网,就可以在终端查询,用了之后,感觉很方便,所以推荐给大家。可以从http://sourceforge.net/projects/yodao-free/files/yodao-dict/这个站点下载,就一个python脚本,下载下来就可以。若是不想下载,就复制下述代码,以dict.py存储。

下述代码就是sourceforge的这个project的源码,版权属于这个youdao-free project所有,我只是推荐,不拥有版权,特此声明。


#! /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):
	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;
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);

if __name__ == "__main__":
	main(sys.argv[1:]);


关于使用方法,下载下dict.py后,在终端下输入

python dict.py word,这个word就是要查询的单词,中文英文都可以。为了方便使用,可以写成脚本youdao 或者yd或者 youdao.sh。

假设将dict.py和youdao存在/bin这个文件夹下,之所以存在这里,是因为大部分linux系统都将这个目录写到了系统路径里,不用再export,这样感觉很方便,虽然乱了点。

#!/bin/bash

python /bin/dict.py $@

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

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

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

http://sourceforge.net/projects/vifm/files/vifm/

http://www.terra.es/personal7/inigoserna/lfm/

ubuntu 用户可以通过apt安装ranger,都非常经典,非常快捷,非常方便


你可能感兴趣的:(xml,python,ubuntu,Path,printing,文件管理器)