码迷,mamicode.com
首页 > 编程语言 > 详细

Simple Python Dictionary :)

时间:2014-10-04 17:54:46      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:des   http   io   os   ar   for   sp   div   c   

摘自 http://github.com/panweizeng/home/blob/master/code/python/dict/dict.py 支持简单的Ch to En 和En to Ch我把它放在 /usr/bin/dict 1234567891011

$ dict 白痴

单词:白痴 音标: bái chī 释义:idiot idiocy 例句:他很聪明,但有时举止像是白痴。 翻译:He is intelligent, but sometimes he behaves like an idiot. 例句:我不讳言,我说过他是白痴。 翻译:I didn‘t mince matters: I said he was an idiot. 例句:真是白痴一个! 翻译:What a stupid idiot!  

#!/usr/bin/python
#coding=utf8
import urllib
import sys
import os
import re
import xml.dom.minidom as xml

#API_URL = ‘http://dict.cn/ws.php?utf8=true&q=%s‘
API_URL = ‘http://dict-co.iciba.com/api/dictionary.php?w=%s‘
def getword(word):
    xmls = urllib.urlopen(API_URL%urllib.quote(word)).read()
    #print xmls
    root = xml.parseString(xmls).documentElement
    #print re.sub(u‘>‘, ‘>\n‘,xmls)

    #tags = {‘key‘:‘单词‘, ‘pron‘:‘音标‘, ‘def‘:‘释义‘, ‘sent‘:‘例句‘, ‘orig‘:‘例句‘, ‘trans‘:‘翻译‘, ‘acceptation‘:‘释义‘}
    tags = {‘key‘:‘单词‘, ‘ps‘:‘音标‘, ‘def‘:‘释义‘, ‘sent‘:‘例句‘, ‘orig‘:‘例句‘, ‘trans‘:‘翻译‘, ‘acceptation‘:‘释义‘}

    def isElement(node):
        return node.nodeType == node.ELEMENT_NODE
    def isText(node):
        return node.nodeType == node.TEXT_NODE
    def show(node, tagName=None):
        if isText(node):
            tag = tags.get(tagName, tagName)
            print ‘%s:%s‘%(tag, node.nodeValue)
        elif isElement(node) and tags.has_key(node.tagName):
            [show(i, node.tagName) for i in node.childNodes]

    [ show(i) for i in root.childNodes if isElement(i) ]
    
def main():
    if len(sys.argv) >= 2:
        word = ‘ ‘.join(sys.argv[1:])
        getword(word)
        os.system(‘say %s‘ % word);

    else:
        print ‘usage:dict [word]‘


if __name__ == ‘__main__‘:
    reload(sys)
    sys.setdefaultencoding(‘utf8‘)
    main()

 

 

Simple Python Dictionary :)

标签:des   http   io   os   ar   for   sp   div   c   

原文地址:http://www.cnblogs.com/51reboot/p/4006040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!