我有一个程序调用libtahi C库来标记泰语文本。这个程序在python3.2上运行正常,但在python3.4上运行失败。你知道3.4版本失败的原因吗?
请在下面找到两个版本的python的程序代码和输出:
#!/usr/bin/python3
# apt-get install libthai0 libthai-doc libthai-dev libthai-data
from ctypes import *
from ctypes.util import find_library
THAI_LIBRARY = find_library('thai')
if not THAI_LIBRARY:
raise OSError('Cannot find libthai in the system')
thai = cdll.LoadLibrary(THAI_LIBRARY)
thai.th_wbrk_line.restype = c_int
thai.th_wbrk_line.argtypes = [c_wchar_p,c_wchar_p,c_size_t,c_wchar_p]
def whitespace(ain):
# expects bytes
ain=ain.decode('utf8')
aout=whitespace_string(ain)
return aout.encode('utf8')
def whitespace_string(ain):
# expects a string
# ain='แล้วพบกันใหม่'
aout=' '*(2*len(ain)+1)
# we assume that the maximum length is a set of one character + white space
adelim=' '
asize=len(aout)
res=thai.th_wbrk_line(ain,aout,asize,adelim)
result=aout[:res]
return result
"""
แล้วพบกันใหม่ means 'See you later.'
and is compound by three words:
แล้ว
พบกัน
ใหม่
"""
if __name__ == "__main__":
ain='แล้วพบกันใหม่'
aout=whitespace_string(ain)
print(ain,'=>',aout)
aout=whitespace(ain.encode('utf8'))
print(aout.decode('utf8'))输出结果是:使用python3.2进行标记化:
python3.2 thai_libthai.py
แล้วพบกันใหม่ => แล้ว พบ กัน ใหม่
แล้ว พบ กัน ใหม่在python3.4中,结果字符串为空:
python3.4 thai_libthai.py
แล้วพบกันใหม่ => 发布于 2015-08-20 13:49:06
使用aout = (c_wchar * (2 * len(ain) + 1))()使代码可以在Python3.2和3.4上运行。感谢eryksun!
https://stackoverflow.com/questions/32100067
复制相似问题