免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 900 | 回复: 0
打印 上一主题 下一主题

用Python写个翻译工具 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-03 10:47 |只看该作者 |倒序浏览


在英语词典方面,Linux环境下的软件远不及Win环境下,由于工作一般都在Linux环境下,并且希望在堆码的时候不用离开vim编辑器,于是花了一 点时间写了个翻译的小工具,主要方便我在Linux环境下遇到不认识的英语单词时充当翻译小助手。这个小工具使用Python语言编写完成,其中使用到这 些类库(urllib, BeautifulSoup ),前者主要负责网络通讯方面,后者负责HTML的解析。这也是Python语言生态圈的强大之处,写个这样的小工具,毫不费力。
在线翻译的原理:首先根据用户输入的单词提交给 百度词典 ,其次读取百度词典返回的数据并解析,最后将处理过的数据显示给用户。以下是该工具的具体代码(Translate.py)
[Python]代码
  1. import urllib  
  2. import codecs  
  3. from BeautifulSoup import BeautifulSoup  
  4. from sys import argv  
  5. import re,time  
  6.    
  7. class Translate:  
  8.     def Start(self):  
  9.         self._get_html_sourse()  
  10.         self._get_content("enc")  
  11.         self._remove_tag()  
  12.         self.print_result()  
  13.    
  14.     def _get_html_sourse(self):  
  15.         word=argv[1] if len(argv)>1 else ''  
  16.         url="http://dict.baidu.com/s?wd=%s&tn=dict" %  word  
  17.         self.htmlsourse=unicode(urllib.urlopen(url).read(),"gb2312","ignore").encode("utf-8","ignore")  
  18.    
  19.     def _get_content(self,div_id):  
  20.         soup=BeautifulSoup("".join(self.htmlsourse))  
  21.         self.data=str(soup.find("div",{"id":div_id}))  
  22.    
  23.     def _remove_tag(self):  
  24.         soup=BeautifulSoup(self.data)  
  25.         self.outtext=''.join([element  for element in soup.recursiveChildGenerator() if isinstance(element,unicode)])  
  26.    
  27.     def print_result(self):  
  28.         for item in range(1,10):  
  29.             self.outtext=self.outtext.replace(str(item),"\n%s" % str(item))  
  30.         self.outtext=self.outtext.replace("  ","\n")  
  31.         print self.outtext  
  32.    
  33. if __name__=="__main__":  
  34.      Translate().Start()
复制代码
[Python]代码
  1. import urllib  
  2. import codecs  
  3. from BeautifulSoup import BeautifulSoup  
  4. from sys import argv  
  5. import re,time  
  6.    
  7. class Translate:  
  8.     def Start(self):  
  9.         self._get_html_sourse()  
  10.         self._get_content("enc")  
  11.         self._remove_tag()  
  12.         self.print_result()  
  13.    
  14.     def _get_html_sourse(self):  
  15.         word=argv[1] if len(argv)>1 else ''  
  16.         url="http://dict.baidu.com/s?wd=%s&tn=dict" %  word  
  17.         self.htmlsourse=unicode(urllib.urlopen(url).read(),"gb2312","ignore").encode("utf-8","ignore")  
  18.    
  19.     def _get_content(self,div_id):  
  20.         soup=BeautifulSoup("".join(self.htmlsourse))  
  21.         self.data=str(soup.find("div",{"id":div_id}))  
  22.    
  23.     def _remove_tag(self):  
  24.         soup=BeautifulSoup(self.data)  
  25.         self.outtext=''.join([element  for element in soup.recursiveChildGenerator() if isinstance(element,unicode)])  
  26.    
  27.     def print_result(self):  
  28.         for item in range(1,10):  
  29.             self.outtext=self.outtext.replace(str(item),"\n%s" % str(item))  
  30.         self.outtext=self.outtext.replace("  ","\n")  
  31.         print self.outtext  
  32.    
  33. if __name__=="__main__":  
  34.      Translate().Start()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP