Linux下Python实现有道词典

  最近发现,用Linux系统比Window的工作和学习效率高多了,做任何事情都更直接有效, 而且现在绝大部分应用都是基于WEB的;所以,以后尽量用Linux了. 以下是用Python脚本实现的有道词典. 当然是参考了大牛的代码, 其中有某些部分出了点问题, 一是正则匹配, 二是颜色输出.

  Python代码的主要流程:

    a. 带输入查询词调有道的WEB API
       b. 使用正则匹配得到翻译内容
    c. 格式化输出结果, 若需要配置颜色, 增加相应的shell配色参数.

1. Python代码

  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
12 def get_elements_by_path(xml, elem):
13 if type(xml) == type(''):
14 xml = [xml];
15 if type(elem) == type(''):
16 elem = elem.split('/');
17 if (len(xml) == 0):
18 return [];
19 elif (len(elem) == 0):
20 return xml;
21 elif (len(elem) == 1):
22 result = [];
23 for item in xml:
24 result += get_elements(item, elem[0]);
25 return result;
26 else:
27 subitems = [];
28 for item in xml:
29 subitems += get_elements(item, elem[0]);
30 return get_elements_by_path(subitems, elem[1:]);
31
32 textre = re.compile("<\!\[CDATA\[(.*?)\]\]", re.DOTALL);
33 def get_text(xml):
34 #print 10*"..."+"\n"
35 match = re.search(textre, xml);
36 if not match:
37 return xml;
38 #print 40*"..."
39 #print match
40 #print 40*"..."
41 return match.group(1);
42
43 def get_elements(xml, elem):
44 p = re.compile("<" + elem + ">" + "(.*?)</" + elem + ">", re.DOTALL);
45 it = p.finditer(xml);
46 result = [];
47 for m in it:
48 result.append(m.group(1));
49 return result;
50
51 GREEN = "\033[1;32m";
52 DEFAULT = "\033[0;49m";
53 BOLD = "\033[1m";
54 UNDERLINE = "\033[4m";
55 NORMAL = "\033[m";
56 RED = "\033[1;31m"
57
58 def crawl_xml(queryword):
59 return urllib2.urlopen("http://dict.yodao.com/search?keyfrom=dict.python&q=" + urllib.quote_plus(queryword) + "&xmlDetail=true&doctype=xml").read();
60
61 def print_translations(xml, with_color, detailed):
62 #print xml;
63 original_query = get_elements(xml, "original-query");
64 queryword = get_text(original_query[0]);
65 custom_translations = get_elements(xml, "custom-translation");
66 print BOLD + UNDERLINE + queryword + NORMAL;
67 translated = False;
68
69 for cus in custom_translations:
70 source = get_elements_by_path(cus, "source/name");
71
72 print RED + "Translations from " + source[0] + DEFAULT;
73 contents = get_elements_by_path(cus, "translation/content");
74 if with_color:
75 for content in contents[0:5]:
76 print GREEN + get_text(content) + DEFAULT;
77 else:
78 for content in contents[0:5]:
79 print get_text(content);
80 translated = True;
81 yodao_translations = get_elements(xml, "yodao-web-dict");
82 printed = False;
83 for trans in yodao_translations:
84 webtrans = get_elements(trans, "web-translation");
85 for web in webtrans[0:5]:
86 if not printed:
87 print RED + "Translations from yodao:" + DEFAULT;
88 printed = True;
89 keys = get_elements(web, "key");
90 values = get_elements_by_path(web, "trans/value");
91 summaries = get_elements_by_path(web, "trans/summary");
92 key = keys[0].strip();
93 value = values[0].strip();
94 #summary = summaries[0].strip();
95 #lines = get_elements(summary, "line");
96 if with_color:
97 print BOLD + get_text(key) + ":\t" +DEFAULT + GREEN + get_text(value) + NORMAL;
98 #for line in lines:
99 # print GREEN + get_text(line) + DEFAULT;
100 #print get_text(summary) + DEFAULT;
101 else:
102 print get_text(value);
103 #print get_text(summary);
104 #translated = True;
105 #if not detailed:
106 # break
107
108 def usage():
109 print "usage: dict.py word_to_translate";
110 def main(argv):
111 if len(argv) <= 0:
112 usage();
113 #debug();
114 sys.exit(1);
115 xml = crawl_xml("".join(argv));
116 print_translations(xml, True, False);
117 #print_translations(xml, True, True);
118 #print_translations(xml, False, False);
119
120 if __name__ == "__main__":
121 main(sys.argv[1:]);


2. 添加Shell脚本

1 #!/bin/bash
2 echo -n "input:";
3 while read input;
4 do
5 python dict.py ${input};
6 echo -n "input:";
7 done

全文完

posted on 2011-11-20 00:10  nwf  阅读(3093)  评论(0编辑  收藏  举报

导航