29 2008

a python program to search english words

Posted by ideal

Because running stardict on my laptop always lead to a 100% cpu utilization, and I haven’t found a better dictionary software under linux, I have to use online dictionaries like stardict.cn or baidu’s dictionary (actually it is powered by DrEye). But I feel that it is not so convenient to use these online dictionaries, for example, if I am reading a document with evince, I have to switch to the browser and open a tab directing to the website. (although I can keep the tab open till the time that I don’t need it)

So I write a simple CLI python program to fetch the translation from website and print the result in the terminal. It is really simple, and now it can’t deal with example sentences, and only supports one website (other ones are similar).


01 #!/usr/bin/python
02 """Search the word"""
03
04 from sgmllib import SGMLParser
05 import sys
06
07 class SearchWord(SGMLParser):
08     def reset(self):
09         self.result = []
10         self.isInResult=False
11         SGMLParser.reset(self)
12
13     def start_div(self, attrs):
14         for key, value in attrs:
15             if key == "class" and value =="pexplain": self.isInResult=True
16
17     def end_div(self):
18         if self.isInResult:
19             self.isInResult=False
20    
21     def handle_data(self, text):
22         if self.isInResult:
23             self.result.append(text+"n")
24
25     def output(self):
26         """Return processed HTML as a single string"""
27         return "".join(self.result).decode("gb2312").encode("utf8")
28
29 def get_search():
30     import urllib
31    
32     if(len(sys.argv) < 2):
33         print "Usage: %s word" % sys.argv[0]
34         return
35     url = "http://www.baidu.com/s?lm=0&si=&rn=10&ie=gb2312&"
36           "ct=1048576&wd=%s&tn=baidu" % urllib.quote(sys.argv[1].decode("utf8").encode("gb2312"))
37     sock=urllib.urlopen(url)
38     trans=SearchWord()
39     trans.feed(sock.read())
40     print trans.output()
41     sock.close()
42     trans.close()
43
44 if __name__ == "__main__":
45     get_search()

screenshot-idealnever

Tags: ,

Leave a Reply

评论: