zoukankan      html  css  js  c++  java
  • Simple Python Dictionary :)

    摘自 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'>', '>
    ',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()
    
     
  • 相关阅读:
    转:android WebView 文字 、图片分开加载
    js获取网页高度
    [转]URLPath匹配原则
    Java课程设计俄罗斯方块
    Three Little Habits to Find Focus
    ubuntu 12.04 无法联网的问题
    [转]时间去了哪里
    matlab 用plot在图像上面画图
    深入理解ES6临时死区(Temporal Dead Zone)
    sql 连接超时问题
  • 原文地址:https://www.cnblogs.com/51reboot/p/4006040.html
Copyright © 2011-2022 走看看