a python program to search english words

十月 29, 2008 on 11:36 下午 | In linux | No Comments

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

future is not what we think it is

十月 11, 2008 on 10:32 下午 | In life | No Comments

Now the no-exam graduation students registration is almost done and the decision that who goes to which school has also decided. Although I have the chance to choose to go to another school other than BJTU, I have never thought that I have the ability to go to cas or even BeiHang, I even have never thought that I am one of the students who have that chance. And the fact is that cas refused me finally.

At the very beginning I have agreed with Mr. Lin that I was going to stay at BJTU. That made me have a thought that breaking the promise is not appropriate.

But when I saw my classmates were busy contacting the schools, I also want to have a try at that time. If I can be accepted by cas, my father maybe will be very happy. So I send a resume to cas. But finally I can’t achieve this.

I am not good at speaking, never, and also have few planning of myself. I think that my classmates have the clear thoughts of their future life, and also mature plans. But I am not capable to be one like them.

When I came from cas, I sent a message to her, which was the message I sent months later after last one, it was her birthday. And she replied "Thank you!". That is all between us. Maybe we are just friends even since the first year when we are in this university, but why I can’t accept this fact.

Powered by WordPress with theme modified from Pool by ideal.
Entries and comments feeds. Valid XHTML and CSS.