我有一个程序,标记泰语文本调用libtahic库。 这个程序在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
แล้วพบกันใหม่ =>
使用
aout = (c_wchar * (2 * len(ain) + 1))()
可以使代码在python3.2和3.4上工作。谢谢你,埃里克森!你知道吗相关问题 更多 >
编程相关推荐