Ejemplo n.º 1
0
def convertor(test, encoding=""):
    """
    convert zhpy source (Chinese) to Python Source 
    
    >>> convertor("印出 'hello'")
    "print 'hello'"
    
    >>> convertor("印出 'hello'", encoding="utf8")
    "print 'hello'"
    
    more keyword test cases are in /tests folder.
    """
    for k, v in replacedict.items():
        test = test.replace(k,v)
    
    if encoding:
        utest = test.decode(encoding)
    else:
        try:
            #detect encoding
            det = chardet.detect(test)
            if det['confidence'] >= 0.8:
                encoding = chardet.detect(test)['encoding']
            else :
                #print 'low confidence encoding detection, use utf8 encoding'
                encoding = 'utf8'
            utest = test.decode(encoding)
        except UnicodeDecodeError, e:
            print "can't recognize your language, set to utf-8"
            utest = test.decode('utf8')
        except ImportError, e:
            #no chardet mode
            utest = test.decode('utf8')
Ejemplo n.º 2
0
def convertor(test, encoding=""):
    """
    convert zhpy source (Chinese) to Python Source 
    
    >>> convertor("印出 'hello'")
    "print 'hello'"
    
    >>> convertor("印出 'hello'", encoding="utf8")
    "print 'hello'"
    
    more keyword test cases are in /tests folder.
    """
    for k, v in replacedict.items():
        test = test.replace(k, v)

    if encoding:
        utest = test.decode(encoding)
    else:
        try:
            #detect encoding
            encoding = chardet.detect(test)['encoding']
            utest = test.decode(encoding)
        except UnicodeDecodeError, e:
            print "can't recognize your language, set to utf-8"
            utest = test.decode('utf8')
        except ImportError, e:
            #no chardet mode
            utest = test.decode('utf8')
Ejemplo n.º 3
0
def convertor(test, encoding=""):
    """
    convert zhpy source (Chinese) to Python Source.
    
    always run annotator before access convertor
    
    >>> annotator()
    >>> convertor("印出 'hello'")
    "print 'hello'"
    
    >>> convertor("印出 'hello'", encoding="utf8")
    "print 'hello'"
    
    more keyword test cases are in /tests folder.
    """
    for k, v in replacedict.items():
        test = test.replace(k, v)

    if encoding:
        utest = test.decode(encoding)
    else:
        try:
            #detect encoding
            det = chardet.detect(test)
            if det['confidence'] >= 0.8:
                encoding = chardet.detect(test)['encoding']
            else:
                #print 'low confidence encoding detection, use utf8 encoding'
                encoding = 'utf8'
            utest = test.decode(encoding)
        except UnicodeDecodeError, e:
            print "can't recognize your language, set to utf-8"
            utest = test.decode('utf8')
        except ImportError, e:
            #no chardet mode
            utest = test.decode('utf8')