tttwwy / pyjft

Automatically exported from code.google.com/p/pyjft
0 stars 0 forks source link

修改了j2f函数,可以把混合了英文字母,数字,特殊字符等和简体中文混排的字符串转成繁体字符串。 #1

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
您好,我修改了一下 j2f 
简体转繁体的函数,基本上可以实现带有字符,逗号,英
文,数字等特殊符合和中文简体混排的内容也可以正常的转��
�成繁体字符串。 下面的
代码供您参考。

def _j2f(nc, destinationEncoding):
    pos = jt.find(nc)
    while pos>0:
        if pos%2 == 0:
            return unicode(ft[pos:pos+2],'gbk').encode(destinationEncoding)
            break
        pos = jt.find(nc,pos+1)
    return None

def j2f(sourceEncoding,destinationEncoding,sourceString):
    '''
    Simplified Chinese character to traditional Chinese character converter

    Parameters:
        sourceEncoding : The encoding codec name of string to be converted
        destinationEncoding : The encoding codec name of output string
        sourceString : The string to be converted

    Return value:
        The output traditional Chinese character string
    '''
    des = []
    nString = unicode(sourceString,sourceEncoding).encode('gbk')
    #print nString
    sLen = len(nString)
    i = 0
    while i < sLen:
        nc = nString[i:i+2]
        fnc = _j2f(nc,destinationEncoding)
        if fnc:
            des.append(fnc)
            i += 2
        else:
            des.append(nString[i:i+1])
            i += 1

    return ''.join(des)

其他部分都不用改变。
联系人:lidongok # gmail.com

Original issue reported on code.google.com by lidongok@gmail.com on 1 Dec 2009 at 7:50

Attachments:

GoogleCodeExporter commented 9 years ago
您好,我想对您的改动发表一点我的看法。

在您的改进版本中,如果用户对“一条记录”进行翻译,就��
�出错:
>>> a = "一条记录"
>>> b = jft2.j2f('gbk', 'gbk', a)
>>> print b
一条兼@件
>>>

针对这个问题,我阅读了您的代码,然后发现问题的原因是��
�样的:
“一”的编码是\xd2\xbb
若j2f以“一”为参数调用_j2f函数,那么,pos的值会被赋为0,
这时,由于您的while条件的写法,_j2f会返回NONE,从而间接导�
��了混乱。对此,我认为,有必要在jt和ft的开头处加入"\x00\x0
0",这样就可以解决问题了。

Original comment by interest...@gmail.com on 7 Mar 2014 at 9:11

Attachments: